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

相关推荐
ss2732 天前
手写MyBatis第85弹:组合模式在SqlNode设计中的精妙应用
mybatis
sniper_fandc2 天前
MybatisPlus和pagehelper分页冲突—关于jsqlparser、pagehelper、MybatisPlus三者的版本兼容问题
mybatis·mybatisplus
上官浩仁3 天前
springboot3 mybatisplus 数据库操作入门与实战
spring boot·mybatis·db
weixin_419658313 天前
MyBatis 进阶
mybatis
祈祷苍天赐我java之术3 天前
Redis 热点数据与冷数据解析
java·redis·mybatis
Roye_ack4 天前
【项目实战 Day9】springboot + vue 苍穹外卖系统(用户端订单模块 + 商家端订单管理模块 完结)
java·vue.js·spring boot·后端·mybatis
人间有清欢4 天前
java数据权限过滤
java·mybatis·权限控制·数据过滤
22jimmy4 天前
MyBatis动态sql
java·开发语言·mybatis
不要再敲了4 天前
SSM框架下的redis使用以及token认证
数据库·spring boot·redis·缓存·mybatis