实现分页插件的具体步骤:
1.导入jar包
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2.(两种)在application.yml(propreties)文件加上
第一种:yml
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
params: count=countSql
第二种:propreties
pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql
3.在controller控制层编写代码
@GetMapping("/getAllPerson")
public String getAllPerson(Model model,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,
String singleName){
PageHelper.startPage(pageNum,2); //设置当前页码,每页的行数
List<Tabulation> list = aaService.getAllPerson(singleName); //查询的集合
PageInfo<Tabulation> pageInfo = new PageInfo<Tabulation>(list); //把查询的集合放入pageInfo返回给页面
model.addAttribute("pageInfo",pageInfo);
model.addAttribute("name",singleName);
return "index";
}
4.编写页面
<p>当前 <span th:text="${pageInfo.pageNum}"></span> 页,总 <span th:text="${pageInfo.pages}"></span> 页,共 <span th:text="${pageInfo.total}"></span> 条记录</p>
<a th:href="@{/getAllPerson}">首页</a>
<a th:href="@{/getAllPerson(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}">上一页</a>
<a th:href="@{/getAllPerson(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}">下一页</a>
<a th:href="@{/getAllPerson(pageNum=${pageInfo.pages})}">尾页</a>
5.运行
总结
使用分页插件无需创建page类,也不用在查询语句中写limit