SpringBoot同時(shí)集成Mybatis和Mybatis-plus框架
1. 背景
Mybatis-plus可以生成CRUD,減少開發(fā)中SQL編寫量,但是某些情況下我們需要多表關(guān)聯(lián)查詢,這時(shí)候Mybatis可以手寫SQL的優(yōu)勢(shì)就體現(xiàn)出來了,在實(shí)際開發(fā)中,項(xiàng)目里面一般都是Mybatis和Mybatis-Plus公用,但是公用有版本不兼容的問題。下面這篇文章主要介紹如何在項(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. 不兼容問題
如果是單純的引入完兩個(gè)jar包,就開始使用這樣是會(huì)拋出下面的異常
這個(gè)異常也是常見的,不少后端開發(fā)可能都遇到過,綁定異常,意思是識(shí)別不了對(duì)應(yīng)的mapper.xml
有人說是版本不兼容問題,我把Mybatis(2.1.3)和Mybatis-plus(3.3.0)替換成他們說的版本依然無解。單單只是替換版本是解決不了的
如果你最初項(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包來解決沖突。
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 分頁插件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)文章
Java JVM字節(jié)碼指令集總結(jié)整理與介紹
本節(jié)將會(huì)著重介紹一下JVM中的指令集、Java是如何跨平臺(tái)的、JVM指令集參考手冊(cè)等內(nèi)容。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09spring-boot react如何一步一步實(shí)現(xiàn)增刪改查
這篇文章主要介紹了spring-boot react如何一步一步實(shí)現(xiàn)增刪改查,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11詳解java開啟異步線程的幾種方法(@Async,AsyncManager,線程池)
在springboot框架中,可以使用注解簡單實(shí)現(xiàn)線程的操作,還有AsyncManager的方式,如果需要復(fù)雜的線程操作,可以使用線程池實(shí)現(xiàn),本文通過實(shí)例代碼介紹java開啟異步線程的幾種方法(@Async,AsyncManager,線程池),感興趣的朋友一起看看吧2023-09-09IntelliJ IDEA Run時(shí)報(bào)“無效的源發(fā)行版:16“錯(cuò)誤問題及解決方法
這篇文章主要介紹了IntelliJ IDEA Run時(shí)報(bào)“無效的源發(fā)行版:16“錯(cuò)誤問題及解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05簡單了解SpringMVC緩存對(duì)靜態(tài)資源有什么影響
這篇文章主要介紹了簡單了解SpringMVC緩存對(duì)靜態(tài)資源有什么影響,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09