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; /**// Welcome to Java Development exchange. 909038429 * This column is automatically assigned to insert or update users. 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

INSERT fill = FieldFill.INSERT_UPDATE Inserts nSERT or UPDATE inserts 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()); }// Welcome to Java development exchange: 909038429 // Set data modification during 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, during data insert operations,insertFill(MetaObject metaObject)Will be executed automatically. Mysql, Netty, Spring, thread, Spring Cloud, JVM, source code, algorithm, etc., also have a detailed learning plan map, interview questions, etc., need to obtain these contents of the friend please add Q: sample: 909038429/./* Welcome to Java chat