欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot同時集成Mybatis和Mybatis-plus框架

 更新時間:2024年12月06日 11:51:02   作者:zuihongyan518  
在實際開發(fā)中,項目里面一般都是Mybatis和Mybatis-Plus公用,但是公用有版本不兼容的問題,本文主要介紹了Spring Boot項目中同時集成Mybatis和Mybatis-plus,具有一檔的參考價值,感興趣的可以了解一下

1. 背景

Mybatis-plus可以生成CRUD,減少開發(fā)中SQL編寫量,但是某些情況下我們需要多表關聯(lián)查詢,這時候Mybatis可以手寫SQL的優(yōu)勢就體現(xiàn)出來了,在實際開發(fā)中,項目里面一般都是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包,就開始使用這樣是會拋出下面的異常

 這個異常也是常見的,不少后端開發(fā)可能都遇到過,綁定異常,意思是識別不了對應的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開啟

根據(jù)自己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就已經完成了??梢哉禹椖窟M行測試了

        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也能正常查詢數(shù)據(jù)行

到此這篇關于SpringBoot同時集成Mybatis和Mybatis-plus框架的文章就介紹到這了,更多相關SpringBoot同時集成Mybatis和Mybatis-plus內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論