1、创建时间和更新时间在数据库表层面就可以解决
`create_time` datetime DEFAULT NULL CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
2、创建人和更新人赋值操作
MetaObjectHandler接口是mybatisPlus为我们提供的的一个扩展接口,我们可以利用这个接口在我们插入或者更新数据的时候,为一些字段指定默认值。
使用场景:公共字段填充等,如updateTime、createTime、createUser、updateUser等公共字段多的填充。
@Data
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String username;
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
@TableField(fill = FieldFill.INSERT)
private Long createUser;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Long updateUser;
}
2、自定义元对象处理器类,实现metaObjecthandler接口,重写insertFill、updateFill方法;
1)在每次执行插入和修改操作时,会执行insertFill,updateFill方法;
2)setValue(String name,Object value) : 设置公共字段填充值,第一个参数值name与实体字段要相同
import cn.hutool.core.util.ObjectUtil;
import