[Solved] Mybatis uses the PageHelper paging plugin error: Could not find method on interface ibatis.executor.Executor named query.

(1) Problem Description
This week at work, the company’s project using mybatis and pagehelper for paging queries, start the project, query data actually reported an error, the error reported as follows.

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)

After thinking for a long time, I didn’t understand what went wrong. Then I inadvertently checked the underlying source code of PageHelper and found that there were errors, as shown below:

Then enter the query () method and jump to the source code of the mybatis framework. It is found that the method in the mybatis framework has only four parameters, while the query () method with six parameters is used in the PageHelper, resulting in an error.

The reason for the problem is that there must be a mismatch between the versions of the mybatis framework and the page helper. I checked that the mybatis version used in the company’s project is 3.0.4 and the PageHelper version is 5.0.0. The solution is simple. Replace the corresponding framework version.

(2) Solution

There are two ways to solve this problem:

Change the version corresponding to mybatis and PageHelper (this is the simplest way). Instead of using the PageHelper plug-in, customize the mybatis paging plug-in

1. Method 1: replace mybatis version

I checked the page helper dependency repository and found that the page helper supports mybatis version 3.2.6 at least.

2. Custom mybatis paging plug-in

The version of mybatis 3.0.4 used by our company. At that time, the technical manager specially told me that if it is a problem with the dependent version, the dependency of PageHelper can be replaced at will, but the dependent version of mybatis cannot be replaced. Ah, I think it’s not just a custom mybatis paging plug-in… I can’t help it. I can only view some articles about custom paging plug-ins, and then write one myself. If necessary, you can check the article [mybatis implementation of custom paging plug-in].

Read More: