今天在使用MyBatis的过程中,想使用一下自定义拦截器,结果出现了一下异常信息:
Caused by: 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(java.sql.Connection) at org.apache.ibatis.plugin.Plugin.getSignatureMap(Plugin.java:87) at org.apache.ibatis.plugin.Plugin.wrap(Plugin.java:44) at A1003.Mybatis的拦截器.MyInterceptor.plugin(MyInterceptor.java:38) at org.apache.ibatis.plugin.InterceptorChain.pluginAll(InterceptorChain.java:31) at org.apache.ibatis.session.Configuration.newExecutor(Configuration.java:581) at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:96) ... 2 more Caused by: java.lang.NoSuchMethodException: org.apache.ibatis.executor.statement.StatementHandler.prepare(java.sql.Connection) at java.lang.Class.getMethod(Class.java:1786) at org.apache.ibatis.plugin.Plugin.getSignatureMap(Plugin.java:84) ... 7 more 复制代码
以下是我的配置版本信息:
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.20</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.4</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.24</version> </dependency> 复制代码
后来在查阅了相关资料之后发现,使用mybatis3.4版本以上,配置拦截器规则应增加Intger参数,如下:
@Intercepts({ @Signature(method = "query", type = Executor.class, args = { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}), @Signature(method = "prepare", type = StatementHandler.class, args = {Connection.class,Integer.class}) })