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 的依赖也以及被引入了,不需要单独引入或配置。

相关推荐
阿里云云原生2 小时前
AI Agent 上线容易稳定难?阿里云 AgentLoop 推出“经验自进化”闭环治理方案
人工智能·阿里云·mybatis·agentscope
CodeStats16 小时前
【Spring事务】Spring事务注解 @Transactional 完整体系:从 MySQL 隔离级别到 MyBatis 原理详解
java·spring·mybatis·事务·transactional
Nuanyt1 天前
SSM 学习记录 第二部分 Spring整合Mybatis&Junit AOP核心概念 Spring事务管理 SpringMVC 请求与响应 Rest风格
java·spring·junit·mybatis·restful
米码收割机1 天前
【SSM】Spring MVC_MyBatis SSM商城系统(源码+论文)【独一无二】
spring·mvc·mybatis
Devin~Y2 天前
互联网大厂 Java 面试实录:Spring Boot、MyBatis、Redis、Kafka、Spring Security、RAG 与 MCP 全链路问答
java·redis·kafka·mybatis·spring security·spring mvc·sprint boot
VX_bysjlw9853 天前
基于微信小程序的宠物用品商城系统-后端74346-计算机毕设原创(免费领源码+带部署教程)
java·redis·微信小程序·eclipse·mybatis·idea·微信开发者工具
hexu_blog3 天前
springboot3集成shardingsphere4.0 分表分库
java·spring boot·mybatis
乐观的Terry3 天前
9、发布系统-Webhook自动发布
java·spring boot·spring·spring cloud·mybatis
聆听。。花开雨落4 天前
mybatis的typeHandler 作用
数据库·mybatis
天若有情6736 天前
SpringBoot4 + MyBatis 前后端分离实战|从零实现坏习惯管理系统,支持局域网手机访问CRUD
智能手机·mybatis