一、pom.xml
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
二、application.properties
#分页插件
pagehelper.helper-dialect=mysql
pagehelper.params=count=countSql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
三、Controller
@RequestMapping("/queryEmpPage2")
public String queryEmpPage2(@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "10") int pageSize, Model model) {
PageHelper.startPage(pageNo,pageSize);
//查询全部数据
List<Emp> emps = empService.selectAll();
model.addAttribute("emp1", emps);
//用PageInfo对结果进行包装
PageInfo<Emp> info = new PageInfo<>(emps);
model.addAttribute("infoo", info);
return "index";
}
四、Thymeleaf 页面
<p th:align="center"><a th:href="'?pageNo=1'">首页</a><a th:href="'?pageNo='+${infoo.prePage}">上一页</a>当前第 <span th:text="${infoo.pageNum}"></span> 页,
总 <span th:text="${infoo.pages}"></span> 页,共 <span th:text="${infoo.total}"></span> 条记录,<a th:href="'?pageNo='+${infoo.nextPage}">下一页</a>,
<a th:href="'?pageNo='+${infoo.pages}">尾页</a></p>
本文介绍如何使用PageHelper分页插件实现Spring Boot应用中的分页功能。通过配置pom.xml引入依赖,并设置application.properties文件来指定数据库方言及分页参数。文章详细展示了Controller层如何使用PageHelper进行分页查询,以及Thymeleaf页面中分页导航的实现。
223

被折叠的 条评论
为什么被折叠?



