Implementation of Mybatis Plus Automatic Filling and Update Operation Related Fields
- 2021-12-11 17:56:03
- OfStack
It is stated in the table building protocol of Ali Development Manual that there should be create_time and update_time fields in the database table; Then, in the development, the processing of these common fields should be unified, which can simplify our development process. Then this article records the automatic field filling in Mybatis-Plus.
Scene introduction
In the data table design of the project, each table has four fields: create_id, create_time, update_id and update_time, so in the application of Java, these four fields should be changed accordingly for the addition and modification operations:
create_id
,
update_id
To automatically populate ID as the currently logged-in user
create_time
,
update_time
To automatically populate as the current time
Let's talk about the code implementation:
Autofill processor
The MetaObjectHandler interface in Mybatis-Plus needs to be implemented
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import hw.topevery.basis.runtime.UserRuntime;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.Objects;
/**
* Update related field handler
*
* @author whw
* @date 2020/1/3 16:41
*/
@Component
public class UpdateRelatedFieldsMetaHandler implements MetaObjectHandler {
/**
* Add operation
*
* @param metaObject
*/
@Override
public void insertFill(MetaObject metaObject) {
this.strictInsertFill(metaObject, "createId", String.class, getCurrentUserId());
this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
this.strictInsertFill(metaObject, "updateId", String.class, getCurrentUserId());
this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
}
/**
* Update operation
*
* @param metaObject
*/
@Override
public void updateFill(MetaObject metaObject) {
this.strictUpdateFill(metaObject, "updateId", String.class, getCurrentUserId());
this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
}
/**
* Get the current logged-in user ID
*
* @return
*/
private String getCurrentUserId() {
return " Current Login User ID";
}
}
Mybatis-Plus configuration class
The processor needs to be injected into the Spring container to take effect
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import hw.topevery.kunming.wapi.handler.UpdateRelatedFieldsMetaHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Mybatis-Plus Configuration class
*
* @author whw
* @date 2020/1/3 16:41
*/
@Configuration
public class MybatisPlusConfig {
@Bean
public GlobalConfig globalConfig() {
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setMetaObjectHandler(new UpdateRelatedFieldsMetaHandler());
return globalConfig;
}
}
Configure auto-population policies for related fields in entity classes
Setting the fill property in the @ TableField annotation
Field Fill Policy FieldFill Description
值 | 描述 |
---|---|
DEFAULT | 默认不处理 |
INSERT | 插入填充字段 |
UPDATE | 更新填充字段 |
INSERT_UPDATE | 插入和更新填充字段 |
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* Basic entity class
*
* @author whw
* @date 2020/1/3 17:37
*/
@Data
public class BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = " Create a user ID")
@TableField(value = "c_create_id", fill = FieldFill.INSERT)
private String createId;
@ApiModelProperty(value = " Creation time ")
@TableField(value = "c_create_time", fill = FieldFill.INSERT)
private LocalDateTime createTime;
@ApiModelProperty(value = " Update users ID")
@TableField(value = "c_update_id", fill = FieldFill.INSERT_UPDATE)
private String updateId;
@ApiModelProperty(value = " Update time ")
@TableField(value = "c_update_time", fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
}
Here, I extracted a parent class from the entity. Of course, I can also add annotations to the corresponding fields of the entity class directly.