如何解决Mybatis-plus与Mybatis不兼容的问题
在使用Mybatis-plus和Mybatis时,开发者可能会遇到版本不兼容的问题,这会导致应用程序无法正确运行。这类问题通常表现为方法不存在的错误,例如本文案例中的org.apache.ibatis.session.Configuration.setArgNameBasedConstructorAutoMapping(Z)V方法不存在。了解并解决这些问题对于保持项目的稳定性和可用性至关重要。
报错信息如下
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2024-07-03 12:15:05.977 ERROR 20232 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: An attempt was made to call a method that does not exist. The attempt was made from the following location: com.baomidou.mybatisplus.core.MybatisXMLScriptBuilder$ForEachHandler.handleNode(MybatisXMLScriptBuilder.java:230) The following method did not exist: org.apache.ibatis.scripting.xmltags.ForEachSqlNode.<init>(Lorg/apache/ibatis/session/Configuration;Lorg/apache/ibatis/scripting/xmltags/SqlNode;Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V The calling method's class, com.baomidou.mybatisplus.core.MybatisXMLScriptBuilder$ForEachHandler, was loaded from the following location: jar:file:/D:/Maven_work/repository/com/baomidou/mybatis-plus-core/3.5.7/mybatis-plus-core-3.5.7.jar!/com/baomidou/mybatisplus/core/MybatisXMLScriptBuilder$ForEachHandler.class The called method's class, org.apache.ibatis.scripting.xmltags.ForEachSqlNode, is available from the following locations: jar:file:/D:/Maven_work/repository/org/mybatis/mybatis/3.5.7/mybatis-3.5.7.jar!/org/apache/ibatis/scripting/xmltags/ForEachSqlNode.class The called method's class hierarchy was loaded from the following locations: org.apache.ibatis.scripting.xmltags.ForEachSqlNode: file:/D:/Maven_work/repository/org/mybatis/mybatis/3.5.7/mybatis-3.5.7.jar Action: Correct the classpath of your application so that it contains compatible versions of the classes com.baomidou.mybatisplus.core.MybatisXMLScriptBuilder$ForEachHandler and org.apache.ibatis.scripting.xmltags.ForEachSqlNode
这表明MybatisXMLConfigBuilder
尝试调用一个在org.apache.ibatis.session.Configuration
中不存在的方法。
解决这个问题有两种主要方法:降低Mybatis-plus的版本或升级Mybatis的版本。
降级Mybatis-plus版本
-
将Mybatis-plus的版本从3.5.2降级到3.5.1。
-
修改pom.xml文件中的依赖项,如下所示:
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-core</artifactId> <version>3.5.1</version> </dependency>
-
重新构建项目以确保更改生效
小结
根据项目需求和其他依赖关系的兼容性,开发者可以选择降级Mybatis-plus或升级Mybatis。通常,保持依赖项的最新版本有助于利用最新的功能和修复。