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

相关推荐
cike_y8 小时前
Mybatis之作用域(Scope)和生命周期-解决属性名和字段名不一致的问题&ResultMap结果集映射
java·开发语言·数据库·tomcat·mybatis
柒.梧.10 小时前
MyBatis一对一关联查询深度解析:大实体类、SQL99联表、分布式查询实践
分布式·mybatis
Coder_Boy_12 小时前
SpringAI与LangChain4j的智能应用-(理论篇)
人工智能·spring·mybatis·springai·langchain4j
zhoupenghui16812 小时前
项目访问接口时报“MISCONF Redis is configured to save RDB snapshots, ...“错误的解决方案
数据库·redis·mybatis
cike_y20 小时前
Mybatis之解析配置优化
java·开发语言·tomcat·mybatis·安全开发
勇往直前plus1 天前
MyBatis/MyBatis-Plus类型转换器深度解析:从基础原理到自定义实践
数据库·oracle·mybatis
古城小栈1 天前
Spring Boot 数据持久化:MyBatis-Plus 分库分表实战指南
spring boot·后端·mybatis
无名-CODING1 天前
SQL 注入指南
sql·mybatis
shuair1 天前
redis缓存双写
redis·缓存·mybatis
前端程序猿之路1 天前
AI大模型应用之-RAG 全流程
人工智能·python·gpt·语言模型·aigc·mybatis·ai编程