In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces how to achieve the automatic filling of public fields in the SQL sentence, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
1. Preface
When we design the database, we will bring new, updated time, operators and other audit information. The reason for bringing this information is that if one day the company's database is artificially deleted, although there may be a database backup to recover the data. But we still need to track down who did it, when it was done, what it was done, and so on, to facilitate accountability and repair. But it is tedious for us to change each piece of data explicitly, and we want to deal with it without perception.
two。 General mode
So are there any good solutions? @ CreatedBy and @ LastModifiedBy are provided in the Spring Data framework to capture who created or modified entities, and @ CreatedDate and @ LastModifiedDate to capture appropriate created or modified entities. You can use these features if you use the relevant framework. Well, in fact, we know that domestic Spring Data JDBC, Spring Data JPA is not the mainstream, the mainstream is Mybatis. So what are our options?
2.1 develop the Mybatis audit plug-in
If you use native Mybatis, you can write an audit plug-in to implement these functions. I've explained the Mybatis plug-in tutorial before, and it's not very difficult. If you want to use it, there are a lot of Mybatis audit components available on GitHub. I was going to write one by hand, but I did write it well. You can search for them through the keyword Mybatis Audit and choose the one that works best for you.
2.2 automatic filling of Mybatis Plus
If you use Mybatis Plus, you can do so with the help of its auto-fill function.
Based on Mybatis Plus 3.3.0
You only need to implement the MetaObjectHandler interface:
@ Componentpublic class MybatisAuditHandler implements MetaObjectHandler {@ Override public void insertFill (MetaObject metaObject) {/ / declares the logic for automatically populating fields. String userId = AuthHolder.getCurrentUserId (); this.strictInsertFill (metaObject, "creator", String.class, userId); this.strictInsertFill (metaObject, "createTime", LocalDateTime.class,LocalDateTime.now ());} @ Override public void updateFill (MetaObject metaObject) {/ / declares the logic that automatically populates the field. String userId = AuthHolder.getCurrentUserId (); this.strictUpdateFill (metaObject, "updater", String.class,userId); this.strictUpdateFill (metaObject, "updateTime", LocalDateTime.class,LocalDateTime.now ();}}
Then let's extend Mybatis Plus's Model to put in the public audit field and declare the corresponding padding strategy:
Public abstract class BaseEntity
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.