淺談Spring Boot、MyBatis、MyBatis-Plus 依賴版本對應(yīng)關(guān)系
在使用 Spring Boot、MyBatis 和 MyBatis-Plus 時,確保它們的依賴版本兼容是項(xiàng)目正常運(yùn)行的關(guān)鍵。版本不兼容可能會導(dǎo)致諸如 sqlSessionFactory、sqlSessionTemplate 未正確配置等錯誤。因此,合理選擇各個依賴的版本尤為重要。以下是 Spring Boot 各個版本與 MyBatis 和 MyBatis-Plus 的版本對應(yīng)關(guān)系總結(jié)。
1. Spring Boot、MyBatis、MyBatis-Plus 依賴版本對應(yīng)關(guān)系表
| Spring Boot 版本 | MyBatis 版本 | MyBatis-Plus 版本 | MyBatis-Plus Starter 版本 |
|---|---|---|---|
| 2.7.x | 2.2.x | 3.4.x | 3.4.x |
| 3.0.x | 3.5.x | 3.5.x | 3.5.x |
| 3.1.x | 3.5.x | 3.5.x | 3.5.x |
| 3.2.x | 3.5.x | 3.5.x | 3.5.x |
2. 依賴選擇說明
- MyBatis 版本:
- Spring Boot 2.x 版本推薦使用 MyBatis 2.x 系列。
- 從 Spring Boot 3.x 開始,推薦使用 MyBatis 3.5.x 系列。它是 MyBatis 當(dāng)前最穩(wěn)定的版本,支持最新的 Java 17 特性以及 Spring Boot 3.x API。
- MyBatis-Plus 版本:
- MyBatis-Plus 3.4.x 適用于 Spring Boot 2.x 系列。
- MyBatis-Plus 3.5.x 是與 Spring Boot 3.x 系列兼容的最佳選擇。此版本優(yōu)化了對 Java 17 和 Spring Boot 3.x 的支持,同時提高了性能和擴(kuò)展能力。
- MyBatis-Plus Starter 版本:
mybatis-plus-boot-starter是 MyBatis-Plus 提供的整合 Starter,它包括了 MyBatis-Plus 所需的核心依賴和配置,可以簡化項(xiàng)目配置過程。與 Spring Boot 2.x 使用 3.4.x 版本,與 Spring Boot 3.x 則使用 3.5.x 版本。
3. 版本選擇注意事項(xiàng)
- 兼容性問題: 如果在項(xiàng)目中遇到
sqlSessionFactory或sqlSessionTemplate配置錯誤,最常見的問題是 MyBatis 或 MyBatis-Plus 的版本與 Spring Boot 版本不兼容。務(wù)必確保三者的版本處于同一兼容范圍內(nèi)。 - Spring Boot 3.x 的變化: Spring Boot 3.x 與之前的版本有較大改動,特別是在模塊化系統(tǒng)、Java 17 支持等方面。使用 MyBatis 和 MyBatis-Plus 的時候,一定要選擇 3.5.x 版本,以便與 Spring Boot 3.x 更好地配合。
4. 依賴示例(Spring Boot 3.1.x + MyBatis 3.5.x + MyBatis-Plus 3.5.x)
<dependencies>
<!-- Spring Boot Web Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- PostgreSQL 驅(qū)動 -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.6.2</version>
</dependency>
<!-- MyBatis Plus Starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
5. 常見問題及解決方案
- 錯誤提示:Property ‘sqlSessionFactory’ or ‘sqlSessionTemplate’ are required: 這種報(bào)錯通常是由于 MyBatis-Plus 和 Spring Boot 的版本不兼容造成的。解決方案是檢查并升級 MyBatis-Plus 到與 Spring Boot 版本匹配的版本。
- 版本沖突: 如果項(xiàng)目中有其他依賴版本沖突,確保所有依賴庫使用的是兼容的版本,特別是在使用
mybatis-plus-boot-starter時,MyBatis 和 MyBatis-Plus 的版本必須匹配。
6. 總結(jié)
在整合 Spring Boot、MyBatis 和 MyBatis-Plus 時:
- 確保所使用的 Spring Boot、MyBatis、MyBatis-Plus 和其 Starter 的版本是相互兼容的。
- 對于 Spring Boot 3.x,建議選擇 MyBatis 3.5.x 和 MyBatis-Plus 3.5.x,并確保所有依賴庫版本一致。
- 如果遇到錯誤,首先檢查版本是否匹配,再排查具體配置問題。
通過合理選擇依賴版本和 Starter,可以有效避免常見的配置錯誤和兼容性問題。
到此這篇關(guān)于淺談Spring Boot、MyBatis、MyBatis-Plus 依賴版本對應(yīng)關(guān)系的文章就介紹到這了,更多相關(guān)Spring Boot MyBatis MyBatis-Plus依賴版本對應(yīng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 5分鐘快速搭建SpringBoot3?+?MyBatis-Plus工程/項(xiàng)目的實(shí)現(xiàn)示例
- 解決mybatis-plus-boot-starter與mybatis-spring-boot-starter的錯誤問題
- Spring Boot 中整合 MyBatis-Plus詳細(xì)步驟(最新推薦)
- Spring?Boot?集成?MyBatis?全面講解(最新推薦)
- Spring Boot 中使用 Mybatis Plus的操作方法
- SpringBoot同時集成Mybatis和Mybatis-plus框架
- Springboot使用MybatisPlus實(shí)現(xiàn)mysql樂觀鎖
- Spring Boot Mybatis++ 2025詳解
相關(guān)文章
Spring Cloud Gateway使用Token驗(yàn)證詳解
這篇文章主要介紹了Spring Cloud Gateway使用Token驗(yàn)證詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02
Java網(wǎng)絡(luò)編程之基于TCP協(xié)議
本文主要將Java基于TCP的網(wǎng)絡(luò)編程主要分解成5個功能:功能分解1:單向通信功能分解,2:雙向通信功能分解,3:對象流傳送功能分解,4:加入完整的處理異常方式功能分解,5:多線程接收用戶請求,需要的朋友可以參考下2021-05-05
Spring?Boot4實(shí)戰(zhàn)指南之Jackson2.x升級到3.x踩坑完全記錄
Spring?Boot升級幾乎是Java后端項(xiàng)目繞不開的話題,尤其是很多老項(xiàng)目還停留在2.x,一旦要升級,就會發(fā)現(xiàn)牽扯的絕不只是改個版本號,這篇文章主要介紹了Spring?Boot4實(shí)戰(zhàn)指南之Jackson2.x升級到3.x踩坑完全記錄的相關(guān)資料,需要的朋友可以參考下2026-05-05
將Java的List結(jié)構(gòu)通過GSON庫轉(zhuǎn)換為JSON的方法示例
GONS是Google在GitHub上開源的Java類庫,提供各種Java對象和JSON格式對象之間的轉(zhuǎn)換功能,將Java的List結(jié)構(gòu)通過GSON庫轉(zhuǎn)換為JSON的方法示例2016-06-06
JavaWeb實(shí)現(xiàn)用戶登錄與注冊功能(服務(wù)器)
這篇文章主要介紹了JavaWeb實(shí)現(xiàn)用戶登錄與注冊功能,服務(wù)器部分的關(guān)鍵代碼實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
java數(shù)學(xué)工具類Math詳解(round方法)
這篇文章主要為大家詳細(xì)介紹了java數(shù)學(xué)工具類Math,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08

