MyBatis可引入第三方插件对相关功能进行扩展,本文只涉及MyBatis整合PageHelper步骤及其使用,具体Mybatis分页插件原理及源码分析部分,可参考Mybatis插件原理分析及自定义一个插件
1.引入PageHelper依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>3.7.5</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>0.9.1</version>
</dependency>
2.在SqlMapConfig.xml中配置PageHelper插件信息
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 指定方言-->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
3.执行查询之前传入当前页,页数开始分页
查询前设置当前页、页数:PageHelper.startPage(1,1);// 参数依次为当前页、页数
4.查询到结果集resultList后,传入resultList对象,可获取分页信息
PageInfo pageInfo = new PageInfo(resultList);