Spring Boot 中使用 Mybatis Plus的操作方法
Spring Boot 中使用 Mybatis Plus
在現(xiàn)代的企業(yè)級(jí)開發(fā)中,MyBatis Plus 是 MyBatis 的增強(qiáng)工具,它簡(jiǎn)化了很多常見的數(shù)據(jù)庫操作。通過 Spring Boot 集成 MyBatis Plus,可以快速構(gòu)建高效、簡(jiǎn)潔的數(shù)據(jù)庫操作層。本文將介紹如何在 Spring Boot 項(xiàng)目中集成 MyBatis Plus。
1. 添加 Maven 依賴
在Spring Boot 項(xiàng)目的 pom.xml
文件中添加如下相關(guān)的依賴:
<dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.7</version> </dependency>
2. 在配置文件中加入相關(guān)配置
spring: datasource: url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull username: root password: xxxxxx hikari: # 數(shù)據(jù)庫連接池 minimum-idle: 5 # 連接池最小空閑連接數(shù) maximum-pool-size: 20 # 連接池最大連接數(shù) auto-commit: true # 自動(dòng)提交從連接池中返回的連接 idle-timeout: 30000 # 連接允許在連接池中閑置的最長(zhǎng)時(shí)間 pool-name: SpringBootDemo-HikariCP # 連接池的用戶定義名稱 max-lifetime: 1800000 # 連接池中連接最長(zhǎng)生命周期 connection-timeout: 30000 # 等待來自連接池的連接的最大毫秒數(shù) connection-test-query: SELECT 1 # 連接池連接測(cè)試語句
3. 創(chuàng)建 Mybatis 配置類
// 掃描 Mapper 所在包路徑 @MapperScan("com.xxxx.xxx") @Configuration public class MybatisPlusConfig { /** * 添加 Mybatis Plus 分頁插件 */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor()); return mybatisPlusInterceptor; } }
4. 創(chuàng)建實(shí)體類
public class User { @TableId(type = IdType.AUTO) private Long id; private String name; private Integer age; @TableLogic private boolean isDeleted; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public boolean isDeleted() { return isDeleted; } public void setDeleted(boolean deleted) { isDeleted = deleted; } }
5.創(chuàng)建Mapper
MyBatis Plus 提供了基礎(chǔ)的 Mapper 接口,繼承它即可擁有常用的增、刪、改、查功能。
public interface UserMapper extends BaseMapper<User> { }
6. 創(chuàng)建 Service
Mybatis Plus 的 ServiceImpl 是實(shí)現(xiàn)了 IService 接口的抽象類, 對(duì) Mapper 進(jìn)行了增強(qiáng)封裝
@Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { }
Service接口層
public interface UserService extends IService<User> { }
7. 結(jié)論
本文介紹了如何在 Spring Boot 項(xiàng)目中集成 Mybatis Plus,Spring Boot 與 MyBatis Plus 的集成非常簡(jiǎn)單,通過自動(dòng)配置和簡(jiǎn)潔的 API,可以大大減少開發(fā)中常見的數(shù)據(jù)庫操作代碼。MyBatis Plus 提供了很多實(shí)用的功能,如分頁查詢、條件構(gòu)造、自動(dòng)填充等,能大大提高開發(fā)效率。
到此這篇關(guān)于Spring Boot 中使用 Mybatis Plus的文章就介紹到這了,更多相關(guān)Spring Boot使用 Mybatis Plus內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 5分鐘快速搭建SpringBoot3?+?MyBatis-Plus工程/項(xiàng)目的實(shí)現(xiàn)示例
- 解決mybatis-plus-boot-starter與mybatis-spring-boot-starter的錯(cuò)誤問題
- Spring Boot 中整合 MyBatis-Plus詳細(xì)步驟(最新推薦)
- Spring?Boot?集成?MyBatis?全面講解(最新推薦)
- SpringBoot同時(shí)集成Mybatis和Mybatis-plus框架
- Springboot使用MybatisPlus實(shí)現(xiàn)mysql樂觀鎖
- 淺談Spring Boot、MyBatis、MyBatis-Plus 依賴版本對(duì)應(yīng)關(guān)系
- Spring Boot Mybatis++ 2025詳解
相關(guān)文章
Java內(nèi)存模型相關(guān)知識(shí)總結(jié)
這篇文章主要介紹了Java內(nèi)存模型相關(guān)知識(shí)總結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10Spring如何基于注解顯式實(shí)現(xiàn)自動(dòng)裝配
這篇文章主要介紹了Spring如何基于注解顯式實(shí)現(xiàn)自動(dòng)裝配,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08淺談在springboot中使用定時(shí)任務(wù)的方式
今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識(shí),文章圍繞著在springboot中使用定時(shí)任務(wù)的方式展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06Java開發(fā)人員最常犯的5個(gè)錯(cuò)誤總結(jié)
作為一名java開發(fā)程序員,不知道大家有沒有遇到過一些匪夷所思的bug。這些錯(cuò)誤通常需要您幾個(gè)小時(shí)才能解決。今天,小編總結(jié)一些常見的編碼錯(cuò)誤,然后給出解決方案。希望大家在日常編碼中能夠避免這樣的問題2022-12-12Springboot工具類FileCopyUtils使用教程
這篇文章主要介紹了Springboot內(nèi)置的工具類之FileCopyUtils的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12SpringBoot項(xiàng)目部署到Tomcat的最新步驟
通過使用Spring Boot應(yīng)用程序,我們可以創(chuàng)建一個(gè)war文件來部署到Web服務(wù)器中,這篇文章主要給大家介紹了關(guān)于SpringBoot項(xiàng)目部署到Tomcat的最新步驟,需要的朋友可以參考下2024-01-01GateWay路由規(guī)則與動(dòng)態(tài)路由詳細(xì)介紹
這篇文章主要介紹了GateWay路由規(guī)則與GateWay動(dòng)態(tài)路由,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09