锋盈数科-知识库 Logo
首页
软件开发
计算机基础
Hello Halo
新手必读
关于本知识库
登录 →
锋盈数科-知识库 Logo
首页 软件开发 计算机基础 Hello Halo 新手必读 关于本知识库
登录
  1. 首页
  2. 软件开发
  3. JAVA
  4. SpringBoot项目同时集成Mybatis和Mybatis-plus框架

SpringBoot项目同时集成Mybatis和Mybatis-plus框架

0
  • JAVA
  • 发布于 2024-09-26
  • 12 次阅读
黄健
黄健

1. 背景

Mybatis-plus可以生成CRUD,减少开发中SQL编写量,但是某些情况下我们需要多表关联查询,这时候Mybatis可以手写SQL的优势就体现出来了,在实际开发中,项目里面一般都是Mybatis和Mybatis-Plus公用,但是公用有版本不兼容的问题。下面这篇文章主要介绍如何在项目中同时集成Mybatis和Mybatis-plus。

2. 前期准备

引入相关的jar包

<!--  mybatis依赖    -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.2</version>
</dependency>

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.2</version>
 </dependency>

<!--引入autoconfigure-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-autoconfigure</artifactId>
    <version>2.1.4</version>
</dependency>

3. 不兼容问题

如果是单纯的引入完两个jar包,就开始使用这样是会抛出下面的异常

这个异常也是常见的,不少后端开发可能都遇到过,绑定异常,意思是识别不了对应的mapper.xml

有人说是版本不兼容问题,我把Mybatis(2.1.3)和Mybatis-plus(3.3.0)替换成他们说的版本依然无解。单单只是替换版本是解决不了的

如果你最初项目中是已经集成了Mybatis或者Mybatis-plus;先集成Mybatis的项目再集成Mybatis-plus,这个时候application.yml配置文件需要做出修改,即将原 mybatis 改成 mybatis-plus即可。

原Mybatis配置:

mybatis:
  type-aliases-package: com.xxxx.shortchain.entity
  mapper-locations: classpath*:mappers/**/*.xml
  configuration:
      log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  map-underscore-to-camel-case: true

改成Mybatis-plus配置:

mybatis-plus:
  type-aliases-package: com.xxxx.shortchain.entity
  mapper-locations: classpath*:/mappers/*.xml,classpath*:/mappers/**/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    mapUnderscoreToCamelCase: true  #自动转驼峰 true开启

根据自己xml的实际路径修改。

4. jar包冲突

在集成Mybatis和Mybatis-plus时,也可能会遇到jar包冲突的情况,这时候我们可以排除一些jar包来解决冲突。

4.1 Mybatis中

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
            <!--原Mybatis中需排除下面2个依赖-->
            <exclusions>
                <exclusion>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis-spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

4.2 分页插件pageHelper中

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
            <!--需排除下面包-->
            <exclusions>
                <exclusion>
                    <groupId>org.mybatis.spring.boot</groupId>
                    <artifactId>mybatis-spring-boot-starter</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

至此,SpringBoot同时集成Mybatis和Mybatis-plus就已经完成了。可以正常启动项目进行测试了

        DateTime dateTime = DateUtil.beginOfDay(new Date());
        QueryWrapper<ShortChain> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<ShortChain> lambda = queryWrapper.lambda();
        lambda.ge(ShortChain::getExpireDate, dateTime).eq(ShortChain::getDeleted, 0);
        List<ShortChain> shortChains = shortChainMapper.selectList(queryWrapper);

使用Mybatis-plus也能正常查询数据行

原文链接: https://onlyou.blog.csdn.net//article/details/142377464

标签: #Spring Boot 173 #JAVA 991 #mybatis-plus 7 #Mybatis 37
相关文章

Spring 实现 3 种异步接口 2024-10-18 09:07

大家好,我是苏三~ 如何处理比较耗时的接口? 这题我熟,直接上异步接口,使用 Callable、WebAsyncTask 和 DeferredResult、CompletableFuture等均可实现。 但这些方法有局限性,处理结果仅返回单个值。在某些场景下,如果需要接口异步处理的同时,还持续不断地

重学SpringBoot3-集成Redis(五)之布隆过滤器 2024-10-08 11:24

更多SpringBoot3内容请关注我的专栏:《SpringBoot3》 期待您的点赞👍收藏⭐评论✍ 重学SpringBoot3-集成Redis(五)之布隆过滤器 1. 什么是布隆过滤器? * 基本概念 适用场景 2. 使用 Redis 实现布隆过滤器 * 项目依赖 Redis 配置

SpringBoot整合异步任务执行 2024-10-08 11:24

同步任务: 同步任务是在单线程中按顺序执行,每次只有一个任务在执行,不会引发线程安全和数据一致性等 并发问题 同步任务需要等待任务执行完成后才能执行下一个任务,无法同时处理多个任务,响应慢,影响用 户体验 异步任务: 异步任务是在多线程中同时执行,多个任务可以并发执行,同时处理多个请求,响应快,资源

springboot kafka多数据源,通过配置动态加载发送者和消费者 2024-10-08 11:24

前言 最近做项目,需要支持kafka多数据源,实际上我们也可以通过代码固定写死多套kafka集群逻辑,但是如果需要不修改代码扩展呢,因为kafka本身不处理额外逻辑,只是起到削峰,和数据的传递,那么就需要对架构做一定的设计了。 准备test kafka本身非常容易上手,如果我们需要单元测试,引入ja

SpringBoot 集成 Redis 2024-10-08 11:24

一:SpringBoot 集成 Redis ①Redis是一个 NoSQL(not only)数据库, 常作用缓存 Cache 使用。 ②Redis是一个中间件、是一个独立的服务器;常用的数据类型: string , hash ,set ,zset , list ③通过Redis客户端可以使用多种语

SpringBoot整合QQ邮箱 2024-10-08 11:24

SpringBoot可以通过导入依赖的方式集成多种技术,这当然少不了我们常用的邮箱,现在本章演示SpringBoot整合QQ邮箱发送邮件…. 下面按步骤进行: 1.获取QQ邮箱授权码 1.1 登录QQ邮箱 1.2 开启SMTP服务 找到下图中的SMTP服务区域,如果当前账号未开启的话自己手动开启。

目录

IT 外包服务商

  • 意见投递
  • zyf6619

软件开发应用

主菜单

  • 首页
  • 软件开发
  • 计算机基础
  • Hello Halo
  • 新手必读
  • 关于本知识库
Copyright © 2024 your company All Rights Reserved. Powered by Halo.