In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how MyBatis-Plus implements the auto-populating function of fields. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
I. Preface
In the project, we have some common fields that need to be modified
Such as:
Gmt_create: creation time
Creator_id: creator
Gmt_modified: modification time
Modifier_id: modified by
At this time, we can use the field auto-filling function in MyBatis-Plus to implement.
Idea: extract the common field into the BaseEntity class, and then inherit the base class of the class that uses this common field, and finally MyBatis-Plus helps us to achieve automatic filling, so that we can reduce the amount of code repetition in the service service class!
Second, to achieve 1. Entity class @ Data@TableName ("t_user") public class User extends BaseEntity {private static final long serialVersionUID = 1L; @ TableId (value = "id", type = IdType.AUTO) private Integer id; @ TableField ("username") private String username; @ Override protected Serializable pkVal () {return this.id;}} 2. Common Fields-populate fields with annotations
For example: @ TableField (fill = FieldFill.INSERT)
Getter@Setterpublic abstract class BaseEntity extends Model {/ * creation date-present tense indicates active creation of * / @ TableField (value = "gmt_create", fill = FieldFill.INSERT) private Date gmtCreate; / * modification time-past participle indicates passive update * / @ TableField (value = "gmt_modified", fill = FieldFill.INSERT_UPDATE) private Date gmtModified;}
Ctrl Select FieldFill to enter the source code to view the corresponding field filling policy enumeration class, as follows:
Public enum FieldFill {DEFAULT (0, not processed by default), INSERT (1, insert filled field), UPDATE (2, update filled field), INSERT_UPDATE (3, insert and update filled field); / * * primary key * / private final int key; / * * description * / private final String desc FieldFill (final int key, final String desc) {this.key = key; this.desc = desc;} public static FieldFill getIgnore (int key) {FieldFill [] fis = FieldFill.values (); for (FieldFill fi: fis) {if (fi.getKey () = = key) {return fi;}} return FieldFill.DEFAULT } public int getKey () {return this.key;} public String getDesc () {return this.desc;}} 3. Custom MyMetaObjectHandler field auto-fill processing class inherits MetaObjectHandler
Note: you need to declare @ Component injection in Spring Boot
/ *
MyBatisPlus custom field auto-fill processing class-use @ TableField annotations in entity classes
* * @ description: note that null * @ author: zhengqing * @ date: 2019-8-18 0018 1:46 * / @ Componentpublic class MyMetaObjectHandler extends MetaObjectHandler {private static final Logger LOG = LoggerFactory.getLogger (MyMetaObjectHandler.class) / * creation time * / @ Override public void insertFill (MetaObject metaObject) {LOG.info ("- start insert fill... -") If (metaObject.hasGetter ("gmtCreate") & & metaObject.hasGetter ("gmtModified") {setFieldValByName ("gmtCreate", new Date (), metaObject); setFieldValByName ("gmtModified", new Date (), metaObject) }} / * Last update time * / @ Override public void updateFill (MetaObject metaObject) {LOG.info ("- start update fill... -") If (metaObject.hasGetter ("et.gmtModified")) {setFieldValByName ("gmtModified", new Date (), metaObject);}
Warm Tips:
We use et. Exe when updating fields. Field name or param1. The field will only take effect!
Reason: we can check the properties in metaObject in debug mode and find that there is too much et.
Or looking at the inherited BaseMapper class source code, we can also find that there is et in all the updated methods.
The insertion method does not have et.
Another point to note is that auto-padding occurs after the insert or update method is executed, that is, MyBatis-Plus determines whether the fields annotated by @ TableField have been manually updated after the method, and if not, it will go to the custom implementation class MyMetaObjectHandler!
Thank you for reading! This is the end of this article on "how to achieve automatic field filling in MyBatis-Plus". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
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.