自动填充功能
有些时候我们可能会有这样的需求,插入或者更新数据时,希望有些字段可以自动填充数据,比如updateTime、updateUser等。
在Mybatis-Plus中提供了这样的功能,可以实现自动填充。
添加注解
编写MyMetaObjectHandler
MyMetaObjectHandler.java
package com.tian.handler;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
public class MyMetaObjectHandler implements MetaObjectHandler {
/**
* 插入数据时填充
*
* @param metaObject
*/
@Override
public void insertFill(MetaObject metaObject) {
// 先获取到password的值,再进行判断,如果为空,就进行填充,如果不为空,就不做处理
Object password = getFieldValByName("password", metaObject);
if (null == password) {
//字段为空,可以进行填充
setFieldValByName("password", "888888", metaObject);
}
}
/**
* 更新数据时填充
*
* @param metaObject
*/
@Override
public void updateFill(MetaObject metaObject) {
}
}
把MyMetaObjectHandler放入SpringIOC容器
@Bean
public MyMetaObjectHandler myMetaObjectHandler() {
return new MyMetaObjectHandler();
}
项目实战
公司是CustomMetaObjectHandler 继承MetaObjectHandler,然后CustomMetaObjectChemicalHandler 继承CustomMetaObjectHandler
实体类
package com.spang.partyManage.common.base;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.spang.core.tool.util.DateUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
import java.util.Date;
/**
* 基础实体类
*/
@Data
public class BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 主键id
*/
@JsonSerialize(using = ToStringSerializer.class)
@ApiModelProperty(value = "主键id")
@TableId(value = "id", type = IdType.ASSIGN_ID)
private Long id;
@ApiModelProperty("租户id")
@TableField(value = "tenant_id", fill = FieldFill.INSERT)
private String tenantId;
@ApiModelProperty("租户名称")
@TableField("tenant_name")
private String tenantName;
@ApiModelProperty("创建人")
@JsonSerialize(using = ToStringSerializer.class)
@TableField(value = "create_user", fill = FieldFill.INSERT)
private Long createUser;
@ApiModelProperty("创建人名称")
@TableField(value = "create_user_name", fill = FieldFill.INSERT)
private String createUserName;
@ApiModelProperty("创建部门")
@JsonSerialize(using = ToStringSerializer.class)
@TableField(value = "create_dept", fill = FieldFill.INSERT)
private Long createDept;
//@ApiModelProperty("创建部门名称")
//@TableField("create_dept_name")
//private String createDeptName;
@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;
}
属性自动填充配置
package com.spang.partyManage.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, "createTime", Date.class, new Date());
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, "updateTime", Date.class, new Date());
this.strictInsertFill(metaObject, "updateUser", Long.class, AuthUtil.getUser().getUserId());
this.strictInsertFill(metaObject, "updateUserName", String.class, AuthUtil.getUser().getRealName());
}
}
@Override
public void updateFill(MetaObject metaObject) {
if (Objects.nonNull(AuthUtil.getUser())) {
this.setFieldValByName("updateTime", new Date(), metaObject);
this.setFieldValByName("updateUser", AuthUtil.getUser().getUserId(), metaObject);
this.setFieldValByName("updateUserName", AuthUtil.getUser().getRealName(), 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());
}
}
}
mybatis属性配置
package com.spang.partyManage.config;
import com.spang.core.mp.handler.CustomMetaObjectHandler;
import com.spang.partyManage.handler.CustomMetaObjectChemicalHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
/**
* @author ZC
* @date 2020/8/9 18:51
*/
@Configuration
public class MyBatisConfig {
@Bean
@Primary
public CustomMetaObjectHandler customMetaObjectHandler1() {
return new CustomMetaObjectChemicalHandler();
}
}