SoFunction
Updated on 2025-03-08

MybatisPlus' MetaObjectHandler is used with @TableLogic

Implement the automatic filling function of public fields

1.1 Public fields in daily development

During the daily development process,DAOThe database entity class of the layer (EntityThere are often some public fields in ) such ascreate_by(creator),create_timeFields such as (creation time) are basically unrelated to the business and belong to the property fields that belong to the record itself.

These fields are usually easily overlooked by everyone, and it is more troublesome and time-consuming to process each Entity; if ignored, it will cause the record itself to be incomplete, and it will not regret it until it is used.

1.2 Solutions in Mybatis Plus

soMybatis PlusIn-houseMetaObjectHandlerThis is a good solution to this problem.

MetaObjectHandlerThere are two main methods:

public interface MetaObjectHandler {
    
    /**
      * Insert meta-object field fill (used to fill common fields when inserting)
      *
      * @param metaObject
      */
    void insertFill(MetaObject metaObject);

    /**
      * Update meta-object field fill (used to fill public fields when updating)
      *
      * @param metaObject
      */
    void updateFill(MetaObject metaObject);
}

1.3 Usage

1.3.1 Define the public field superclass and add annotations on the field

Generally speaking, we will encapsulate these public fields in oneSuper EntityIn the class, so in this class, we add all fields that need to be changed when updating and inserting@TableFieldAnnotation and setfillproperty.

Give an example:

@Data
public class BaseEntity implements Serializable {
    
    /**
      * Creation time
      */
    @TableField(value = "create_time", fill = )
    private Date createTime;

    /**
      * Update time
      */
    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

}

FieldFillIt is an enumeration class that defines four processing methods:

public enum FieldFill {
    /**
      * Not processed by default
      */
    DEFAULT,
    /**
      * Fill in fields when inserting
      */
    INSERT,
    /**
      *Fill in fields when updating
      */
    UPDATE,
    /**
      * Fill in fields when inserting and updating
      */
    INSERT_UPDATE
}

1.3.2 Implementing the MetaObjectHandler interface

Define a class implementationMetaObjectHandlerIn-houseinsertFillandupdateFillAbstract method:

public class MetaHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        // The getFieldValByName method is the default method in the parent interface. Use the field name to obtain the value of the specified name field in the object to be inserted through reflection using the field name to be inserted through reflection.        Object createTime = getFieldValByName("createTime", metaObject);
        if ((createTime)) {
            //Similarly, the setFieldValByName method is also the default method in the parent interface. Use the field name to reflect the value of the specified name field in the object to be inserted through reflection.            //When inserting, the createTime field defaults to the current time            setFieldValByName("createTime", new Date(), metaObject);
        }
        Object updateTime = getFieldValByName(UPDATE_TIME_FIELD, metaObject);
        if ((updateTime)) {
            setFieldValByName(UPDATE_TIME_FIELD, new Date(), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        //Set the updateTime field to the current time when updating        setFieldValByName(UPDATE_TIME_FIELD, (), metaObject);
    }
}

2. Mybatis Plus implements logical deletion

2.1 Current logical deletion

In daily development, data in most scenarios does not require physical deletion, but uses a field to indicate whether this field is deleted, that is, logical deletion.

Normally, we need to manually update this value to the deleted enum value when deleting.

2.2 Solutions provided by Mybatis Plus

existMybatis PlusAmong them, a@TableLogicAnnotation, the code is as follows:

@Documented
@Retention()
@Target()
public @interface TableLogic {

    /**
      * The default logic does not delete the value (this value can be ignored and the global configuration will be automatically obtained)
      */
    String value() default "";

    /**
      * Default logical deletion value (this value can be ignored and will automatically obtain global configuration)
      */
    String delval() default "";
}

So when we need to use logically deleted fields, we can use the following method:

    @TableLogic(delval = "1", value = "0")
    private String delete;

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.