关于Mybatis分页,myBatis分页,常用的有两种方法,方法如下:
1.AOP 使用myBatis的@interceptor 可以切入类的方法中,获取sql进行拼接
2.分页插件,PageHelper
下面是关于PageHelper的使用。配置文件如下:
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库, 4.0.0以后版本支持自动识别数据库-->
<!-- <property name="dialect" value="Oracle"/>-->
</plugin>
</plugins>
PageHelper.startPage(1, 10);
自动识别数据库其实就是根据配置的jar包进行匹配的。
-
- PageHelper.startPage方法重要提示
只有紧跟在PageHelper.startPage方法后的第一个Mybatis的查询(Select方法)方法会被分页。
-
- 分页插件不支持带有for update语句的分页
对于带有for update的sql,会抛出运行时异常,对于这样的sql建议手动分页,毕竟这样的sql需要重视。
-
- 分页插件不支持关联结果查询
原因以及解决方法可以看这里:
http://my.oschina.net/flags/blog/274000
分支插件不支持关联结果查询,但是支持关联嵌套查询。只会对主sql进行分页,嵌套的sql不会被分页。
- 相关链接
对应于oschub的项目地址:http://git.oschina.net/free/Mybatis_PageHelper
对应于github的项目地址:https://github.com/pagehelper/Mybatis-PageHelper
Mybatis-Sample(分页插件测试项目):http://git.oschina.net/free/Mybatis-Sample
Mybatis项目:https://github.com/mybatis/mybatis-3