SpringBoot同時(shí)集成Mybatis和Mybatis-plus框架
1. 背景
Mybatis-plus可以生成CRUD,減少開發(fā)中SQL編寫量,但是某些情況下我們需要多表關(guān)聯(lián)查詢,這時(shí)候Mybatis可以手寫SQL的優(yōu)勢(shì)就體現(xiàn)出來(lái)了,在實(shí)際開發(fā)中,項(xiàng)目里面一般都是Mybatis和Mybatis-Plus公用,但是公用有版本不兼容的問(wèn)題。下面這篇文章主要介紹如何在項(xiàng)目中同時(shí)集成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. 不兼容問(wèn)題
如果是單純的引入完兩個(gè)jar包,就開始使用這樣是會(huì)拋出下面的異常

這個(gè)異常也是常見(jiàn)的,不少后端開發(fā)可能都遇到過(guò),綁定異常,意思是識(shí)別不了對(duì)應(yīng)的mapper.xml
有人說(shuō)是版本不兼容問(wèn)題,我把Mybatis(2.1.3)和Mybatis-plus(3.3.0)替換成他們說(shuō)的版本依然無(wú)解。單單只是替換版本是解決不了的
如果你最初項(xiàng)目中是已經(jīng)集成了Mybatis或者M(jìn)ybatis-plus;先集成Mybatis的項(xiàng)目再集成Mybatis-plus,這個(gè)時(shí)候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 #自動(dòng)轉(zhuǎn)駝峰 true開啟根據(jù)自己xml的實(shí)際路徑修改。
4. jar包沖突
在集成Mybatis和Mybatis-plus時(shí),也可能會(huì)遇到j(luò)ar包沖突的情況,這時(shí)候我們可以排除一些jar包來(lái)解決沖突。
4.1 Mybatis中
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
<!--原Mybatis中需排除下面2個(gè)依賴-->
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
</exclusions>
</dependency>4.2 分頁(yè)插件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同時(shí)集成Mybatis和Mybatis-plus就已經(jīng)完成了??梢哉?dòng)項(xiàng)目進(jìn)行測(cè)試了

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同時(shí)集成Mybatis和Mybatis-plus框架的文章就介紹到這了,更多相關(guān)SpringBoot同時(shí)集成Mybatis和Mybatis-plus內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
實(shí)例詳解MyBatis-plus自動(dòng)填充功能
每次對(duì)數(shù)據(jù)進(jìn)行新增、刪除、修改時(shí)都需要對(duì)這些字段進(jìn)行設(shè)置,雖然新增時(shí)間和修改時(shí)間可以使用數(shù)據(jù)庫(kù)的時(shí)間,但是新增人和修改人就不能使用這樣的功能,下面小編給大家介紹下MyBatis-plus自動(dòng)填充功能的實(shí)例代碼,感興趣的朋友一起看看吧2022-01-01
SpringBoot實(shí)現(xiàn)日志文件分隔(根據(jù)日期和文件大小)
文章簡(jiǎn)要介紹了如何在Spring Boot項(xiàng)目中配置日志文件路徑,包括在resource目錄下創(chuàng)建logback-spring.xml文件,并在yml配置文件中設(shè)置logging.file.path屬性來(lái)指定日志文件的輸出路徑2026-01-01
解決netty中spring對(duì)象注入失敗的問(wèn)題
這篇文章主要介紹了解決netty中spring對(duì)象注入失敗的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Spring Boot 將yyyy-MM-dd格式的文本字符串直接轉(zhuǎn)換為L(zhǎng)ocalDateTime出現(xiàn)的問(wèn)題
這篇文章主要介紹了Spring Boot 將yyyy-MM-dd格式的文本字符串直接轉(zhuǎn)換為L(zhǎng)ocalDateTime出現(xiàn)的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Java在Word中插入上標(biāo)和下標(biāo)的實(shí)現(xiàn)方法
Java?Bluetooth?藍(lán)牙通訊?BlueCove?掃描附近的藍(lán)牙設(shè)備(測(cè)試代碼)

