1. 添加注解
@TableField(fill = FieldFill.INSERT) //新增时调用
@TableField(fill = FieldFill.INSERT_UPDATE) //新增或更新时调用
@ApiModelProperty(value = "创建时间")
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@ApiModelProperty(value = "更新时间")
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
2. 配置拦截器
注意:启动类必须能扫描到当前类 才可以使用自动填充
@Component
public class SrbTimeHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
if(metaObject.hasSetter("createTime")){
metaObject.setValue("createTime",new Date());
}
if(metaObject.hasSetter("updateTime")){
metaObject.setValue("updateTime",new Date());
}
}
@Override
public void updateFill(MetaObject metaObject) {
if(metaObject.hasSetter("updateTime")){
metaObject.setValue("updateTime",new Date());
}
}
}