在实际开发中使用Mybatis Plus会遇到插入数据的时候,比如每条数据插入创建人ID时间等,使用Mybatis Plus的FieldFill就会很方便了。
1、创建 CustomMetaObjectHandler 实现 CustomMetaObjectHandler接口
public class CustomMetaObjectHandler implements MetaObjectHandler {
public CustomMetaObjectHandler() {
}
public void insertFill(MetaObject metaObject) {
Object delFlag = null;
try {
delFlag = this.getFieldValByName(this.getDeleteFlagFieldName(), metaObject);
if (delFlag == null) {
this.setFieldValByName(this.getDeleteFlagFieldName(), this.getDefaultDelFlagValue(), metaObject);
}
} catch (ReflectionException var8) {
}
Object createTime = null;
try {
createTime = this.getFieldValByName(this.getCreateTimeFieldName(), metaObject);
if (createTime == null) {
this.setFieldValByName(this.getCreateTimeFieldName(), new Date(), metaObject);
}
} catch (ReflectionException var7) {
}
Object createUser = null;
try {
createUser = this.getFieldValByName(this.getCreateUserFieldName(), metaObject);
if (createUser == null) {
Object accountId = this.getUserUniqueId();
this.setFieldValByName(this.getCreateUserFieldName(), accountId, metaObject);
}
} catch (ReflectionException var6) {
}
}
public void updateFill(MetaObject metaObject) {
try {
this.setFieldValByName(this.getUpdateTimeFieldName(), new Date(), metaObject);
} catch (ReflectionException var5) {
}
Object updateUser = null;
try {
updateUser = this.getFieldValByName(this.getUpdateUserFieldName(), metaObject);
if (updateUser == null) {
Object accountId = this.getUserUniqueId();
this.setFieldValByName(this.getUpdateUserFieldName(), accountId, metaObject);
}
} catch (ReflectionException var4) {
}
}
protected String getDeleteFlagFieldName() {
return "delFlag";
}
protected Object getDefaultDelFlagValue() {
return "N";
}
protected String getCreateTimeFieldName() {
return "createTime";
}
protected String getCreateUserFieldName() {
return "createUser";
}
protected String getUpdateTimeFieldName() {
return "updateTime";
}
protected String getUpdateUserFieldName() {
return "updateUser";
}
protected Object getUserUniqueId() {
return "";
}
}
2、创建MyMetaObjectHandler 继承 CustomMetaObjectHandler
public class MyMetaObjectHandler extends CustomMetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
Object delFlag = null;
try {
delFlag = this.getFieldValByName(this.getDeleteFlagFieldName(), metaObject);
if (delFlag == null) {
this.setFieldValByName(this.getDeleteFlagFieldName(), this.getDefaultDelFlagValue(), metaObject);
}
} catch (ReflectionException e) {
}
Object createTime = null;
try {
createTime = this.getFieldValByName(this.getCreateTimeFieldName(), metaObject);
if (createTime == null) {
this.setFieldValByName(this.getCreateTimeFieldName(), new Date(), metaObject);
}
} catch (ReflectionException e) {
}
Object createUser = null;
try {
createUser = this.getFieldValByName(this.getCreateUserFieldName(), metaObject);
if (createUser == null) {
Object accountId = this.getCreateUserValue();
this.setFieldValByName(this.getCreateUserFieldName(), accountId, metaObject);
}
} catch (ReflectionException e) {
}
//此处为扩展填充的字段
Object deptId = null;
try {
//字段值
deptId = this.getFieldValByName(this.getDeptIdFieldName(), metaObject);
if (deptId == null) {
//获取字段值的方法 在PluginsConfig中重写
Object accountId = this.getDeptIdValue();
this.setFieldValByName(this.getDeptIdFieldName(), accountId, metaObject);
}
} catch (ReflectionException e) {
}
}
@Override
public void updateFill(MetaObject metaObject) {
try {
this.setFieldValByName(this.getUpdateTimeFieldName(), new Date(), metaObject);
} catch (ReflectionException e) {
}
Object updateUser = null;
try {
updateUser = this.getFieldValByName(this.getUpdateUserFieldName(), metaObject);
if (updateUser == null) {
Object accountId = this.getUpdateUserValue();
this.setFieldValByName(this.getUpdateUserFieldName(), accountId, metaObject);
}
} catch (ReflectionException e) {
}
}
protected String getDeptIdFieldName() {
return "deptId";
}
protected Object getCreateUserValue() {
return "";
}
protected Object getUpdateUserValue() {
return "";
}
protected Object getDeptIdValue() {
return "";
}
}
3、创建PluginsConfig 配置
@Configuration
@MapperScan(basePackages = {"cn.whzl.**.mapper"})
public class PluginsConfig {
/**
* 拓展核心包中的字段包装器
*
*/
@Bean
public MyMetaObjectHandler myMpFiledHandler() {
return new MyMetaObjectHandler() {
//配置获取当前登录用户的部门id返回
@Override
protected Long getDeptIdValue() {
try {
return LoginContextHolder.getContext().getUser().getDeptId();
} catch (Exception e) {
//如果获取不到当前用户的deptId就存空id
return -100L;
}
}
@Override
protected Long getCreateUserValue() {
try {
return LoginContextHolder.getContext().getUser().getId();
} catch (Exception e) {
//如果获取不到当前用户就存空id
return -100L;
}
}
@Override
protected Long getUpdateUserValue() {
try {
return LoginContextHolder.getContext().getUser().getId();
} catch (Exception e) {
//如果获取不到当前用户就存空id
return -100L;
}
}
};
}
}
4、在创建基础实体类时使用(@TableField(value = “deptId”,fill = FieldFill.INSERT))即可
public class BaseEntity<T extends BaseEntity<?>> extends Model<T> implements Serializable{
/**
* 部门id
*/
@TableField(value = "deptId",fill = FieldFill.INSERT)
protected Long deptId;
/**
* 部门id
*/
@TableField("menuId")
protected Long menuId;
/**
* 创建人
*/
@TableField(value = "create_user",fill = FieldFill.INSERT)
protected Long createUser;
/**
* 创建时间
*/
@TableField(value = "create_time", fill = FieldFill.INSERT)
protected Date createTime;
/**
* 更新人
*/
@TableField(value = "update_user",fill = FieldFill.UPDATE)
protected Long updateUser;
/**
* 更新时间
*/
@TableField(value = "update_time", fill = FieldFill.UPDATE)
protected Date updateTime;
/**
* 1:可用 0:不可用
*/
@TableField("isEnable")
@TableLogic(value = "1", delval = "0")
protected Integer isEnable;
public void asCrUser() {
setCreateUser(LoginContextHolder.getContext().getUserId());
}
public void asUpUser() {
setUpdateUser(LoginContextHolder.getContext().getUserId());
}
public void asDeptId() {
setDeptId(LoginContextHolder.getContext().getUser().getDeptId());
}
public Long getCreateUser() {
return createUser;
}
public void setCreateUser(Long createUser) {
this.createUser = createUser;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Long getUpdateUser() {
return updateUser;
}
public void setUpdateUser(Long updateUser) {
this.updateUser = updateUser;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public Integer getIsEnable() {
return isEnable;
}
public void setIsEnable(Integer isEnable) {
this.isEnable = isEnable;
}
public Long getDeptId() {
return deptId;
}
public void setDeptId(Long deptId) {
this.deptId = deptId;
}
public Long getMenuId() {
return menuId;
}
public void setMenuId(Long menuId) {
this.menuId = menuId;
}
@Override
public String toString() {
return "BaseEntity{" +
"createUser=" + createUser +
", createTime=" + createTime +
", updateUser=" + updateUser +
", updateTime=" + updateTime +
", isEnable=" + isEnable +
'}';
}
}