优点:无需你自己去封装以及关心sql分页等问题,使用很方便,前端取数据也很方便。
1.添加依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
2.编写配置文件 (yml 形式)
#分页配置
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
2.编写配置文件 (propreties形式)
#pagehelper分页插件配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
3.编写Controller 层
两大核心代码:
PageHelper.startPage(pageNum , 3);
PageInfo<Detail> pageInfo = new PageInfo<>(list);
具体如下:
然后需要转发到页面需要的数据:
PageInfo类自动封装很多数据,如下有:
//打印下属性
log.info("当前页数:"+pageInfo.getPageNum());
log.info("每页数量:"+pageInfo.getPageSize());
log.info("当前页面数量:"+pageInfo.getSize());
log.info("总记录数:"+pageInfo.getTotal());
log.info("总页数:"+pageInfo.getPages());
4.编写页面展示层
<p>
<a th:href="@{/pageList(pageNum=1,sortId=${sortId})}">首页</a>
<a th:href="@{/pageList(pageNum=${pageNum -1},sortId=${sortId})}" th:if="${pageNum>1}">上一页</a>
<a th:href="@{/pageList(pageNum=${pageNum +1},sortId=${sortId})}" th:if="${pageNum<pageTotal}">下一页</a>
<a th:href="@{/pageList(pageNum=${pageTotal},sortId=${sortId})}">尾页</a>
第<span th:text="${pageNum}"></span> 页/
共<span th:text="${pageTotal}"></span>页
</p>