一、 POM导入PageHelper

二、 Restful API 分页 的Controller
1) 直接编写Controller
@RequestMapping("/DeptDomainAPIPaging")
@ResponseBody
public List<DeptDomain> DeptDomainAPIPaging(int currentPage,int pageSize){
return deptService.findItemByPage(currentPage, pageSize);
}
- POSTMAN 直接访问 http://localhost:8080/test/DeptDomainAPIPaging?currentPage=2&pageSize=10 即可获得分页json。
三、通过 thymleaf 实现分页 的Controller
1)Controller代码
@GetMapping("/WebDeptDomainPaging")
public ModelAndView WebDeptDomainPaging(
@RequestParam(required = false,defaultValue="1",value="pageNum")Integer pageNum,
@RequestParam(defaultValue="5",value="pageSize")Integer pageSize, ModelAndView view){
//为了程序的严谨性,判断非空:
if(pageNum == null){
pageNum = 1; //设置默认当前页
}
if(pageNum <= 0){
pageNum = 1;
}
if(pageSize == null){
pageSize = 5; //设置默认每页显示的数据数
}
System.out.println("当前页是:"+pageNum+"显示条数是:"+pageSize);
//1.引入分页插件,pageNum是第几页,pageSize是每页显示多少条,默认查询总数count
PageHelper.startPage(pageNum,pageSize);
//2.紧跟的查询就是一个分页查询-必须紧跟.后面的其他查询不会被分页,除非再次调用PageHelper.startPage
try {
List<DeptDomain> deptDomains = deptService.selectAll();
System.out.println("分页数据:"+deptDomains);
//3.使用PageInfo包装查询后的结果,5是连续显示的条数,结果list类型是Page<E>
PageInfo<DeptDomain> pageInfo = new PageInfo<DeptDomain>(deptDomains,pageSize);
//4.使用model/map/modelandview等带回前端
view.addObject("pageInfo", pageInfo);
view.setViewName("/test/TstShowByPage.html");
System.out.println("pageInfo.getPageSize()>>>" + pageInfo.getPageSize());
System.out.println("pageInfo.isHasPreviousPage()>>>" + pageInfo.isHasPreviousPage());
}finally {
PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
}
//5.设置返回的jsp/html等前端页面
return view;
}
-
thymleaf渲染代码: 由于pageInfo.getList() 可以直接返回分页的内容,故直接通过这个方法获取。注意:由于CSDN的这个编辑器无法显示全部代码,故请从以下链接资源中下载完整l的htm代码。
https://download.csdn.net/download/snetlogon20/11826716
<table class="table table-hover" width="200px;" style="test-align: center;"> <caption>部门表格</caption> <thead> <tr> <th width="30%">部门号</th> <th>部门名称</th> <th>部门描述</th> <th>部门电话</th> </tr> </thead> <tbody> <tr th:each="deptDomain : ${pageInfo.getList()}"> <td th:text="${deptDomain.deptId}">0</td> <td th:text="${deptDomain.deptName}">0</td> <td th:text="${deptDomain.desp}">0</td> <td th:text="${deptDomain.phone}">0</td> </tr> </tbody> </table> <!--显示分页信息--> <div class="modal-footer no-margin-top"> <div class="col-md-6"> 当前第 [[${pageInfo.pageNum}]]页,共 [[${pageInfo.pages}]] 页.一共 [[${pageInfo.total}]] 条记录 </div> <ul class="pagination pull-right no-margin"> <li th:if="${pageInfo.hasPreviousPage}"> <a th:href="'/test/DeptDomainPaging?pageNum=1'" target="_blank" rel="external nofollow" >首页</a> </li> <li class="prev" th:if="${pageInfo.hasPreviousPage}"> <a th:href="'/test/usermanage?pageNum='+${pageInfo.prePage}" target="_blank" rel="external nofollow" > <i class="fa fa-leaf"><<</i> </a> </li> <!--遍历条数--> <li th:each="nav:${pageInfo.navigatepageNums}"> <a th:href="'/test/DeptDomainPaging?pageNum='+${nav}" target="_blank" rel="external nofollow" th:text="${nav}" th:if="${nav != pageInfo.pageNum}"></a> <span style="font-weight: bold;background: #6faed9;" th:if="${nav == pageInfo.pageNum}" th:text="${nav}" ></span> </li> <li class="next" th:if="${pageInfo.hasNextPage}"> <a th:href="'/test/DeptDomainPaging?pageNum='+${pageInfo.nextPage}" target="_blank" rel="external nofollow" > <i class="fa fa-leaf">>></i> </a> </li> <li> <a th:href="'/test/DeptDomainPaging?pageNum='+${pageInfo.pages}+'&pageSize=5'" target="_blank" rel="external nofollow" >尾页</a> </li> </ul> </div> <div>当前页号:<span th:text="${pageInfo.pageNum}"></span></div> <div>每页条数:<span th:text="${pageInfo.pageSize}"></span></div> <div>起始行号:<span th:text="${pageInfo.startRow}"></span></div> <div>终止行号:<span th:text="${pageInfo.endRow}"></span></div> <div>总结果数:<span th:text="${pageInfo.total}"></span></div> <div>总页数:<span th:text="${pageInfo.pages}"></span></div> <hr /> <div>是否为第一页:<span th:text="${pageInfo.isFirstPage}"></span></div> <div>是否为最后一页:<span th:text="${pageInfo.isLastPage}"></span></div> <div>是否有前一页:<span th:text="${pageInfo.hasPreviousPage}"></span></div> <div>是否有下一页:<span th:text="${pageInfo.hasNextPage}"></span></div>