您的位置:首页 > 其它

Mybatis物理分页插件PageHelper 5.0

2018-01-03 22:05 495 查看
昨天在Maven管理的项目中集成PageHelper的时候总是没有成功。我采用的是Spring的集成方式。 



因为我使用的是PageHelper5.0,如果按照这样的配置的话,Log就会报出如下异常: 

Error creating bean with name ‘sqlSessionFactory’ defined in class path resource 

[META-INF/spring/applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: 

Failed to convert property value of type ‘java.lang.Object[]’ to required type ‘org.apache.ibatis.plugin.Interceptor[]’ for property ‘plugins’; 

nested exception is java.lang.IllegalStateException: 

Cannot convert value of type ‘com.github.pagehelper.PageHelper’ to required type ‘org.apache.ibatis.plugin.Interceptor’ for property ‘plugins[0]’: no matching editors or conversion strategy found 

大致意思就是没办法转型成org.apache.ibatis.plugin.Interceptor,在这里我就不明白了,因为网上的教程基本都是这么配置的。 

然后我就换一种方式,采用mybatis集成方式进行配置,结果还是出错。内容描述基本差不多。到这里我就很纳闷了,我还特意打开源码,发现确实有PageHelper这个包啊。 

然后我打开org.mybatis.spring.SqlSessionFactoryBean找到plugins的具体传参看见
if (!isEmpty(this.plugins)) {
for (Interceptor plugin : this.plugins) {
configuration.addInterceptor(plugin);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Registered plugin: '" + plugin + "'");
}
}
}
1
2
3
4
5
6
7
8
9

这里我发现plugin是Interceptor,没办法,只能在github上找最新的PageHelper的文档了。 

突然发现这儿坑还挺多的。下面我就把PageHelper 5.0与以前版本不一样的地方给贴出来。
使用 QueryInterceptor 规范 处理分页逻辑 新的分页插件拦截器为 

com.github.pagehelper.PageInterceptor 新的 PageHelper 是一个特殊的 Dialect
实现类,以更友好的方式实现了以前的功能 新的分页插件仅有 dialect 一个参数,默认的 dialect 实现类为 PageHelper
PageHelper 仍然支持以前提供的参数,在最新的使用文档中已经全部更新 PageHelper 的 helperDialect 参数和以前的 dialect 功能一样,具体可以看文档的参数说明
增加了基于纯 RowBounds 和 PageRowBounds 的分页实现,在com.github.pagehelper.dialect.rowbounds 包中,这是用于作为 dialect 参数示例的实现,后面会补充更详细的文档 去掉了不适合出现在分页插件中的 orderby功能,以后会提供单独的排序插件
去掉了PageHelper 中不常用的方法新的文档,更新历来更新日志中提到的重要内容,提供英文版本文档 解决 bug 将 Db2RowDialect 改为Db2RowBoundsDialect 所有分页插件抛出的异常改为 PageException

然后他给的Demo中配置是这样的。
<bean id="sqlSessionFactory"         class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="plugins">
<array>
```ruby
<bean class="com.github.pagehelper.PageInterceptor">
```
<property name="properties">
<!--使用下面的方式配置参数,一行配置一个 -->
<value>
params=value1
</value>
</property>
</bean>
</array>
</property>
</bean>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

还有一个小坑需要注意的是,在5.0中使用了helperDialect代替了原来的dialect属性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: