MybatisPlus logical deletion, automatically generate creation time and update time
Below is an abstract class. Since these 3 attributes are used for each table, you can write the attributes with an abstract class and be inherited by the entity class.
@TableField(fill = )
The fill indicates how the field is filled.
There are three values in the FieldFill enumeration class
-
: No fill is done by default;
-
: Fill in fields when inserting;
-
: Populate fields when updated.
I only use insertion and update here
@TableLogic
is a field used to identify logical deletion.
When deleting, the data in the database table is not really deleted, but it is marked as deleted.
package ; import ; import ; import ; import ; import ; public abstract class ValueObject { // Book creation time (perform MP's automatic FILL operation when adding records) @TableField(fill = ) private Date createTime; // Book modification time @TableField(fill=FieldFill.INSERT_UPDATE) private Date updateTime; @TableLogic private int isDeleted; }
package ; import ; import .slf4j.Slf4j; import ; import ; import ; @Slf4j @Component //Customize a MetaObjectHandler implementation class, inject it into the container, and it will be automatically recognized by MP and usedpublic class MyMetaObjectHandler implements MetaObjectHandler { @Override public void insertFill(MetaObject metaObject) { ("start insert fill ...."); ("createTime", new Date()); ("updateTime", new Date()); } @Override public void updateFill(MetaObject metaObject) { ("start update fill ...."); ("updateTime", new Date()); } }
Entity Class
package ; import ; import ; import ; import ; import ; @Data @NoArgsConstructor @TableName("tbl_book") public class Book extends ValueObject{ @TableId("isbn") private String isbn; private String name; private double price; }
yml file
#mybatis-plus configurationmybatis-plus: #Logical Deletion global-config: db-config: logic-delete-field: is_deleted logic-delete-value: 1 logic-not-delete-value: 0
in:
logic-delete-field is used to bind field properties in a database that is deleted logically
-
logic-delete-value
: Indicates the deleted value -
logic-not-delete-value
: indicates the existing value
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.