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

当然用不到的属性定义最好不要写在项目中。

原文地址:https://www.cnblogs.com/54chensongxia/p/14096995.html