实体类中有如下属性,通过上面的自动填充属性,我们可以实现在进行插入(insert)操作时对添加了注解@TableField(fill = FieldFill.INSERT)的字段进行自动填充(解释:后面会写配置自动填充的配置类,该配置类的作用用于配置自动填充的值)。
对添加了注解@TableField(fill = FieldFill.INSERT_UPDATE)的字段在进行插入(insert)和更新(update)时进行自动填充。(解释:后面会写配置自动填充的配置类,该配置类的作用用于配置自动填充的值)。
@ApiModelProperty("创建时间")
@TableField(value = "create_time", fill = FieldFill.INSERT)
@DateTimeFormat(pattern = DateUtil.PATTERN_DATETIME)
@JsonFormat(pattern = DateUtil.PATTERN_DATETIME)
private Date createTime;
@ApiModelProperty("修改人")
@TableField(value = "update_user", fill = FieldFill.INSERT_UPDATE)
private Long updateUser;
@ApiModelProperty("修改用户名称")
@TableField(value = "update_user_name", fill = FieldFill.INSERT_UPDATE)
private String updateUserName;
@ApiModelProperty("修改时间")
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
@DateTimeFormat(pattern = DateUtil.PATTERN_DATETIME)
@JsonFormat(pattern = DateUtil.PATTERN_DATETIME)
private Date updateTime;
@ApiModelProperty("是否已删除")
@TableField("is_deleted")
@TableLogic
private Integer isDeleted;
这样我们在具体业务中对实体类进行赋值就可以不用对这些公共字段进行赋值,在执行插入或者更新时就能自动赋值并插入数据库。
那么要自动赋的值在哪里配置?
在项目的config包下新建自动填充处理类使其实现接口MetaObjectHandler,接下来我们来写自动赋值的配置类,并重写其方法:
package com.spang.monitor.common.handler;
import com.spang.core.auth.util.AuthUtil;
import com.spang.core.mp.handler.CustomMetaObjectHandler;
import com.spang.core.tool.util.Func;
import org.apache.ibatis.reflection.MetaObject;
import java.util.Date;
import java.util.Objects;
public class CustomMetaObjectChemicalHandler extends CustomMetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
if (Objects.nonNull(AuthUtil.getUser())) {
this.strictInsertFill(metaObject, "tenantId", String.class, AuthUtil.getTenantId());
this.strictInsertFill(metaObject, "createUser", Long.class, AuthUtil.getUser().getUserId());
this.strictInsertFill(metaObject, "createUserName", String.class, AuthUtil.getUser().getRealName());
this.strictInsertFill(metaObject, "createDept", Long.class, Func.firstLong(AuthUtil.getUser().getDeptId()));
this.strictInsertFill(metaObject, "updateUser", Long.class, AuthUtil.getUser().getUserId());
this.strictInsertFill(metaObject, "updateUserName", String.class, AuthUtil.getUser().getRealName());
}
this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
}
@Override
public void updateFill(MetaObject metaObject) {
if (Objects.nonNull(AuthUtil.getUser())) {
this.setFieldValByName("updateUser", AuthUtil.getUser().getUserId(), metaObject);
this.setFieldValByName("updateUserName", AuthUtil.getUser().getRealName(), metaObject);
}
this.setFieldValByName("updateTime", new Date(), metaObject);
}
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.spang.core.mp.handler;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.spang.core.auth.util.AuthUtil;
import com.spang.core.tool.util.Func;
import java.util.Date;
import org.apache.ibatis.reflection.MetaObject;
public class CustomMetaObjectHandler implements MetaObjectHandler {
public CustomMetaObjectHandler() {
}
public void insertFill(MetaObject metaObject) {
this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
if (AuthUtil.getUser() != null) {
this.strictInsertFill(metaObject, "createUser", Long.class, AuthUtil.getUser().getUserId());
this.strictInsertFill(metaObject, "createDept", Long.class, Func.toLong(AuthUtil.getUser().getDeptId()));
}
}
public void updateFill(MetaObject metaObject) {
this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
if (AuthUtil.getUser() != null) {
this.strictInsertFill(metaObject, "updateUser", Long.class, AuthUtil.getUser().getUserId());
}
}
}