SpringBoot+MyBatis Plus實(shí)現(xiàn)update_time字段自動更新詳解
在 Spring Boot + MyBatis Plus 中實(shí)現(xiàn) update_time 字段自動更新,可通過 ??MyBatis Plus 的自動填充(Auto Fill)功能?? 完成。以下是詳細(xì)步驟:
??步驟 1:添加依賴(若未添加)??
確保項(xiàng)目中已引入 MyBatis Plus 核心依賴(Spring Boot 項(xiàng)目通常已集成):
<!-- pom.xml -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version> <!-- 推薦 3.3.0+ 版本 -->
</dependency>
??步驟 2:實(shí)體類配置??
在需要自動更新的 update_time 字段上添加 @TableField 注解,并指定填充策略為 FieldFill.UPDATE。
示例實(shí)體類:
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import java.time.LocalDateTime;
@TableName("your_table") // 對應(yīng)數(shù)據(jù)庫表名
public class YourEntity {
// 其他字段...
/**
* 更新時(shí)間(自動填充)
*/
@TableField(fill = FieldFill.UPDATE) // 關(guān)鍵注解:更新時(shí)自動填充
private LocalDateTime updateTime;
}
FieldFill.UPDATE:表示該字段僅在 ??更新操作?? 時(shí)自動填充。
若需 ??插入和更新時(shí)都填充??,可使用 FieldFill.INSERT_UPDATE。
??步驟 3:實(shí)現(xiàn)元對象處理器(MetaObjectHandler)??
創(chuàng)建一個(gè)類實(shí)現(xiàn) MetaObjectHandler 接口,重寫 updateFill 方法,定義更新時(shí)的填充邏輯(如設(shè)置當(dāng)前時(shí)間)。
示例代碼:
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
@Component // 必須注冊為 Bean
public class MyMetaObjectHandler implements MetaObjectHandler {
/**
* 更新時(shí)自動填充(覆蓋 updateFill 方法)
*/
@Override
public void updateFill(MetaObject metaObject) {
// 設(shè)置 update_time 為當(dāng)前時(shí)間(無需手動調(diào)用,MP 自動處理)
this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
// 或使用更靈活的非嚴(yán)格模式(推薦,避免字段不存在時(shí)報(bào)錯)
// this.fillStrategy(metaObject, "updateTime", LocalDateTime.now());
}
}
??strictInsertFill??:嚴(yán)格模式,僅當(dāng)字段存在且值為 null 時(shí)填充(可能拋異常,謹(jǐn)慎使用)。
??fillStrategy??:非嚴(yán)格模式,直接填充(推薦,兼容性更好)。
??步驟 4:驗(yàn)證自動填充效果??
使用 MyBatis Plus 提供的更新方法(如 updateById、update),??無需手動設(shè)置 updateTime??,MP 會自動填充當(dāng)前時(shí)間。
示例更新操作:
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
@Service
public class YourServiceImpl extends ServiceImpl<YourMapper, YourEntity> implements YourService {
@Override
public boolean updateEntity(YourEntity entity) {
// 只需設(shè)置需要更新的字段(如 name),updateTime 會自動填充
entity.setName("新名稱");
// 調(diào)用 MP 的更新方法(基于主鍵更新)
return this.updateById(entity);
}
}
??注意事項(xiàng)??
1.??數(shù)據(jù)庫字段類型??:確保數(shù)據(jù)庫中 update_time 字段類型與 Java 類型匹配(如 datetime、timestamp 或 bigint 存儲時(shí)間戳)。
- 若使用
LocalDateTime,數(shù)據(jù)庫建議用datetime(MySQL 5.6.5+ 支持)或timestamp。 - 若需兼容舊版 MySQL(5.6 以下),可改用
bigint存儲時(shí)間戳(毫秒級),并在填充時(shí)使用System.currentTimeMillis()。
2.??自定義 SQL 場景??:若使用自定義 SQL(如 XML 或 @Update 注解),需手動在 SQL 中添加 update_time = NOW()(或通過 MP 自動填充)。
示例(XML 自定義更新):
<update id="customUpdate">
UPDATE your_table
SET name = #{name}, update_time = NOW() <!-- 手動添加 -->
WHERE id = #{id}
</update>
3.??時(shí)區(qū)問題??:若服務(wù)器時(shí)區(qū)與業(yè)務(wù)時(shí)區(qū)不一致,需統(tǒng)一時(shí)區(qū)配置(如 application.yml 中設(shè)置 spring.jackson.time-zone=Asia/Shanghai)。
??總結(jié)??
通過 MyBatis Plus 的 @TableField(fill = FieldFill.UPDATE) 注解和 MetaObjectHandler 元對象處理器,可輕松實(shí)現(xiàn) update_time 字段的自動更新,無需手動干預(yù)。核心是??注解標(biāo)記字段?? + ??處理器定義填充邏輯?? + ??使用 MP 內(nèi)置更新方法??。
到此這篇關(guān)于SpringBoot+MyBatis Plus實(shí)現(xiàn)update_time字段自動更新詳解的文章就介紹到這了,更多相關(guān)SpringBoot MyBatis Plus字段更新內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java文件處理之使用itextpdf實(shí)現(xiàn)excel轉(zhuǎn)pdf
在文件處理中,經(jīng)常有文件類型轉(zhuǎn)換的使用場景,本文主要介紹了如何使用poi以及itextpdf完成excel轉(zhuǎn)pdf的操作,需要的小伙伴可以參考一下2024-02-02
Spring boot + thymeleaf 后端直接給onclick函數(shù)賦值的實(shí)現(xiàn)代碼
這篇文章主要介紹了Spring boot + thymeleaf 后端直接給onclick函數(shù)賦值的實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-06-06
Spring中@Autowired和@Qualifier注解的3個(gè)知識點(diǎn)小結(jié)
這篇文章主要介紹了Spring中@Autowired和@Qualifier注解的3個(gè)知識點(diǎn)小結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
SpringBoot整合Security實(shí)現(xiàn)權(quán)限控制框架(案例詳解)
Spring Security是一個(gè)能夠?yàn)榛赟pring的企業(yè)應(yīng)用系統(tǒng)提供聲明式的安全訪問控制解決方案的安全框,是一個(gè)重量級的安全管理框架,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-08-08

