使用IDEASpringBoot和JPA整合分页
效果展示:
步骤一:工程搭建
备注:(web–thymeleaf–JPA–JDBC–MySql)
步骤二:创建全局配置文件application.yml
spring:
datasource:
url: jdbc:mysql://localhost/jpa_onetomany
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
naming:
#表中字段命名策略,这里要引入hibernate的核心包,不然这个命名策略会报错
implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
strategy: org.hibernate.cfg.ImprovedNamingStrategy
show-sql: true
thymeleaf:
mode: LEGACYHTML5
cache: false
mvc:
date-format: yyyy-MM-dd
server:
port: 9090
步骤三:pom.xml文件需要引入两个依赖
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
<!—通过webjars导入依赖-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1-1</version>
</dependency>
步骤四:创建实体类
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer eid;
@Column
private String ename;
@Column
private String gender;
@Column
private Integer age;
@Column
private Date birth;
public Employee(){
super();
}
public Employee(String ename, String gender, Integer age, Date birth) {
this.ename = ename;
this.gender = gender;
this.age = age;
this.birth = birth;
}
public Integer getEid() {
return eid;
}
public void setEid(Integer eid) {
this.eid = eid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return "Employee{" +
"eid=" + eid +
", ename='" + ename + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
", birth=" + birth +
'}';
}
}
步骤五:编写EmployeeRepository接口
@Repository
public interface EmployeeRepository extends JpaRepository<Employee,Integer> {
}
步骤六:编写EmployeeController
分页实现:
JpaRepository ------Pageable------PageRequest
@Controller
public class EmployeeController {
@Autowired
EmployeeRepository employeeRepository;
@RequestMapping("/user/findAll")
public String findAll(@RequestParam(value="start",defaultValue = "0") Integer start,
@RequestParam(value="limit",defaultValue = "2") Integer limit, Model model){
//分页
start=start<0?0:start;
Pageable pageable=new PageRequest(start,limit);
Page<Employee> page = employeeRepository.findAll(pageable);
model.addAttribute("page",page);
return "list";
}
}
步骤七:在template中编写list.html
引入thymeleaf的命名空间:
xmlns:th="http://www.thymeleaf.org"
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1">
<tr>
<td>编号</td>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
<td>生日</td>
<td>操作</td>
</tr>
<tr th:each="emp:${page}">
<td th:text="${emp.eid}"></td>
<td th:text="${emp.ename}"></td>
<td th:text="${emp.gender}"></td>
<td th:text="${emp.age}"></td>
<td th:text="${#dates.format(emp.birth,'yyyy-MM-dd')}"></td>
<td>修改 删除</td>
</tr>
</table>
<a th:href="@{/user/findAll(start=0)}" target="_blank" rel="external nofollow" >首页</a>
<a th:if="${not page.isFirst()}" th:href="@{/user/findAll(start=${page.number-1})}" target="_blank" rel="external nofollow" >上一页</a>
<a th:if="${not page.isLast()}" th:href="@{/user/findAll(start=${page.number+1})}" target="_blank" rel="external nofollow" >下一页</a>
<a th:href="@{/user/findAll(start=${page.totalPages-1})}" target="_blank" rel="external nofollow" >尾页</a>
第<span th:text="${page.number+1}"></span>页/共<span th:text="${page.totalPages}" ></span>页
共<span th:text="${page.totalElements}" ></span>条
</body>