Non-business fields such as create_time, update_time, update_BY, and create_BY are used to maintain the data record creation time, modification time, modifiers, and creator. Normally we need to manually assign these fields. The assignment process is also redundant and repetitive.

  • Create_time is the current time of the system and update_time is the current time of the system modification operation.
  • Create_by (creator) and update_by(modifier) set the value to the user name of the current login user
xxxYyyZzz.setUpdateBy("zimug"); // Update operator xxxYYYZZz.setupDateTime (new Date()); // Data records the time of the update operationCopy the code

Mybatis Plus provides us with a way to automatically assign values once and for all.

First, adjust the database table structure

Take the xxX_YYY_ZZz table in the mysql database as an example. Add the following four general data maintenance fields on the basis of the original table fields.

ALTER TABLE 'xxx_YYY_zzz' ADD COLUMN 'create_by' VARCHAR(64) NOT NULL COMMENT 'owner '; ALTER TABLE 'xxx_YYY_zzz' ADD COLUMN 'create_time' DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'CURRENT_TIMESTAMP '; ALTER TABLE 'xxx_YYY_zzz' ADD COLUMN 'update_by' VARCHAR(64) NOT NULL COMMENT 'VARCHAR '; ALTER TABLE `xxx_yyy_zzz` ADD COLUMN `update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'CURRENT_TIMESTAMP ';Copy the code

General maintenance information parent class – autoassigned fields

Now that we are maintaining new creation and modification information for a table, our entity class also needs to make necessary adjustments. To avoid adding these four member variables to every entity class, we define a parent class, BaseColumns.

@data public class BaseColumns {** * */ @tableField (fill = FieldFill. Insert private String createBy; /** * @tableField (fill = FieldFill. Insert) private LocalDateTime createTime; /** * The value of this column is automatically assigned when the user is updating the column. select = false */ @TableField(fill = FieldFill.INSERT_UPDATE,select = false) private String updateBy; /** * Select * from 'insert' or 'update'; select = false */ @TableField(fill = FieldFill.INSERT_UPDATE,select = false) private LocalDateTime updateTime; }Copy the code
  • Fill = FieldFill.INSERT Indicates that the field is automatically assigned during the INSERT operation
  • Fill = FieldFill.INSERT_UPDATE Indicates that this field is automatically assigned during nSERT or update operations
  • Select = false indicates that the database field corresponding to this property will not be queried when using the Mybatis Wrapper conditional constructor. The data modification time operator is usually more meaningful to operations and therefore does not usually need to be displayed on a Web page, so it is usually not included in select queries. (This content is not directly related to our native field autofill, but it is meaningful in practical application)

Implementation of entity class

The following entity class XxxYyyZzz corresponds to the XXX_YYY_ZZZ table in the database. In addition to the above four general fields, the XXX_YYY_ZZZ table also contains other service fields.

@data@equalSandHashCode (callSuper = true) public class XxxYyyZzz extends BaseColumns {Copy the code

4. Rules for automatic assignment

@Component public class MybastisColumnsHandler implements MetaObjectHandler { @Resource private JwtTokenUtil jwtTokenUtil; // My utility class, which is used to obtain the logon information from the Token. @override public void insertFill(MetaObject MetaObject) {this.strictInsertFill(MetaObject, "createTime", LocalDateTime.class, LocalDateTime.now()); this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); this.strictInsertFill(metaObject, "createBy", String.class, jwtTokenUtil.getUsernameFromToken()); this.strictUpdateFill(metaObject, "updateBy", String.class, jwtTokenUtil.getUsernameFromToken()); } // Set data to update, @override public void updateFill(MetaObject MetaObject) {this.strictUpdateFill(MetaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); this.strictUpdateFill(metaObject, "updateBy", String.class, jwtTokenUtil.getUsernameFromToken()); }}Copy the code
  • When data is added, the system automatically assigns values to createTime, updateTime, createBy, and updateBy.
  • Automatically assign values to updateTime and updateBy during data modification.
  • JwtTokenUtil is a utility class I wrote that gets the username of the current logged-in user from the current logged-in user JWT Token. (Your system has a different method to obtain the current login user name than mine, but it can be obtained anyway)

Five, to achieve the effect

For example, when performing data updates, the following two lines of code do not need to be written, and are automatically completed by updateFill(MetaObject MetaObject)

//xxxYyyZzz.setUpdateBy("zimug"); / / record data update operation / / xxxYyyZzz setUpdateTime (new Date ()); / / data record update operation time xxxYyyZzzMapper updateById (xxxYyyZzz);Copy the code

Similarly, insertFill(MetaObject MetaObject) is automatically executed during data insert operations.

Welcome to my blog, where there are many fine collections

  • This article is reprinted with a credit (must be accompanied by a link, not only the text) : Antetokounmpo blog.

Feel helpful to you, help me like, share! Your support is my inexhaustible creative power! . In addition, the author recently a period of time output as follows boutique content, looking forward to your attention.

  • Spring Boot2.0 by Hand
  • Spring Security- JWT-OAUTH2
  • RBAC Authority Management System for Actual Combat Front-end and Back-end Separation
  • “Actual SpringCloud Micro-service from Bronze to King”
  • VUE Series