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

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

posted @   程序员自由之路  阅读(7498)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示
主题色彩