mybatis 和 mybatisPlus 兼容性问题

项目采用的是 mybatis, 后续引入了 mybatisPlus,用 mybatisX 创建的四个类一直报错,提示找不到符号,意识到 mybatis 和 mybatisPlus 的兼容性问题,通过修改配置 两者的配置如下

复制代码
#配置mybatis配置
mybatis:
  type-aliases-package: top.year21.computerstore.entity
  mapper-locations: classpath:mybatis/mapper/*.xml
  configuration:
    #开启在mybatis处理过程中打印出对应的sql语句功能
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    #开启数据库字段自动转换为驼峰命名
    map-underscore-to-camel-case: true

# 配置 mybatisPlus 配置
mybatis-plus:
  type-aliases-package: top.year21.computerstore.plusEntity
  mapper-locations: classpath:mapper/*.xml

启动时又报错如下:

查到可能是 MyBatis 和 MyBatisPlus 版本兼容性问题

原配置:

其中 3.5.9 的版本可能还需要新增依赖:

所以我将 mybatisPlus 的依赖版本降为了 3.5.3

成功启动项目。

调用接口方法时报错:

查到是 xml 文件识别的问题,在网上查了很久但是一直不能解决,注意到 在执行到 IUserServiceImpl 中的第 91行时,

会调用到 mapper 提供的方法,根据错误递归栈发现后续直接走的 mybatisplus,没有走 mybatis,而我的 mybatisPlus 配置的 classpath 和 type-aliases-package 并没有包含UserMapper 对应的 xml 文件

后续通过修改 mybatisplus 的配置,并将 plusEntity 中的 TProductCategory 和 mapper 中的 TProductCategoryMapper 分别移动到 entity 和 mybatis/mapper 中解决了问题。

复制代码
#配置mybatis配置
#mybatis:
#  type-aliases-package: top.year21.computerstore.entity
#  mapper-locations: classpath:mybatis/mapper/*Mapper.xml
#  configuration:
#    #开启在mybatis处理过程中打印出对应的sql语句功能
#    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#    #开启数据库字段自动转换为驼峰命名
#    map-underscore-to-camel-case: true

# 配置 mybatisPlus 配置
mybatis-plus:
  type-aliases-package: top.year21.computerstore.entity
  mapper-locations: classpath:mybatis/mapper/*Mapper.xml        # MyBatis-Plus 的 XML Mapper 路径(可选,如果使用 XML)
  configuration:
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

总结一下:当有 MyBatisPlus 时,会优先选用 MyBatisPlus ,而不是 MyBatis,所以 MyBatisPlus 的配置需要包含所有 xml 和 entiy,避免识别不到的错误。实际上,当引入 MyBatisPlus (MyBatis 的增强)之后,MyBatis 的依赖也以及被引入了,不需要单独引入或配置。

相关推荐
Java小白程序员9 小时前
MyBatis基础到高级实践:全方位指南(中)
数据库·mybatis
山楂树下懒猴子11 小时前
ChatAI项目-ChatGPT-SDK组件工程
人工智能·chatgpt·junit·https·log4j·intellij-idea·mybatis
Mr_hwt_12320 小时前
基于mybatis-plus动态数据源实现mysql集群读写分离和从库负载均衡教程(详细案例)
数据库·spring boot·mysql·mybatis·mysql集群
Z_z在努力1 天前
【杂类】Spring 自动装配原理
java·spring·mybatis
little_xianzhong1 天前
关于对逾期提醒的定时任务~改进完善
java·数据库·spring boot·spring·mybatis
MadPrinter2 天前
SpringBoot学习日记 Day11:博客系统核心功能深度开发
java·spring boot·后端·学习·spring·mybatis
奔跑吧邓邓子2 天前
【Java实战㉟】Spring Boot与MyBatis:数据库交互的进阶之旅
java·spring boot·实战·mybatis·数据库交互
lunzi_fly2 天前
【源码解读之 Mybatis】【基础篇】-- 第1篇:MyBatis 整体架构设计
java·mybatis
摸鱼仙人~2 天前
深入理解 MyBatis-Plus 的 `BaseMapper`
java·开发语言·mybatis
隔壁阿布都2 天前
spring boot + mybatis 使用线程池异步修改数据库数据
数据库·spring boot·mybatis