在 spring boot + mybatis plus 中实现 update_time 字段自动更新,可通过 mybatis plus 的自动填充(auto fill)功能 完成。以下是详细步骤:
步骤 1:添加依赖(若未添加)
确保项目中已引入 mybatis plus 核心依赖(spring boot 项目通常已集成):
<!-- pom.xml -->
<dependency>
<groupid>com.baomidou</groupid>
<artifactid>mybatis-plus-boot-starter</artifactid>
<version>3.5.3.1</version> <!-- 推荐 3.3.0+ 版本 -->
</dependency>
步骤 2:实体类配置
在需要自动更新的 update_time 字段上添加 @tablefield 注解,并指定填充策略为 fieldfill.update。
示例实体类:
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") // 对应数据库表名
public class yourentity {
// 其他字段...
/**
* 更新时间(自动填充)
*/
@tablefield(fill = fieldfill.update) // 关键注解:更新时自动填充
private localdatetime updatetime;
}
fieldfill.update:表示该字段仅在 更新操作 时自动填充。
若需 插入和更新时都填充,可使用 fieldfill.insert_update。
步骤 3:实现元对象处理器(metaobjecthandler)
创建一个类实现 metaobjecthandler 接口,重写 updatefill 方法,定义更新时的填充逻辑(如设置当前时间)。
示例代码:
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 {
/**
* 更新时自动填充(覆盖 updatefill 方法)
*/
@override
public void updatefill(metaobject metaobject) {
// 设置 update_time 为当前时间(无需手动调用,mp 自动处理)
this.strictinsertfill(metaobject, "updatetime", localdatetime.class, localdatetime.now());
// 或使用更灵活的非严格模式(推荐,避免字段不存在时报错)
// this.fillstrategy(metaobject, "updatetime", localdatetime.now());
}
}
strictinsertfill:严格模式,仅当字段存在且值为 null 时填充(可能抛异常,谨慎使用)。
fillstrategy:非严格模式,直接填充(推荐,兼容性更好)。
步骤 4:验证自动填充效果
使用 mybatis plus 提供的更新方法(如 updatebyid、update),无需手动设置 updatetime,mp 会自动填充当前时间。
示例更新操作:
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) {
// 只需设置需要更新的字段(如 name),updatetime 会自动填充
entity.setname("新名称");
// 调用 mp 的更新方法(基于主键更新)
return this.updatebyid(entity);
}
}
注意事项
1.数据库字段类型:确保数据库中 update_time 字段类型与 java 类型匹配(如 datetime、timestamp 或 bigint 存储时间戳)。
- 若使用
localdatetime,数据库建议用datetime(mysql 5.6.5+ 支持)或timestamp。 - 若需兼容旧版 mysql(5.6 以下),可改用
bigint存储时间戳(毫秒级),并在填充时使用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.时区问题:若服务器时区与业务时区不一致,需统一时区配置(如 application.yml 中设置 spring.jackson.time-zone=asia/shanghai)。
总结
通过 mybatis plus 的 @tablefield(fill = fieldfill.update) 注解和 metaobjecthandler 元对象处理器,可轻松实现 update_time 字段的自动更新,无需手动干预。核心是注解标记字段 + 处理器定义填充逻辑 + 使用 mp 内置更新方法。
到此这篇关于springboot+mybatis plus实现update_time字段自动更新详解的文章就介绍到这了,更多相关springboot mybatis plus字段更新内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论