一、给要自动填充的字段加注解
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
/**
* 修改时间
*/
@TableField(fill = FieldFill.UPDATE)
private LocalDateTime updateTime;
二、实现MetaObjectHandler
/**
* @author beth
* @data 2019-10-22 08:38
*/
@Component
public class MyMetaObjcetHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
boolean hasSetter = metaObject.hasSetter("createTime1");
if(hasSetter){
setInsertFieldValByName("createTime", LocalDateTime.now(),metaObject);
}
}
@Override
public void updateFill(MetaObject metaObject) {
Object val = getFieldValByName("updateTime",metaObject);
if(val == null){
setUpdateFieldValByName("updateTime",LocalDateTime.now(),metaObject);
}
}
}
来源:oschina
链接:https://my.oschina.net/u/2427561/blog/3121370