使用MyBatis分页插件PageHelper遇到的问题

本文详细讲述了在使用PageHelper进行分页查询时遇到的版本问题和ClassCastException,重点介绍了如何解决版本冲突、插件类变更及配置调整,确保Mybatis与PageHelper的兼容性。
摘要由CSDN通过智能技术生成

最近在使用mybatis的PageHelper进行分页查询时,遇到了不少问题,总结一下希望能帮到别人。

1、版本错误
报错如下

java.lang.NoSuchMethodError: 'java.util.List net.sf.jsqlparser.statement.select.PlainSelect.getGroupByColumnReferences()'

这个错误是因为pagehelper的版本低导致的

解决办法:pagehelper尽量引入5.0以上的依赖。

<!--mybatis分页插件-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.jsqlparser</groupId>
            <artifactId>jsqlparser</artifactId>
            <version>4.0</version>
        </dependency>

2、
更换了版本之后问题还没有解决,又有报错如下:

org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in SQL Mapper Configuration
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.ClassCastException: class com.github.pagehelper.PageHelper cannot be cast to class org.apache.ibatis.plugin.Interceptor (com.github.pagehelper.PageHelper and org.apache.ibatis.plugin.Interceptor are in unnamed module of loader 'app')

错误提示讲,是PageHelper出现了问题。通过这篇回答得到了解决:
https://blog.csdn.net/sinat_34104446/article/details/92679046

是因为pagehelper5.0版本以上在mybatis中指定的插件类不是PageHelper,而是PageInterceptor。

解决办法:
更改mybatis配置文件,将插件类com.github.pagehelper.PageHelper换为com.github.pagehelper.PageInterceptor

同时,因为PageHelper4.0以后的版本可以自动检测数据库类型,不需要再配置dialect,所以把配置dialect的标签也删掉(这个标签不删掉也会报错从而无法运行

<plugins>
	<plugin interceptor="com.github.pagehelper.PageHelper">
	    <property name="dialect" value="mysql"/>
	</plugin>
</plugins>
-------    变为如下    -------
<plugins>
	<plugin interceptor="com.github.pagehelper.PageInterceptor">	  
	</plugin>
</plugins>

至此,问题解决

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值