当前使用版本(必填,否则不予处理)

  • 当前使用版本,V3.5.5 目前最新版本
  • 如题,自动填充功能,字段使用@TableField(fill = FieldFill.UPDATE)
  • 当字段为空时,注入成功,但是当字段有值时,注入失败,这种是正常的吗
  • 比如我想更新的时候,FieldFill.UPDATE 标注的字段,一直走自动注入最新的,比如更新时间
  • 如下是代码片段
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.5</version>
</dependency>
// 此类已注入
public class DefaultFieldAutoFillHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        try {
            this.strictInsertFill(metaObject, "createBy", Long.class, SecurityContextHolder.getUserId());
            this.strictInsertFill(metaObject, "updateBy", Long.class, SecurityContextHolder.getUserId());
            this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
            this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        } catch (Exception e) {
            throw new ServiceException("insertFill-自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        try {
            this.strictUpdateFill(metaObject, "updateBy", Long.class, SecurityContextHolder.getUserId());
            this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        } catch (Exception e) {
            throw new ServiceException("updateFill-自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
        }
    }
}
  • 实体类使用注解:
@TableField(fill = FieldFill.UPDATE)
private LocalDateTime updateTime;

该问题是如何引起的?(确定最新版也有问题再提!!!)

  • 直接调用 updateById(entity) 方法
  • 当字段为空时,注入成功,但是当字段有值时,注入失败,这种是正常的吗
  • 字段使用的是 @TableField(fill = FieldFill.UPDATE)

重现步骤(如果有就写完整)

  • 直接调用 updateById(entity) 方法

报错信息

  • 无报错

述求

  • 我想在更新的时候,FieldFill.UPDATE 标注的字段,一直走自动填充最新的,比如更新时间
  • 是否可以在 MetaObjectHandler 配置

Comment From: nieqiurong

setFieldValByName

Comment From: rstyro

setFieldValByName

懂了懂了