Mybatis使用PageHelper分页插件报错Could not find method on interface ibatis.executor.Executor named query.

在公司项目中遇到使用mybatis和pagehelper进行分页查询时出现报错,原因是mybatis版本(3.0.4)与pagehelper版本(5.0.0)不匹配,pagehelper调用了mybatis不存在的方法。解决方案包括升级mybatis至3.2.6以上版本以匹配pagehelper,或者自定义mybatis分页插件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(1)问题描述

这周工作中,公司的项目使用mybatis和pagehelper进行分页查询,启动工程,查询数据居然报错了,报错内容如下:

Exception in thread "main" org.apache.ibatis.plugin.PluginException: Could not find method on interface org.apache.ibatis.executor.Executor named query. Cause: java.lang.NoSuchMethodException: org.apache.ibatis.executor.Executor.query(org.apache.ibatis.mapping.MappedStatement, java.lang.Object, org.apache.ibatis.session.RowBounds, org.apache.ibatis.session.ResultHandler, org.apache.ibatis.cache.CacheKey, org.apache.ibatis.mapping.BoundSql)

Exception in thread "main" org.apache.ibatis.plugin.PluginException: Could not find method on interface org.apache.ibatis.executor.Executor named query. Cause: java.lang.NoSuchMethodException: org.apache.ibatis.executor.Executor.query(org.apache.ibatis.mapping.MappedStatement, java.lang.Object, org.apache.ibatis.session.RowBounds, org.apache.ibatis.session.ResultHandler, org.apache.ibatis.cache.CacheKey, org.apache.ibatis.mapping.BoundSql)
	at org.apache.ibatis.plugin.Plugin.getSignatureMap(Plugin.java:63)
	at org.apache.ibatis.plugin.Plugin.wrap(Plugin.java:26)
	at com.github.pagehelper.PageInterceptor.plugin(PageInterceptor.java:151)
	at org.apache.ibatis.plugin.InterceptorChain.pluginAll(InterceptorChain.java:12)
	at org.apache.ibatis.session.Configuration.newExecutor(Configuration.java:289)
	at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:78)
	at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession(DefaultSqlSessionFactory.java:32)
Caused by: java.lang.NoSuchMethodException: org.apache.ibatis.executor.Executor.query(org.apache.ibatis.mapping.MappedStatement, java.lang.Object, org.apache.ibatis.session.RowBounds, org.apache.ibatis.session.ResultHandler, org.apache.ibatis.cache.CacheKey, org.apache.ibatis.mapping.BoundSql)
	at java.lang.Class.getMethod(Class.java:1786)
	at org.apache.ibatis.plugin.Plugin.getSignatureMap(Plugin.java:60)

想了半天没想明白哪里出了问题,然后无意间查看到pagehelper的底层源码,发现居然有报错的地方,如下所示:

 然后进入query()方法,跳转到了mybatis框架的源码中,发现mybatis框架中这个方法只有四个参数,而pagehelper中使用的是有六个参数的query()方法,所以导致报错。

 那问题的根据就知道,肯定是mybatis框架和pagehelper的版本不匹配导致的。我查看公司项目使用的mybatis版本是3.0.4的,pagehelper版本是5.0.0的。那解决就简单了,更换对应的框架版本即可。

(2)解决方案

两种方式解决:

  • 更换mybatis和pagehelper对应的版本(这是最简单的方式)
  • 不使用pagehelper插件,自定义mybatis分页插件

1、方式一:更换mybatis版本

我查看pagehelper依赖仓库, 发现pagehelper最低支持mybatis 3.2.6版本的。

 2、自定义mybatis分页插件

我们公司使用的那个mybatis3.0.4版本,当时技术经理还特意告诉我,如果是依赖版本的问题,那么pagehelper的依赖可以随意更换,但是mybatis依赖版本不能更换。啊呀,我一想那不是只能自定义mybatis分页插件啦。。。。没办法啦,只能查看一些自定义分页插件的文章,然后自己写一个了。如果有需要的可以查看【Mybatis实现自定义分页插件】文章。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.小朱同学

随心赞赏,助力前行

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值