Use @ TableField in MyBatisPlus to complete the operation of field automatic filling

  • 2021-08-12 02:58:28
  • OfStack

Scene

Official documents:

Field comment @ TableField


com.baomidou.mybatisplus.annotations.TableField
描述
value 字段值(驼峰命名方式,该值可无)
update 预处理 set 字段自定义注入
condition 预处理 WHERE 实体条件自定义运算规则
el 详看注释说明
exist 是否为数据库表字段( 默认 true 存在,false 不存在 )
strategy 字段验证 ( 默认 非 null 判断,查看 com.baomidou.mybatisplus.enums.FieldStrategy )
fill 字段填充标记 ( FieldFill, 配合自动填充使用 )

Field Fill Policy FieldFill

描述
DEFAULT 默认不处理
INSERT 插入填充字段
UPDATE 更新填充字段
INSERT_UPDATE 插入和更新填充字段

Realization

There are the following attributes in the entity class. With the above auto-fill attribute, we can implement the

When the insert operation is performed, the

Added annotation @ TableField (fill = FieldFill. INSERT)

To automatically populate the fields of.

Annotated @ TableField (fill = FieldFill.INSERT_UPDATE)

Is automatically populated when inserting and updating.


 /**
   *  Founder 
   */
  @TableField(fill = FieldFill.INSERT)
  private Long creatorId;
 
  /**
   *  Creation time 
   */
  @TableField(fill = FieldFill.INSERT)
  private Date gmtCreat;
 
  /**
   *  Modifier 
   */
  @TableField(fill = FieldFill.INSERT_UPDATE)
  private Long modifierId;
 
  /**
   *  Modification time 
   */
  @TableField(fill = FieldFill.INSERT_UPDATE)
  private Date gmtModified;
 
  /**
   *  Available 
   */
  @TableField(fill = FieldFill.INSERT)
  private Boolean availableFlag;

In this way, when we assign values to entity classes in specific business, we can automatically assign values and insert them into the database when inserting or updating without assigning values to these public fields.

So where are the values to be assigned automatically configured?

Create a new auto-fill processing class under the config package of the project to implement the interface MetaObjectHandler

And override its methods:


package com.ws.api.config;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.util.Date;
 
/**
 *  Autopopulate processing class 
 * @author badao
 * @version 1.0
 * @see
 **/
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
  @Override
  public void insertFill(MetaObject metaObject) {
    this.setFieldValByName("modifierId", new Long(111), metaObject);
    this.setFieldValByName("gmtModified", new Date(), metaObject);
    this.setFieldValByName("creatorId", new Long(111), metaObject);
    this.setFieldValByName("gmtCreat",new Date(), metaObject);
    this.setFieldValByName("availableFlag",true, metaObject);  
  }
 
  @Override
  public void updateFill(MetaObject metaObject) {
    this.setFieldValByName("modifierId", new Long(111), metaObject);
    this.setFieldValByName("gmtModified", new Date(), metaObject);
  }
}

The first of the method parameters is the field corresponding to the previous auto-populate, and the second is the value to be automatically populated.

Supplement: configuration class of @ TableField annotation automatic assignment function in Mybatis-Plus


package com.jt.auto;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.util.Date;
// Complete the auto-fill function 
@Component // Give the object to the spring Container management 
public class MyMetaObjectHandler implements MetaObjectHandler {
  /**
   *  In POJO Added to the   Add / Updated comments , However, the assignment must be completed in the fields of the database .
   *  So . Must be clear , Add / Which field is operated on when updating , What is the sum value 
   * * * @param metaObject
   */
  @Override
  public void insertFill(MetaObject metaObject) {
    this.setInsertFieldValByName("created", new Date(), metaObject);
    this.setInsertFieldValByName("updated", new Date(), metaObject);
  }
  @Override
  public void updateFill(MetaObject metaObject) {
    this.setUpdateFieldValByName("updated", new Date(), metaObject);
  }
}

@ TableField annotation completes auto-populate assignment use case:


package com.jt.pojo;
import java.io.Serializable;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import lombok.experimental.Accessors;
//pojo Base class, complete 2 Tasks, 2 Date, serialization 
@Data
@Accessors(chain=true)
public class BasePojo implements Serializable{
 @TableField(fill = FieldFill.INSERT) // Add effective 
 private Date created;
 @TableField(fill = FieldFill.INSERT_UPDATE) // Add and Update Valid 
 private Date updated;
}

Related articles: