SpringBoot同時集成Mybatis和Mybatis-plus框架
1. 背景
Mybatis-plus可以生成CRUD,減少開發(fā)中SQL編寫量,但是某些情況下我們需要多表關(guān)聯(lián)查詢,這時候Mybatis可以手寫SQL的優(yōu)勢就體現(xiàn)出來了,在實際開發(fā)中,項目里面一般都是Mybatis和Mybatis-Plus公用,但是公用有版本不兼容的問題。下面這篇文章主要介紹如何在項目中同時集成Mybatis和Mybatis-plus。
2. 前期準(zhǔn)備
引入相關(guān)的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ā)可能都遇到過,綁定異常,意思是識別不了對應(yīng)的mapper.xml
有人說是版本不兼容問題,我把Mybatis(2.1.3)和Mybatis-plus(3.3.0)替換成他們說的版本依然無解。單單只是替換版本是解決不了的
如果你最初項目中是已經(jīng)集成了Mybatis或者M(jìn)ybatis-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 #自動轉(zhuǎn)駝峰 true開啟根據(jù)自己xml的實際路徑修改。
4. jar包沖突
在集成Mybatis和Mybatis-plus時,也可能會遇到j(luò)ar包沖突的情況,這時候我們可以排除一些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就已經(jīng)完成了??梢哉禹椖窟M(jìn)行測試了

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ù)行

到此這篇關(guān)于SpringBoot同時集成Mybatis和Mybatis-plus框架的文章就介紹到這了,更多相關(guān)SpringBoot同時集成Mybatis和Mybatis-plus內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot實現(xiàn)日志文件分隔(根據(jù)日期和文件大小)
文章簡要介紹了如何在Spring Boot項目中配置日志文件路徑,包括在resource目錄下創(chuàng)建logback-spring.xml文件,并在yml配置文件中設(shè)置logging.file.path屬性來指定日志文件的輸出路徑2026-01-01
Spring Boot 將yyyy-MM-dd格式的文本字符串直接轉(zhuǎn)換為LocalDateTime出現(xiàn)的問題
這篇文章主要介紹了Spring Boot 將yyyy-MM-dd格式的文本字符串直接轉(zhuǎn)換為LocalDateTime出現(xiàn)的問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Java在Word中插入上標(biāo)和下標(biāo)的實現(xiàn)方法
Java?Bluetooth?藍(lán)牙通訊?BlueCove?掃描附近的藍(lán)牙設(shè)備(測試代碼)

