社区专栏>mybatis-plus,MetaObjectHandler#updateFill不生效的原因排查>
原创
mybatis-plus,MetaObjectHandler#updateFill不生效的原因排查
场景:
a.配置MetaObjectHandler
@Configuration
public class EntityAutoFilledHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
Date now = new Date();
this.setFieldValByName("createdTime", now, metaObject);
this.setFieldValByName("updatedTime", now, metaObject);
}
@Override
public void updateFill(MetaObject metaObject) {
Date now = new Date();
this.setFieldValByName("updatedTime", now, metaObject);
}
}
b.定义entity
@Data
@TableName(value = "sys_user")
public class SysUser {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
@TableField(fill = FieldFill.INSERT)
private Date createdTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updatedTime;
}
c.调用update
sysUserDAO.lambdaUpdate().set(SysUser::getAge, 21).eq(SysUser::getName, "tom").update();
问题:
调用成功后,sys_user表中对应记录更新成功,但updatedTime字段没有更新
排查:
经过debug,来到MybatisParameterHandler#process.
if (tableInfo != null) {
//到这里就应该转换到实体参数对象了,因为填充和ID处理都是针对实体对象处理的,不用传递原参数对象下去.
MetaObject metaObject = this.configuration.newMetaObject(entity);
if (SqlCommandType.INSERT == this.sqlCommandType) {
populateKeys(tableInfo, metaObject, entity);
insertFill(metaObject, tableInfo);
} else {
updateFill(metaObject, tableInfo);
}
}
结论:
update()没有传entity,获取不到tableInfo,也就无法触发updateFill方法