How Mybatis-Plus automatically populates fields related to update operation
Editor to share with you how Mybatis-Plus automatically fill in the update operation related fields, I believe that most people do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Scene introduction
In the data table design of the project, each table has four fields: create_id, create_time, update_id and update_time. In the Java application, these four fields should be changed accordingly for new and modified operations:
Create_id and update_id are to be automatically populated as the currently logged-in user ID
Create_time and update_time should be automatically populated as the current time.
Let's talk about the code implementation:
Auto-populate processor
Need to implement the MetaObjectHandler interface in Mybatis-Plus
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 processor * * @ author whw * @ date 16:41 on 2020-1-3 * / @ Componentpublic 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 login 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 * / @ Configurationpublic class MybatisPlusConfig {@ Bean public GlobalConfig globalConfig () {GlobalConfig globalConfig = new GlobalConfig () GlobalConfig.setMetaObjectHandler (new UpdateRelatedFieldsMetaHandler ()); return globalConfig;}} configure the automatic filling policy for related fields in the entity class
Set the fill property in the @ TableField annotation
Field filling Policy FieldFill description
Value description DEFAULT does not handle INSERT insert fill fields UPDATE update fill fields INSERT_UPDATE insert and update fill fields 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 by default / * basic entity class * * @ author whw * @ date 2020-1-3 17:37 * / @ Datapublic class BaseEntity implements Serializable {private static final long serialVersionUID = 1L; @ ApiModelProperty (value = "create 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 user 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 } the above is all the content of the article "how to automatically fill in the fields related to update operation by Mybatis-Plus". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!