pom.xml添加依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.13</version>
</dependency>
配置yml文件
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
controller层代码
@RequestMapping("list")
public String getList(HttpServletRequest request,String currentPage,WeiUser weiUser){
//判断是否有当前页
if(currentPage == null){
currentPage = "1";
}
//设置分页
PageHelper.startPage(Integer.parseInt(currentPage), 3);
List<WeiUser> list = userService.query(weiUser);
PageInfo<WeiUser> pageInfo = new PageInfo<>(list);
//此时,list中包含了分页信息和所查询的数据
request.setAttribute("list",pageInfo);
//返回 list.html页面
return "list";
}
前端页面使用了thymeleaf,所以一定要注意标签格式,否则跳转页面会报错
<div class="Pages">
<a href="" th:href="@{|list?currentPage=1|}">首页</a>
<span th:if="${list.isFirstPage==false}">
<a href="" th:href="@{|list?currentPage=${list.pageNum-1}|}">上一页</a>
</span>
<span th:each="pa:${list.navigatepageNums}" >
<a href="" th:href="@{|list?currentPage=${pa}|}" th:text="${pa}"></a>
</span>
<span th:if="${list.hasNextPage==true}">
<a href="" th:href="@{|list?currentPage=${list.pageNum+1}|}">下一页</a>
</span>
<a href="" th:href="@{|list?currentPage=${list.pages}|}">末页</a>
当前页:第<span th:text="${list.pageNum}"></span>/<span th:text="${list.pages}"></span>页
</div>