MyBatis-Plus自動(dòng)填充字段的詳細(xì)教程
1. 項(xiàng)目環(huán)境配置
首先,需要確保項(xiàng)目中已經(jīng)集成了 Spring Boot 和 MyBatis-Plus。下面是一些基本的配置步驟:
1.1 引入必要的依賴(lài)
在 pom.xml
中添加 MyBatis-Plus 的依賴(lài):
<dependencies> <!-- MyBatis-Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> <!-- 其他必要依賴(lài) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MySQL 驅(qū)動(dòng) --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies>
1.2 配置數(shù)據(jù)源
在 application.yml
或 application.properties
文件中配置數(shù)據(jù)庫(kù)連接:
spring: datasource: url: jdbc:mysql://localhost:3306/your_database?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC username: your_username password: your_password driver-class-name: com.mysql.cj.jdbc.Driver mybatis-plus: mapper-locations: classpath:/mapper/*.xml type-aliases-package: com.example.entity global-config: db-config: id-type: auto
2. 創(chuàng)建基礎(chǔ)實(shí)體類(lèi)
為數(shù)據(jù)庫(kù)中的表創(chuàng)建一個(gè)基礎(chǔ)實(shí)體類(lèi) BaseEntity
,在該類(lèi)中定義創(chuàng)建時(shí)間和更新時(shí)間字段,并使用 MyBatis-Plus 的注解配置自動(dòng)填充。
package com.example.utils; import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.annotation.TableField; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; import lombok.Data; import java.io.Serializable; import java.util.Date; import java.util.HashMap; import java.util.Map; @Data public class BaseEntity implements Serializable { private static final long serialVersionUID = 1L; /** * 搜索值(暫時(shí)忽略) */ @JsonIgnore @TableField(exist = false) private String searchValue; /** * 創(chuàng)建者 */ @TableField(fill = FieldFill.INSERT) private String createBy; /** * 創(chuàng)建時(shí)間 */ @TableField(fill = FieldFill.INSERT) private Date createTime; /** * 更新者 */ @TableField(fill = FieldFill.INSERT_UPDATE) private String updateBy; /** * 更新時(shí)間 */ @TableField(fill = FieldFill.INSERT_UPDATE) private Date updateTime; /** * 請(qǐng)求參數(shù)(暫時(shí)忽略) */ @JsonInclude(JsonInclude.Include.NON_EMPTY) @TableField(exist = false) private Map<String, Object> params = new HashMap<>(); }
3. 實(shí)現(xiàn) MetaObjectHandler 接口
為了使 BaseEntity
中的自動(dòng)填充注解生效,我們需要實(shí)現(xiàn) MetaObjectHandler
接口,并將其配置為 Spring 的一個(gè) Bean。
3.1 創(chuàng)建 MyMetaObjectHandler 類(lèi)
package com.example.config; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import org.apache.ibatis.reflection.MetaObject; import org.springframework.stereotype.Component; import java.util.Date; @Component public class MyMetaObjectHandler implements MetaObjectHandler { @Override public void insertFill(MetaObject metaObject) { // 填充創(chuàng)建時(shí)間和更新時(shí)間 this.strictInsertFill(metaObject, "createTime", Date.class, new Date()); this.strictInsertFill(metaObject, "updateTime", Date.class, new Date()); // 可以根據(jù)業(yè)務(wù)需求獲取當(dāng)前用戶(hù),填充創(chuàng)建者和更新者 this.strictInsertFill(metaObject, "createBy", String.class, getCurrentUser()); this.strictInsertFill(metaObject, "updateBy", String.class, getCurrentUser()); } @Override public void updateFill(MetaObject metaObject) { // 更新時(shí)只填充更新時(shí)間和更新者 this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date()); this.strictUpdateFill(metaObject, "updateBy", String.class, getCurrentUser()); } // 示例方法:獲取當(dāng)前用戶(hù)信息 private String getCurrentUser() { // 這里可以集成 Spring Security 或其他上下文獲取實(shí)際的當(dāng)前用戶(hù) return "system"; // 這是一個(gè)占位符,實(shí)際應(yīng)用中會(huì)替換為真實(shí)用戶(hù)信息 } }
3.2 配置生效
通過(guò)在 MyMetaObjectHandler
類(lèi)上使用 @Component
注解,Spring 會(huì)自動(dòng)管理這個(gè)類(lèi),并在實(shí)體插入和更新時(shí)調(diào)用它來(lái)填充字段。
4. 實(shí)體類(lèi)繼承 BaseEntity
現(xiàn)在,你的具體實(shí)體類(lèi)可以繼承 BaseEntity
,從而自動(dòng)獲得 createTime
和 updateTime
字段的自動(dòng)填充功能。
package com.example.entity; import com.baomidou.mybatisplus.annotation.TableName; import com.example.utils.BaseEntity; import lombok.Data; @Data @TableName("your_table") public class YourEntity extends BaseEntity { // 其他字段 private String name; private Integer age; }
5. 處理數(shù)據(jù)插入與更新
在 Service 或 Mapper 層,執(zhí)行插入或更新操作時(shí),createTime
和 updateTime
字段會(huì)自動(dòng)填充。
5.1 Service 層示例
@Service public class YourEntityService { @Autowired private YourEntityMapper yourEntityMapper; public void saveEntity(YourEntity entity) { yourEntityMapper.insert(entity); } public void updateEntity(YourEntity entity) { yourEntityMapper.updateById(entity); } }
6. 驗(yàn)證自動(dòng)填充功能
通過(guò)簡(jiǎn)單的單元測(cè)試或集成測(cè)試,驗(yàn)證 createTime
和 updateTime
字段在插入和更新時(shí)是否正確自動(dòng)填充。
7. 其他注意事項(xiàng)
- 字段類(lèi)型: 確保數(shù)據(jù)庫(kù)中的
createTime
和updateTime
字段類(lèi)型與 Java 實(shí)體類(lèi)中的類(lèi)型相匹配(通常是DATETIME
類(lèi)型)。 - 時(shí)間格式: 如果需要統(tǒng)一時(shí)間格式,可以在配置文件中設(shè)置 Spring Boot 的全局時(shí)間格式,或使用
@JsonFormat
注解指定序列化時(shí)的格式。
spring: jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+8
總結(jié)
通過(guò)本文的教程,你可以輕松地在 Spring Boot 項(xiàng)目中使用 MyBatis-Plus 自動(dòng)填充創(chuàng)建時(shí)間和更新時(shí)間字段。該方法充分利用了 MyBatis-Plus 的自動(dòng)填充機(jī)制,并結(jié)合 Spring Boot 的優(yōu)勢(shì),使開(kāi)發(fā)過(guò)程更加簡(jiǎn)潔高效。如果遇到問(wèn)題,務(wù)必檢查 MetaObjectHandler
是否正確注冊(cè),字段類(lèi)型是否匹配,以及數(shù)據(jù)庫(kù)配置是否正確。
以上就是MyBatis-Plus自動(dòng)填充字段的詳細(xì)教程的詳細(xì)內(nèi)容,更多關(guān)于MyBatis-Plus自動(dòng)填充字段的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- MyBatisPlus實(shí)現(xiàn)自動(dòng)填充字段的實(shí)踐
- mybatis-plus調(diào)用update方法時(shí),自動(dòng)填充字段不生效問(wèn)題及解決
- MyBatis-Puls插入或修改時(shí)某些字段自動(dòng)填充操作示例
- Mybatis-Plus實(shí)現(xiàn)公共字段自動(dòng)填充的項(xiàng)目實(shí)踐
- MyBatis-Plus邏輯刪除和字段自動(dòng)填充的實(shí)現(xiàn)
- MyBatis-Plus實(shí)現(xiàn)公共字段自動(dòng)填充功能詳解
- Mybatis-Plus自動(dòng)填充更新操作相關(guān)字段的實(shí)現(xiàn)
- MyBatis-Plus實(shí)現(xiàn)字段自動(dòng)填充功能的示例
- Mybatis plus通用字段自動(dòng)填充的示例
- Mybatis攔截器實(shí)現(xiàn)公共字段填充的示例代碼
相關(guān)文章
java新人基礎(chǔ)入門(mén)之遞歸調(diào)用
這篇文章主要給大家介紹了關(guān)于java新人基礎(chǔ)入門(mén)之遞歸調(diào)用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Spring AOP 實(shí)現(xiàn)自定義注解的示例
這篇文章主要介紹了Spring AOP 實(shí)現(xiàn)自定義注解的示例,幫助大家更好的理解和學(xué)習(xí)使用spring框架,感興趣的朋友可以了解下2021-03-03Java NIO寫(xiě)大文件對(duì)比(win7和mac)
這篇文章主要介紹了Java NIO寫(xiě)大文件對(duì)比(win7和mac),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07java.net.ConnectException: Connection refused問(wèn)題解決辦法
這篇文章主要介紹了java.net.ConnectException: Connection refused問(wèn)題解決辦法的相關(guān)資料,需要的朋友可以參考下2016-12-12淺析如何在SpringBoot中實(shí)現(xiàn)數(shù)據(jù)脫敏
脫敏是指在不改變?cè)瓟?shù)據(jù)結(jié)構(gòu)的前提下,通過(guò)某種方式處理數(shù)據(jù),使數(shù)據(jù)不能直接暴露用戶(hù)的真實(shí)信息,下面我們就來(lái)看看SpringBoot中實(shí)現(xiàn)數(shù)據(jù)脫敏的具體方法吧2024-03-03SpringBoot整合SpringSecurityOauth2實(shí)現(xiàn)鑒權(quán)動(dòng)態(tài)權(quán)限問(wèn)題
這篇文章主要介紹了SpringBoot整合SpringSecurityOauth2實(shí)現(xiàn)鑒權(quán)-動(dòng)態(tài)權(quán)限,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06