In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to understand Spring Boot integration Mybatis Plus auto-fill fields", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "how to understand Spring Boot integration Mybatis Plus auto-fill fields" bar!
In general, when designing a table, some system fields are added to the table, such as create_time, update_time, and so on.
Add dependency
Here's how to fill the fields automatically through MyBatis Plus. First, add MyBatis Plus dependencies:
Com.baomidou mybatis-plus-boot-starter 3.4.1
Create entity classes and add fill comments
Create an entity class, then annotate the attributes that need to be automatically populated with @ TableField (fill = FieldFill.INSERT), @ TableField (fill = FieldFill.INSERT_UPDATE), and so on.
@ Data @ TableName ("user") public class UserEntity extends BaseEntity {private static final long serialVersionUID = 1L; / * Primary key * / @ TableId (value = "id", type = IdType.ASSIGN_ID) private Long id; / * * name * / @ TableField ("name") private String name / * Age * / @ TableField ("age") private Integer age; / * email * / @ TableField ("email") private String email; / * creation time * / @ TableField (value = "create_time", fill = FieldFill.INSERT) public Date createTime / * modification time * / @ TableField (value = "modify_time", fill = FieldFill.INSERT_UPDATE) public Date modifyTime;}
Where the fill attribute is the field auto-filling policy, and the optional parameters are as follows:
Public enum FieldFill {/ * does not handle * / DEFAULT by default, / * insert fill field * / INSERT, / * * update fill field * / UPDATE, / * * insert and update fill field * / INSERT_UPDATE}
Just create a Mapper directly to facilitate testing:
@ Mapper public interface UserMapper extends BaseMapper {}
Implement meta-object processor interface
The implementation method may be slightly different depending on the version of MyBatis Plus. In version 3.4.1, the MetaObjectHandler interface is implemented, while the lower version may inherit the MetaObjectHandler abstract class to implement the corresponding method.
The following is to implement the field filling logic for inserting and updating data. When inserting an object, the creation time createTime and modification time modifyTime fields are automatically populated to the current time, and when the object is updated, the modification time modifyTime is modified to the latest time.
@ Component public class CommonMetaObjectHandler implements MetaObjectHandler {/ * creation time * / private static final String FIELD_SYS_CREATE_TIME = "createTime"; / * modification time * / private static final String FIELD_SYS_MODIFIED_TIME = "modifyTime" / * * insert meta-object field fill (for populating public fields when inserting) * * @ param metaObject meta-object * / @ Override public void insertFill (MetaObject metaObject) {Date currentDate = new Date () / / insert creation time if (metaObject.hasSetter (FIELD_SYS_CREATE_TIME)) {this.strictInsertFill (metaObject, FIELD_SYS_CREATE_TIME, Date.class, currentDate) } / / simultaneously set the modification time to the current insertion time if (metaObject.hasSetter (FIELD_SYS_MODIFIED_TIME)) {this.strictUpdateFill (metaObject, FIELD_SYS_MODIFIED_TIME, Date.class, currentDate) }} / * Update meta-object field fill (for populating public fields during update) * * @ param metaObject meta-object * / @ Override public void updateFill (MetaObject metaObject) {this.setFieldValByName (FIELD_SYS_MODIFIED_TIME, new Date (), metaObject);}}
The default padding policy is that the default value is not overridden, and if the value provided is null, it is not populated either. If the default padding policy is not satisfied, you can override the strictFillStrategy method to meet your needs.
Test fields are automatically populated
Write a test class to verify that the fields of the response are automatically populated during insert and update operations.
@ Slf4j @ RunWith (SpringRunner.class) @ SpringBootTest public class AutoFillTest {@ Resource private UserMapper userMapper; @ Test public void test () throws InterruptedException {UserEntity user = new UserEntity (); user.setName ("wupx"); user.setAge (18); user.setEmail ("wupx@qq.com"); userMapper.insert (user); Long id = user.getId () UserEntity beforeUser = userMapper.selectById (id); log.info ("before user: {}", beforeUser); Assert.assertNotNull (beforeUser.getCreateTime ()); Assert.assertNotNull (beforeUser.getModifyTime ()); beforeUser.setAge (19); Thread.sleep (1000L); userMapper.updateById (beforeUser); log.info ("query user: {}", userMapper.selectById (id)) / / clear test data userMapper.deleteById (id);}}
Start the test class, which can be seen in the log:
Before user:UserEntity (id=1346071927831134210, name=wupx, age=18, email=wupx@qq.com, createTime=Mon Jan 04 20:32:11 CST 2021, modifyTime=Mon Jan 04 20:32:11 CST 2021) query user:UserEntity (id=1346071927831134210, name=wupx, age=19, email=wupx@qq.com, createTime=Mon Jan 04 20:32:11 CST 2021, modifyTime=Mon Jan 04 20:32:13 CST 2021)
When the object is inserted for the first time, the creation time and modification time are automatically filled, and when the object is modified, the modification time is updated accordingly.
In addition, public fields can be encapsulated into public classes, such as BaseEntity:
@ Data public class BaseEntity {/ * primary key * / @ TableId (value = "id", type = IdType.ASSIGN_UUID) private Long id; / * creation time * / @ TableField (value = "create_time", fill = FieldFill.INSERT) private Date createTime / * modification time * / @ TableField (value = "modify_time", fill = FieldFill.INSERT_UPDATE) private Date modifyTime;}
After testing, you can also complete the automatic filling of public fields, and you can also do this in the project, which can reduce the set operation every time you insert or update.
Thank you for your reading, the above is "how to understand Spring Boot integration Mybatis Plus auto-fill fields" content, after the study of this article, I believe you on how to understand Spring Boot integration Mybatis Plus auto-fill fields of this problem has a deeper understanding, the specific use of the need for you to practice verification. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.