Search code examples
javamybatisinterceptoribatis

Could not find method on interface org.apache.ibatis.executor.statement.StatementHandler named prepare


I used Mybatis to create an Interceptor, but when I run it the console displayed there is no "prepare" named method in StatementHandler class. Is there anything else I need to do?

This is the annotation:

@Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class, Integer.class }) })
org.apache.ibatis.exceptions.PersistenceException: 
### Error opening session.  Cause: org.apache.ibatis.plugin.PluginException: Could not find method on interface org.apache.ibatis.executor.statement.StatementHandler named prepare. Cause: java.lang.NoSuchMethodException: org.apache.ibatis.executor.statement.StatementHandler.prepare(com.mysql.jdbc.Connection, java.lang.Integer)
### Cause: org.apache.ibatis.plugin.PluginException: Could not find method on interface org.apache.ibatis.executor.statement.StatementHandler named prepare. Cause: java.lang.NoSuchMethodException: org.apache.ibatis.executor.statement.StatementHandler.prepare(com.mysql.jdbc.Connection, java.lang.Integer)
org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)   org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:100)
org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession(DefaultSqlSessionFactory.java:47)
    com.fj.db.DBAccess.getSqlSession(DBAccess.java:30)
    com.fj.dao.MessageDao.count(MessageDao.java:121)
    com.fj.service.QueryService.queryMessageList(QueryService.java:34)
    com.fj.servlet.ListServlet.doGet(ListServlet.java:46)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
org.apache.ibatis.plugin.PluginException: Could not find method on interface org.apache.ibatis.executor.statement.StatementHandler named prepare. Cause: java.lang.NoSuchMethodException: org.apache.ibatis.executor.statement.StatementHandler.prepare(com.mysql.jdbc.Connection, java.lang.Integer)
    org.apache.ibatis.plugin.Plugin.getSignatureMap(Plugin.java:87)
    org.apache.ibatis.plugin.Plugin.wrap(Plugin.java:44)
    com.fj.interceptor.PageInterceptor.plugin(PageInterceptor.java:121)
org.apache.ibatis.plugin.InterceptorChain.pluginAll(InterceptorChain.java:31)
org.apache.ibatis.session.Configuration.newExecutor(Configuration.java:553)
org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:96)
org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession(DefaultSqlSessionFactory.java:47)
    com.fj.db.DBAccess.getSqlSession(DBAccess.java:30)
    com.fj.dao.MessageDao.count(MessageDao.java:121)
    com.fj.service.QueryService.queryMessageList(QueryService.java:34)
    com.fj.servlet.ListServlet.doGet(ListServlet.java:46)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.NoSuchMethodException: org.apache.ibatis.executor.statement.StatementHandler.prepare(com.mysql.jdbc.Connection, java.lang.Integer)
    java.lang.Class.getMethod(Class.java:1786)
    org.apache.ibatis.plugin.Plugin.getSignatureMap(Plugin.java:84)
    org.apache.ibatis.plugin.Plugin.wrap(Plugin.java:44)
    com.fj.interceptor.PageInterceptor.plugin(PageInterceptor.java:121)
org.apache.ibatis.plugin.InterceptorChain.pluginAll(InterceptorChain.java:31)
org.apache.ibatis.session.Configuration.newExecutor(Configuration.java:553)
org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:96) org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession(DefaultSqlSessionFactory.java:47)
    com.fj.db.DBAccess.getSqlSession(DBAccess.java:30)
    com.fj.dao.MessageDao.count(MessageDao.java:121)
    com.fj.service.QueryService.queryMessageList(QueryService.java:34)
    com.fj.servlet.ListServlet.doGet(ListServlet.java:46)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Solution

  • The definition of your interceptor uses wrong Connection class. You are using com.mysql.jdbc.Connection but you should use java.sql.Connection. This line in the log gives you exact reason of the problem

     NoSuchMethodException: org.apache.ibatis.executor.statement.StatementHandler.prepare(com.mysql.jdbc.Connection, java.lang.Integer)
    

    The method in StatementHandler is defined as

    java.sql.Statement prepare(java.sql.Connection connection)