使用Mybatis-Plus的一個坑
阿新 • • 發佈:2020-12-07
問題描述
在使用Mybatis-Plus的過程中,突然發生這樣一個錯誤。
*************************** 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.MybatisMapperAnnotationBuilder.getLanguageDriver(MybatisMapperAnnotationBuilder.java:386) The following method did not exist: org.apache.ibatis.session.Configuration.getLanguageDriver(Ljava/lang/Class;)Lorg/apache/ibatis/scripting/LanguageDriver; The method's class, org.apache.ibatis.session.Configuration, is available from the following locations: jar:file:/D:/software/maven/Repository/org/mybatis/mybatis/3.4.4/mybatis-3.4.4.jar!/org/apache/ibatis/session/Configuration.class It was loaded from the following location: file:/D:/software/maven/Repository/org/mybatis/mybatis/3.4.4/mybatis-3.4.4.jar
上面的錯誤很明顯,是因為com.baomidou.mybatisplus.core.MybatisMapperAnnotationBuilder.getLanguageDriver
這個方法不存在。
MybatisMapperAnnotationBuilder這個類繼承了MyBatis的Configuration類,理論上不應有有問題的。
初步懷疑是因為Jar包衝突導致的。但是在程式碼中搜索了下,整個專案中就mybatis-plus引用了mybatis的依賴,所以也沒有Jar包衝突的問題。
那麼只能是Jar包版本的問題了,是不是mybatis-plus依賴了錯誤的mybatis版本?
開啟mybatis-plus的依賴配置:
<properties> <mybatis.version>3.5.5</mybatis.version> <spring.version>5.2.6.RELEASE</spring.version> ... 省略 </properties> <dependencies> <!-- Compile dependencies --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> <scope>provided</scope> </dependency> ... 省略 </dependencies>
我發現mybatis-plus依賴的是3.5.5版本的mybatis。但是奇怪怪的是為什麼專案中的mybatis版本是3.4.4呢?隱約感覺問題就出在這裡。
繼續找問題,我發現我在我專案的父pom中也定義了一個 <mybatis.version>3.4.4</mybatis.version>。
我專案中定義的屬性將mybatis-plus中定義的屬性“衝”掉了,所以才會導致引入版本不對的版本。將專案中父pom的屬性定義去掉就OK了。
一些思考
我們在maven中定義屬性時,最好將屬性的名字定義的不要太通用,免得和其他屬性重名,導致意想不到的問題。比如定義屬性時可以加個專案名。
<pname.mybatis.version>xxx</pname.mybatis.version>
當然用不到的屬性定義最好不要寫在專案中。