Mybatis-plus MetaObjectHandler 自动字段数据补全更新
在我们向数据库插入一条数据的时候,少不了一些向createTime、updateTime此类字段,每次插入的数据都要设置这些个值,很烦,通过实现MetaObjectHandler接口重写insertFill、updateFill方法可以帮你摆脱烦恼
MetaObjectHandler接口是mybatisPlus为我们提供的的一个扩展接口,我们可以利用这个接口在我们插入或者更新数据的时候,为一些字段指定默认值。
1:编写MetaObjectHandler 实现类
编写类实现MetaObjectHandler接口,重写里面的方法就是了。
@Component
public class FieldMetaObjectHandler implements MetaObjectHandler {
private static final String CREATE_DATE = "createDate";
private static final String CREATE_DATETIME = "createDatetime";
private static final String CREATOR = "creator";
private static final String CREATOR_ID = "creatorId";
private static final String CREATOR_NAME = "creatorName";
private static final String UPDATE_DATE = "updateDate";
private static final String MODIFY_DATETIME = "modifyDatetime";
private static final String UPDATER = "updater";
private static final String MODIFER_ID = "modiferId";
private static final String MODIFER_NAME = "modiferName";
private static final String DEL_FLAG = "delFlag";
private static final String DEPT_ID = "deptId";
private static final String IS_REMOVED = "isRemoved";
public FieldMetaObjectHandler() {
}
public void insertFill(MetaObject metaObject) {
UserDetail user = SecurityUser.getUser();
if (user != null) {
Date date = new Date();
this.strictInsertFill(metaObject, "creator", Long.class, user.getId());
this.strictInsertFill(metaObject, "creatorId", Integer.class, user.getId() == null ? null : user.getId().intValue());
this.strictInsertFill(metaObject, "creatorName", String.class, user.getRealName());
this.strictInsertFill(metaObject, "createDate", Date.class, date);
this.strictInsertFill(metaObject, "createDatetime", Date.class, date);
this.strictInsertFill(metaObject, "deptId", Long.class, user.getDeptId());
this.strictInsertFill(metaObject, "updater", Long.class, user.getId());
this.strictInsertFill(metaObject, "updateDate", Date.class, date);
this.strictInsertFill(metaObject, "delFlag", Integer.class, DelFlagEnum.NORMAL.value());
this.strictInsertFill(metaObject, "isRemoved", String.class, "N");
}
}
public void updateFill(MetaObject metaObject) {
UserDetail user = SecurityUser.getUser();
if (user != null) {
this.strictUpdateFill(metaObject, "updater", Long.class, user.getId());
this.strictUpdateFill(metaObject, "modiferId", Integer.class, user.getId() == null ? null : user.getId().intValue());
this.strictUpdateFill(metaObject, "modiferName", String.class, user.getRealName());
this.strictUpdateFill(metaObject, "updateDate", Date.class, new Date());
this.strictUpdateFill(metaObject, "modifyDatetime", Date.class, new Date());
}
}
}
2:实体类上边加上@TableField(fill = FieldFill.INSERT_UPDATE)
指定进行属性填充的时机(更新、插入、或者更新和插入)
@Data
public abstract class BffBaseEntity implements Serializable {
/**
* 创建人
*/
@TableField(fill = FieldFill.INSERT)
private Integer creatorId;
/**
* 创建人
*/
@TableField(fill = FieldFill.INSERT)
private String creatorName;
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
private Date createDatetime;
/**
* 更新人
*/
@TableField(fill = FieldFill.UPDATE)
private Integer modiferId;
/**
* 更新人
*/
@TableField(fill = FieldFill.UPDATE)
private String modiferName;
/**
* 更新时间
*/
@TableField(fill = FieldFill.UPDATE)
private Date modifyDatetime;
/**
* 是否已删除
*/
@TableField(fill = FieldFill.INSERT)
private String isRemoved;
}
MetaObjectHandler