MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)
目录
- 前言
- 一、问题现象
- 二、原因分析
- 三、总结:常见原因与解决方法对照表
- 四、推荐写法
前言
在使用 MyBATis Plus 时,通常我们会在数据库表中设置 create_time
和 update_time
两个字段,借助 MyB编程客栈atis Plus 的 自动填充功能 来维护这些时间字段。
但是,有时候你可能会遇到 update_time
字段未自动更新的情况,哪怕代码中已经配置了 MetaObjectHandler
。本文将针对这一问题,分析可能的原因,并提供详细的解决方案。
一、问题现象
假设我们有如下的 updateFill()
方法:
@Override public void updateFill(MetaObject metaObject) { log.info("执行更新填充..."); this.strictInsertFill(metaObject, "updateTime", Timestamp.clpythonass, Timestamp.valueOf(LocalDateTime.now())); this.strictInsertFill(metaObject, "updateBy", BigInteger.class, userId); }
但是执行更新操作后:
- 数据库中的
update_time
字段未更新 - 没有报错,但
updateTime
依然是旧值 updateBy
可能也未更新
接下来我们来分析可能的原因。
二、原因分析
1. 使用了 strictInsertFill/strictUpdateFill
导致更新失效
原因:
strictInsertFill/strictUpdateFill
主要用于插入数据时的自动填充。即使在 updateFill()
中使用它,也不会生效。
原理解释:
strictInsertFill/strictUpdateFill
的作用是 如果字段已有值则不会覆盖,而在更新场景中,一般实体对象中 update_time
已有值。
解决方案:
将 strictInsertFill/strictUpdateFill
替换为 setFieldValByName
:
@Override public void updateFill(MetaObject metaObject) { log.info("start update fill ...."); this.setFieldValByName("updateTime", Timestamp.valueOf(LocalDateTime.now()), metaObject); this.setFieldValByName("updateBy", userId, metaObject); }
原因分析补充:
strictInsertFill/strictUpdateFill
→ 插入/更新数据时填充,不适用于更新场景。setFieldValByName
→ 更新数据时直接覆盖字段值,即使原值存在。
2. 实体类注解配置错误
原因:
实体类中的字段没有正确标注 @TableField(fill = FieldFill.INSERT_UPDATE)
,导致自动填充机制无法生效。
解决方案:
在实体类中正确设置注解:
import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.annotation.TableFieldpython; import lombok.Data; import Java.time.LocalDateTime; @Data public class User { private Long id; private String name; @TableField(fill = FieldFill.INSERT) private LocalDateTime createTime; @TableField(fill = FieldFill.INSERT_UPDATE) private LocalDateTime updateTime; @TableField(fill = FieldFill.INSERT_UPDATE) private BigInteger updateBy; }
注意:
FieldFill.INSERT_UPDATE
:用于插入和更新时自动填充。- 如果没有加上这个注解,MyBatis Plus 不会执行自动填充逻辑。
3. MetaObjectHandler
未生效
原因:
MetaObjectHandler
需要被 Spring 管理。如果没有加上 @Component
注解或没有被扫描到,自动填充方法不会执行。
解决方案:
确保 FieldAutoFillHandler
类上有 @Component
注解:
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.reflection.MetaObject; import org.springframework.stereotype.Component; @Slf4j @Component public class FieldAutoFillHandler implements MetaObjectHandler { @Override public void insertFill(MetaObject metaObject) { log.info("执行插js入填充..."); this.setFieldValByName("createTime", LocalDateTime.now(), metaObject); 编程客栈 this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject); } @Override public void updateFill(MetaObject metaObject) { log.info("执行更新填充..."); this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject); } }
注意:
如果项目中有多个 MetaObjectHandler
,需要确保不会有冲突。
4. 使用自定义 SQL 导致自动填充失效
原因:
如果你在 Mapper.XML
中自定义了 update
语句,那么 MyBatis Plus 不会触发自动填充逻辑。
解决方案:
方法 1:在 XML 中手动设置 update_time
字段:
<update id="updateUser" parameterType="com.example.entity.User"> UPDATE user SET name = #{name}, update_time = NOW(), update_by = #{updateBy} WHERE id = #{id} </update>
方法 2:直接使用 BaseMapper 提供的 updateById() 等方法,MyBatis Plus 会自动触发填充:
User user = userMapper.selectById(1L); user.setName("李四"); userMapper.updateById(user);
5. 字段类型不匹配
原因:
数据库中 update_time
的数据类型与 Java 实体类的类型不匹配,导致填充失败。
解决方案:
确保数据库字段类型和实体类一致:
ALTER TABLE user MODIFY update_time TIMESTAMP NULL DEFAULT NULL;
private LocalDateTime updateTime;
注意:
- 使用
LocalDateTime
处理时间字段是最佳实践。 - 如果数据库字段为
datetime
或timestamp
,不要使用String
类型。
三、总结:常见原因与解决方法对照表
原因 | 现象 | 解决方法 |
---|---|---|
使用了 strictInsertFill | update_time 未更新 | 使用 setFieldValByName 替代 |
实体类注解错误 | 自动填充无效 | 确保字段上加了 @TableField(fill = FieldFill.INSERT_UPDATE) |
MetaObjectHandler 未生效 | updateFill() 不执行 | 确保加上 @Component 注解 |
使用自定义 SQL 覆盖了默认方法 | 填充逻辑未触发 | 使用 BaseMapper.updateById() 或在 SQL 中手动填充 |
数据类型不匹配 | 字段更新失败 | 数据库字段使用 timestamp 且实体类使用 LocalDateTime |
四、推荐写法
最终推荐的 updateFill()
写法如下:
@Override public void updateFill(MetaObject metaObject) { log.info("执行更新填充..."); this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject); this.setFieldValByName("updateBy", userId, metaObject); }
通过这种方式,确保 update_time
字段在更新时自动更新,同时避免了不必要的填充失效问题。
到此这篇关于MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)的文章就介绍到这了,更多相关MyBatis Plus update_time 自动填充失效内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论