SpringBoot + thymeleaf 實(shí)現(xiàn)分頁(yè)
SpringBoot
結(jié)合Thymeleaf
實(shí)現(xiàn)分頁(yè),很方便。
效果如下
后臺(tái)代碼
項(xiàng)目結(jié)構(gòu)
1. 數(shù)據(jù)庫(kù)Config
由于hibernate
自動(dòng)建表字符集為latin
不能插入中文,故需要在application.properties
中指定:spring.jpa.properties.hibernate.dialect=com.ikeguang.paging.config.MysqlConfig
。
MysqlConfig.java
代碼:
2. 實(shí)體類Model
3. Jpa操作數(shù)據(jù)庫(kù)
UserRepository.java
代碼
4. service層
UserService
代碼
service實(shí)現(xiàn)層
UserServiceImpl.java
代碼
5. Controller層
UserController .java
代碼
application.properties配置文件
主要配制了mysql
數(shù)據(jù)源,數(shù)據(jù)庫(kù)驅(qū)動(dòng)com.mysql.cj.jdbc.Driver
,對(duì)于mysql-connector-java
用的6.0
以上的,如果用com.mysql.jdbc.Driver
,就會(huì)報(bào)錯(cuò)。
spring.datasource.url=jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 表不存在則新建表
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=com.ikeguang.paging.config.MysqlConfig
spring.jpa.show-sql= true
spring.thymeleaf.cache=false
模板文件
這里用了bootstrap.css
里面的樣式。這里主要展示一下分頁(yè)代碼,前面的table
主要裝一個(gè)Pageable
的N
條數(shù)據(jù),接著是一個(gè)add
添加數(shù)據(jù)的按鈕,最下面就是分頁(yè)部分,主要有5
部分:首頁(yè),上一頁(yè),中間頁(yè),下一頁(yè),尾頁(yè)
。
代碼的Github
地址:代碼地址
作者:柯廣的網(wǎng)絡(luò)日志
微信公眾號(hào):Java大數(shù)據(jù)與數(shù)據(jù)倉(cāng)庫(kù)