問題描述
在使用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>
當然用不到的屬性定義最好不要寫在項目中。