使用Mybatis-Plus的一個坑

問題描述

在使用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>

當然用不到的屬性定義最好不要寫在項目中。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章