首页 > 其他 > 详细

mybatis分页插件pagehelper 5.1.2遇到的问题

时间:2018-04-03 18:46:01      阅读:628      评论:0      收藏:0      [点我收藏+]

如果你也在用Mybatis,建议尝试该分页插件,这个一定是最方便使用的分页插件。

该插件目前支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库分页。

我在做项目时在Mybatis配置xml中配置拦截器插件如下:

<plugins>
  <!-- com.github.pagehelper为PageHelper类所在包名 -->
  <plugin interceptor="com.github.pagehelper.PageHelper">
    <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->    
    <property name="dialect" value="mysql"/>
  </plugin>
</plugins>

  运行时却出现错误,报错java.lang.ClassCastException: com.github.pagehelper.PageHelper cannot be cast to org.apache.ibatis.plugin.Interceptor

pageHelper是如何在mybatis中工作呢,是通过mybatis的pulgin实现了Interceptor接口,从而获得要执行的sql语句实现分页技术,而我们的PageHelper5.1.2版本中的这个类,并没有出现implements Interceptor

然而我们的PageHelper类中,并没有拦截器

技术分享图片

因此,我们需要修改我们的mybatis全局配置文件SqlMapConfig.xml如下:

<configuration>  
  <!-- 配置分页插件 PageHelper -->  
   <plugins>  
     <plugin interceptor="com.github.pagehelper.PageInterceptor">   
 <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->    
    <property name="dialect" value="mysql"/
</plugin> </plugins> </configuration>

再次运行时出现错误Cause: com.github.pagehelper.PageException: java.lang.ClassNotFoundException: mysql

这是因为在pagehelper插件4.0.0以后的版本支持自动识别使用的数据库,可以不用配置 <property name="dialect" value="mysql"/>  

再次更改配置文件

 <plugins>
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
        <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
    </plugin>
</plugins>

这次运行时就能够成功

mybatis分页插件pagehelper 5.1.2遇到的问题

原文:https://www.cnblogs.com/zandz/p/8710156.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!