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. 前期準備
引入相關(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ā)可能都遇到過,綁定異常,意思是識別不了對應的mapper.xml
有人說是版本不兼容問題,我把Mybatis(2.1.3)和Mybatis-plus(3.3.0)替換成他們說的版本依然無解。單單只是替換版本是解決不了的
如果你最初項目中是已經(jīng)集成了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 #自動轉(zhuǎn)駝峰 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就已經(jīng)完成了??梢哉禹椖窟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ù)行

到此這篇關(guān)于SpringBoot同時集成Mybatis和Mybatis-plus框架的文章就介紹到這了,更多相關(guān)SpringBoot同時集成Mybatis和Mybatis-plus內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java JVM字節(jié)碼指令集總結(jié)整理與介紹
本節(jié)將會著重介紹一下JVM中的指令集、Java是如何跨平臺的、JVM指令集參考手冊等內(nèi)容。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09
spring-boot react如何一步一步實現(xiàn)增刪改查
這篇文章主要介紹了spring-boot react如何一步一步實現(xiàn)增刪改查,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11
詳解java開啟異步線程的幾種方法(@Async,AsyncManager,線程池)
在springboot框架中,可以使用注解簡單實現(xiàn)線程的操作,還有AsyncManager的方式,如果需要復雜的線程操作,可以使用線程池實現(xiàn),本文通過實例代碼介紹java開啟異步線程的幾種方法(@Async,AsyncManager,線程池),感興趣的朋友一起看看吧2023-09-09
IntelliJ IDEA Run時報“無效的源發(fā)行版:16“錯誤問題及解決方法
這篇文章主要介紹了IntelliJ IDEA Run時報“無效的源發(fā)行版:16“錯誤問題及解決方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05
簡單了解SpringMVC緩存對靜態(tài)資源有什么影響
這篇文章主要介紹了簡單了解SpringMVC緩存對靜態(tài)資源有什么影響,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-09-09

