淺談Spring Boot、MyBatis、MyBatis-Plus 依賴版本對應(yīng)關(guān)系
在使用 Spring Boot、MyBatis 和 MyBatis-Plus 時,確保它們的依賴版本兼容是項目正常運行的關(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 的支持,同時提高了性能和擴展能力。
- MyBatis-Plus Starter 版本:
mybatis-plus-boot-starter
是 MyBatis-Plus 提供的整合 Starter,它包括了 MyBatis-Plus 所需的核心依賴和配置,可以簡化項目配置過程。與 Spring Boot 2.x 使用 3.4.x 版本,與 Spring Boot 3.x 則使用 3.5.x 版本。
3. 版本選擇注意事項
- 兼容性問題: 如果在項目中遇到
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: 這種報錯通常是由于 MyBatis-Plus 和 Spring Boot 的版本不兼容造成的。解決方案是檢查并升級 MyBatis-Plus 到與 Spring Boot 版本匹配的版本。
- 版本沖突: 如果項目中有其他依賴版本沖突,確保所有依賴庫使用的是兼容的版本,特別是在使用
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à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實現(xiàn)mysql樂觀鎖
- Spring Boot Mybatis++ 2025詳解
相關(guān)文章
springboot?maven?plugin報紅的解決辦法
本文主要介紹了springboot?maven?plugin報紅的解決辦法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07SpringCloud Gateway的路由,過濾器和限流解讀
這篇文章主要介紹了SpringCloud Gateway的路由,過濾器和限流解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02@RefreshScope在Quartz 觸發(fā)器類導(dǎo)致異常問題解決分析
這篇文章主要為大家介紹了@RefreshScope在Quartz 觸發(fā)器類導(dǎo)致異常問題解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02基于maven搭建一個ssm的web項目的詳細(xì)圖文教程
這篇文章主要介紹了基于maven搭建一個ssm的web項目的詳細(xì)教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09springboot實現(xiàn)公眾號接收回復(fù)消息和超過5秒被動回復(fù)消息
本文主要介紹了springboot實現(xiàn)公眾號接收回復(fù)消息和超過5秒被動回復(fù)消息,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05