栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

(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实现自定义分页插件】文章。

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/361946.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号