个人博客地址:https://alexaccele.github.io/
PageHelper是Mybatis的一个很好的分页插件,但要使用它的分页功能需要注意一下几点
1.导入相关包,例如maven导入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.4</version>
</dependency>
2.在mybatis-config.xml中添加插件
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!--分页参数合理化 -->
<property name="reasonable" value="true"/>
</plugin>
</plugins>
3.在Controller的方法中
PageHelper.startPage(1,5);//从第一页开始,每页5条记录
以上代码后面需紧跟查询语句
List<Test> tests = testService.getAllTestsByTypeId(testTypeid);
PageInfo pageInfo = new PageInfo(tests,5);
当一个方法中有多个查询语句时,只有紧跟在PageHelper.starPage()方法后的查询结果才会分页。
缺少以上三步都会导致分页失效