一 说明
1.1 说明
项目的基本架构,的搭建参考:https://blog.csdn.net/u011066470/article/details/107692617 springboot整合mybaits, 实现crud操作(全) springboot的持久化
1.2 项目的结构
1.3 实现访问效果
二 分页的配置
2.1 pom文件
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
2.2 application配置文件的配置
#分页插件
pagehelper:
helper-dialect: mysql
params: count=countSql
reasonable: true #开启优化,如果开启优化,在分页页码结果没有数据的时候,会显示有数据的页码数据
support-methods-arguments: true #是否支持接口参数来传递分页参数,默认false
2.3 编写controller
@GetMapping("/getAllPerson")
public String getAllPerson(Model model,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
PageHelper.startPage(pageNum,5);
List<Users> list = this.usersService.findUserAll();
PageInfo<Users> pageInfo = new PageInfo<Users>(list);
model.addAttribute("pageInfo",pageInfo);
//model.addAttribute("list", list);
return "user_list";
}
2.4 html页面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div align="center">
<table border="1">
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
</tr>
<tr th:each="person:${pageInfo.list}">
<td th:text="${person.id}"></td>
<td th:text="${person.name}"></td>
<td th:text="${person.age}"></td>
</tr>
</table>
<p>当前 <span th:text="${pageInfo.pageNum}"></span> 页,总 <span th:text="${pageInfo.pages}"></span> 页,共 <span th:text="${pageInfo.total}"></span> 条记录</p>
<a th:href="@{/users/getAllPerson}">首页</a>
<a th:href="@{/users/getAllPerson(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}">上一页</a>
<a th:href="@{/users/getAllPerson(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}">下一页</a>
<a th:href="@{/users/getAllPerson(pageNum=${pageInfo.pages})}">尾页</a>
</div>
</body>
</html>
2.5 访问
第一页:
第二页: