Spring Boot入门,整合PAgehelper分页插件
1.在pom文件中引入Pagehelper分页插件
1 2 3 4 5 | <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> |
2.配置分页插件
pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql
3.controller代码
1 2 3 4 5 6 7 8 | @RequestMapping( "findAll" ) public String findAll(Model model,@RequestParam(defaultValue = "1" ,value = "pageNum" ) Integer pageNum){ PageHelper.startPage(pageNum,5); List<ImgTable> list=service.findAll(); PageInfo<ImgTable> pageInfo = new PageInfo<ImgTable>(list); model.addAttribute( "pageInfo" ,pageInfo); return "list" ; } |
其中:PageHelper.startPage(int PageNum,int PageSize):用来设置页面的位置和展示的数据条目数,我们设置每页展示5条数据。PageInfo用来封装页面信息,返回给前台界面。PageInfo中的一些我们需要用到的参数如下表:
PageInfo.list 结果集
PageInfo.pageNum 当前页码
PageInfo.pageSize 当前页面显示的数据条目
PageInfo.pages 总页数
PageInfo.total 数据的总条目数
PageInfo.prePage 上一页
PageInfo.nextPage 下一页
PageInfo.isFirstPage 是否为第一页
PageInfo.isLastPage 是否为最后一页
PageInfo.hasPreviousPage 是否有上一页
PageHelper.hasNextPage 是否有下一页
4.list页面代码
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div align="center"> <table border="1"> <tr> <th>id</th> <th>姓名</th> <th>密码</th> </tr> <tr th:each="product:${pageInfo.list}"> <td th:text="${product.userId}"></td> <td th:text="${product.userName}"></td> <td th:text="${product.userPwd}"></td> <td th:text="${product.userImg}"></td> <td th:text="${product.userMoney}"></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="@{findAll}">首页</a> <a th:href="@{findAll(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}">上一页</a> <a th:href="@{findAll(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}">下一页</a> <a th:href="@{findAll(pageNum=${pageInfo.pages})}">尾页</a> </div> </body> </html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理