SoFunction
Updated on 2025-04-10

Mybatis-Plus uses MetaObjectHandler to achieve automatic filling of entity object fields

1. Description

Some simple CRUDs when we use Mybatis-Plus. You will find many tables, and many fields are repeated, such as: createTime, updateTime, createBy, updateBy and other fields. If we have to manually assign values ​​every time we update or add, then many unnecessary repetitions will occur. So is there a solution for Mybatis-Plus that can reduce this repetitive operation? Of course there is, that is MetaObjectHandler (metaObject Processing).

Main functions

  • Autofill: You can automatically fill in the specified field value when inserting or updating the database record. This is very useful for the record creation time and the last update time.
  • Flexible configuration: You can specify which fields need to be automatically filled and under what circumstances (such as when inserting only, or when inserting and updating) through annotation or global configuration.
  • Simple integration: you only need to implement the MetaObjectHandler interface and rewrite the corresponding method. At the same time, in Spring Boot applications, you can take effect automatically by marking its implementation class as @Component

2. Coding

2.1 Defining entity classes

First define an entity class and use@TableFieldAnnotation to specify which fields need to be automatically filled. For example:

package ;
 
import .*;
import ;
 
import ;
import ;
 
public class BaseModel extends IdModel {
    @TableField(fill = )
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime createTime;
 
    @TableField(fill = )
    private String createBy;
 
    @TableField(fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime updateTime;
 
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private String updateBy;
 
    @Version
    @TableField(fill = )
    private Integer version;
 
    @TableLogic
    @TableField(fill = )
    private Integer deleted;
 
    public String getCreateBy() {
        return createBy;
    }
 
    public void setCreateBy(String createBy) {
         = createBy;
    }
 
    public String getUpdateBy() {
        return updateBy;
    }
 
    public void setUpdateBy(String updateBy) {
         = updateBy;
    }
 
    public Integer getDeleted() {
        return deleted;
    }
 
    public void setDeleted(Integer deleted) {
         = deleted;
    }
 
    public Integer getVersion() {
        return version;
    }
 
    public void setVersion(Integer version) {
         = version;
    }
 
    public LocalDateTime getCreateTime() {
        return createTime;
    }
 
    public void setCreateTime(LocalDateTime createTime) {
         = createTime;
    }
 
    public LocalDateTime getUpdateTime() {
        return updateTime;
    }
 
    public void setUpdateTime(LocalDateTime updateTime) {
         = updateTime;
    }
}

2.2 Implementing MetaObjectHandler

Create a class implementationMetaObjectHandlerInterface and overwriteinsertFillandupdateFillmethod. In this method, it is determined whether to fill and what value to fill according to the properties of the field. For example:

package ;
 
import ;
import ;
import ;
 
import ;
import ;
 
@Component
public class DateMetaObjectHandler implements MetaObjectHandler {
    private String createTimeField = "createTime";
    private String updateTimeField = "updateTime";
    private String createByField = "createBy";
    private String updateByField = "updateBy";
    private String versionField = "version";
    private String deletedField = "deleted";
    private String appIdField = "appId";
    private String tenantIdField = "tenantId";
    private String instanceIdField = "instanceId";
 
    @Override
    public void insertFill(MetaObject metaObject) {
        (metaObject, createTimeField, , ());
        (metaObject, updateTimeField, , ());
        (metaObject, versionField, , 0);
        (metaObject, deletedField, , 0);
 
        (metaObject, createByField, , ((()).orElse(0L)));
        (metaObject, updateByField, , ((()).orElse(0L)));
 
//        (metaObject, appIdField, , ());
//        (metaObject, tenantIdField, , ());
//        (metaObject, instanceIdField, , ());
    }
 
 
    @Override
    public void updateFill(MetaObject metaObject) {
        setFieldValByName(updateTimeField, (), metaObject);
        setFieldValByName(updateByField, ((()).orElse(0L)), metaObject);
 
    }
}

2.3 Configuring MyBatis-Plus

Make sure that MyBatis-Plus is configured correctly in your project. Normally, if you are using Spring Boot, you just need to add the MyBatis-Plus dependency and inorJust make basic configurations.

3. Summary

  • When you achieveMetaObjectHandlerAnd hopefully when it takes effect, make sure that the class has been managed by Spring container (i.e., plus@ComponentAnnotation).
  • If you have multiple projectsMetaObjectHandlerImplementation, they will be executed in order of Spring Bean loading.
  • In usestrictInsertFillandstrictUpdateFillWhen method, make sure that the type passed matches the type of the field in the entity class, otherwise an exception may be thrown.
  • In this way, MyBatis-Plus can greatly simplify data operations during the development process, reduce the workload of manually setting field values, and improve development efficiency.

This is the article about Mybatis-Plus using MetaObjectHandler to automatically fill entity object fields. For more related contents of Mybatis-Plus automatically fill object fields, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!