Full Project Address
Our-task: A complete list management system
preface
According to the Alibaba development manual, the table must have three fields: ID,create_time, update_time. There are two ways to generate the create time and modify time — database level and code level
Automatic filling
Database level
At the database level, when designing a table, you can set some update policies for a certain field. For example, when the record is added, the creation time is automatically set to the current time. When the record is modified, the update time is automatically set to the current time. However, because the design of the database is very important, the development process can not be modified at will database structure, so this method is not considered
The code level
At the code level, we use code to fill in the creation time and the modification time. I will mainly talk about the use of this function in Mybatis- Plus
-
Create a table with fields create_time and update_time of type datetime
-
In the POM file of SpringBoot, introduce the dependency of Mybatis – Plus
< the dependency > < groupId > com. Baomidou < / groupId > < artifactId > mybatis - plus - the boot - starter < / artifactId > < version > 3.2.0 < / version > </dependency>Copy the code
-
In one project, we’re going to have a bunch of entity classes, and we’re going to configure the creation time and update time of the properties in the entity class, and we’re going to add the @TableField annotation to the properties
@apiModelProperty (value = "createTime ") @tableField (fill = FieldFill.INSERT) private Date createTime; @apimodelproperty (value = "updateTime ") @tablefield (fill = FieldFill.INSERT_UPDATE) private Date updateTime;Copy the code
-
The @tablefield annotation fill explains this in detail
- Fill (FieldFill policy), an enumeration type, is not required to be specified and defaults to fieldfill.default
- FieldFill
- DEFAULT: no processing is performed by DEFAULT
- INSERT: Fields are filled during insertion
- UPDATE: Fields are filled during UPDATE
- INSERT_UPDATE: Populates fields when inserting and updating
-
Write the Handler to do the automatic filling. Just write the following code directly into your own Handler package
package com.water76016.ourtask.config.security.handler; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import org.apache.ibatis.reflection.MetaObject; import org.springframework.stereotype.Component; import java.util.Date; /** * @program: our-task * @description: Automatically populate the creation time and update time of each database record * @author: water76016 * @create: 2020-11-24 10:53 **/ @component public class MyMetaObjectHandler implements MetaObjectHandler {/** * implements MetaObjectHandler metaObject */ @Override public void insertFill(MetaObject metaObject) { this.setFieldValByName("createTime", new Date(), metaObject); this.setFieldValByName("updateTime", new Date(), metaObject); } /** * public void updateFill(metaObject metaObject) { this.setFieldValByName("updateTime", new Date(), metaObject); }}Copy the code
-
If you have any questions, please leave them in the comments section or email me at [email protected]