1.第一步:配置类
package com.laoyang.Config;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* @author:Kevin
* @create: 2022-08-30 10:23
* @Description: 自动填充时间
*/
@Component
public class CommonMetaObjectHandler implements MetaObjectHandler {
/**
* 新增时自动填充
* @param metaObject
*/
@Override
public void insertFill(MetaObject metaObject) {
//参数1:数据对象,就是原数据
//参数2:属性名称,一定要和实体类名称一致
//参数3:类对象
//参数4:当前时间
this.strictInsertFill(metaObject,"createTime", Date.class,new Date());
this.strictInsertFill(metaObject,"updateTime", Date.class,new Date());
}
/**
* 修改
* @param metaObject
*/
@Override
public void updateFill(MetaObject metaObject) {
this.strictInsertFill(metaObject,"updateTime", Date.class,new Date());
}
}
2.第二步:在填充的实体类上添加注解
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date createTime;
/**
* 修改时间
*/
@TableField(fill = FieldFill.UPDATE)
private Date updateTime;