Spring Boot 中使用 Mybatis Plus的操作方法
Spring Boot 中使用 Mybatis Plus
在現(xiàn)代的企業(yè)級開發(fā)中,MyBatis Plus 是 MyBatis 的增強工具,它簡化了很多常見的數(shù)據(jù)庫操作。通過 Spring Boot 集成 MyBatis Plus,可以快速構(gòu)建高效、簡潔的數(shù)據(jù)庫操作層。本文將介紹如何在 Spring Boot 項目中集成 MyBatis Plus。
1. 添加 Maven 依賴
在Spring Boot 項目的 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 # 自動提交從連接池中返回的連接
idle-timeout: 30000 # 連接允許在連接池中閑置的最長時間
pool-name: SpringBootDemo-HikariCP # 連接池的用戶定義名稱
max-lifetime: 1800000 # 連接池中連接最長生命周期
connection-timeout: 30000 # 等待來自連接池的連接的最大毫秒數(shù)
connection-test-query: SELECT 1 # 連接池連接測試語句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)建實體類
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 是實現(xiàn)了 IService 接口的抽象類, 對 Mapper 進行了增強封裝
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}Service接口層
public interface UserService extends IService<User> {
}7. 結(jié)論
本文介紹了如何在 Spring Boot 項目中集成 Mybatis Plus,Spring Boot 與 MyBatis Plus 的集成非常簡單,通過自動配置和簡潔的 API,可以大大減少開發(fā)中常見的數(shù)據(jù)庫操作代碼。MyBatis Plus 提供了很多實用的功能,如分頁查詢、條件構(gòu)造、自動填充等,能大大提高開發(fā)效率。
到此這篇關(guān)于Spring Boot 中使用 Mybatis Plus的文章就介紹到這了,更多相關(guān)Spring Boot使用 Mybatis Plus內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 5分鐘快速搭建SpringBoot3?+?MyBatis-Plus工程/項目的實現(xiàn)示例
- 解決mybatis-plus-boot-starter與mybatis-spring-boot-starter的錯誤問題
- Spring Boot 中整合 MyBatis-Plus詳細步驟(最新推薦)
- Spring?Boot?集成?MyBatis?全面講解(最新推薦)
- SpringBoot同時集成Mybatis和Mybatis-plus框架
- Springboot使用MybatisPlus實現(xiàn)mysql樂觀鎖
- 淺談Spring Boot、MyBatis、MyBatis-Plus 依賴版本對應關(guān)系
- Spring Boot Mybatis++ 2025詳解
相關(guān)文章
java calendar 日期實現(xiàn)不斷加一天的代碼
這篇文章主要介紹了java calendar 日期實現(xiàn)不斷加一天的代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
SpringBoot整合Java DL4J實現(xiàn)情感分析系統(tǒng)
在當今數(shù)字化時代,企業(yè)越來越重視用戶的反饋,以不斷改進產(chǎn)品和服務,自然語言處理技術(shù)為分析用戶評價提供了強大的工具,本文將介紹如何使用 Spring Boot 整合 Java Deeplearning4j 構(gòu)建一個情感分析系統(tǒng),需要的朋友可以參考下2024-10-10
SpringMVC @GetMapping注解路徑?jīng)_突問題解決
MD5對密碼進行加密存儲是常見的一種加密方式,本文主要介紹了Java雙重MD5加密實現(xiàn)安全登錄,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07

