BaseEntity
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class BaseEntity {
@ApiModelProperty(value = "创建时间")
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
@ApiModelProperty(value = "更新时间")
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
}
实现mybatisplus自动填充器
@Component
@Slf4j
public class AutoCreateTime implements MetaObjectHandler {
@ApiModelProperty("创建时间")
final String CREATE_TIME = "createTime";
@ApiModelProperty("更新时间")
final String UPDATE_TIME = "updateTime";
@Override
public void insertFill(MetaObject metaObject) {
boolean hasCreateTimeField = metaObject.hasSetter(CREATE_TIME);
if (hasCreateTimeField) {
final LocalDateTime now = LocalDateTime.now();
strictInsertFill(metaObject, CREATE_TIME, LocalDateTime.class, now);
strictUpdateFill(metaObject, UPDATE_TIME, LocalDateTime.class, now);
}
}
@Override
public void updateFill(MetaObject metaObject) {
boolean hasUpdateTime = metaObject.hasSetter(UPDATE_TIME);
Object val = getFieldValByName(UPDATE_TIME, metaObject);
if (val == null && hasUpdateTime) {
strictUpdateFill(metaObject, UPDATE_TIME, LocalDateTime.class, LocalDateTime.now());
}
}
}