Make writing a habit together! This is my first day to participate in the “Gold Digging Day New Plan · April More text challenge”, click to see the details of the activity.

1. Preface 🔥

👨🎓 Author: Bug Bacteria

✏️ blog: CSDN, Nuggets, etc

💌 public account: Magic House of the Circle of the Apes

🚫 special statement: original is not easy, reprint please attach the original source link and this article statement, thank you for your cooperation.

🙏 Copyright notice: part of the text or pictures in the article may come from the Internet or Baidu Encyclopedia, if there is infringement, please contact bug bacteria processing.

Hello, little friends, I am bug bacteria 👀, unconsciously on a year and April, is a good season for outing. But the epidemic in Shanghai, in addition to telecommuting at home is to watch technology incidentally volume some articles. This is not another article activity in April, push yourself, do some content output every day, develop the habit of writing, in the future you will come back to thank yourself for your perseverance to have the brilliant achievements at this moment.

In the process of reviewing articles, if you think the articles are helpful to you at all, please don’t be too mean with your likes and bravely light up the articles 👍. Your likes (collect ⭐️+ pay attention to 👨 port + message board) are the best encouragement and support for bugs on my creation path. Time does not abandon 🏃🏻♀️, nuggets stop 💕, cheer up 🏻

Ii. Background 🔥

In daily development, especially some business system projects, we find a common rule that many business tables contain the same fields, such as ID primary key, UUID, create_time, create_BY, update_BY, update_time, etc. These fields are also necessary for some tables.

Since the fields are exactly the same, how can I, as a code cleanliness freak, live with the fact that each entity needs to create these fields repeatedly? I counted, the largest and most complex personnel declaration system has 64 business tables, among which 32 tables contain all the above field attributes, why not encapsulate? I don’t understand.

  • First, each entity has these fields. Why not encapsulate them?
  • Second, these fields need to be manually assigned in the conventional increment, deletion and modification, why not remove the public method implementation?

And what I want to do today is peel them all out and transform them! Elegance never goes out of style.

  • The first one is a public Entity.
  • Second, you do not need to manually assign values to these fields.

Three, implementation ideas 🔥

Idea 1:

Use AOP for custom annotation methods. The implementation is simple, adding a custom annotation section assignment. I will not introduce more, the focus is on idea 2, convenient and quick.

Idea 2:

Using @TableField annotation provided by Mybatis – Plus to realize automatic filling of field content.

4. Implementation scheme 🔥

I think you are familiar with Mybatis – Plus, here I will not introduce it more, we directly into the topic.

Step 1: Introduce MP dependencies

<! --mybatis-plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> The < version > 3.2.0 < / version > < / dependency >Copy the code

Step 2: Add an annotation attribute to the entity class

Annotate the fields you need to fill automatically. What I have done here is to separate common fields from many tables into a common entity class, which is the first step in solving the problem I mentioned above. Or you can just add the annotation to the fields you want to auto-fill. You can also add a field to the database, because the database must have these fields. Otherwise, the direct configuration will definitely fail to map and an error will be reported.

@tableField (fill = FieldFill.INSERTCopy the code

Development:

The following are four filling strategies, which you can select according to your business.

Public enum FieldFill {/** * DEFAULT not to handle */ DEFAULT, /** * INSERT, /** * UPDATE, /** * insert and update the fill field */ INSERT_UPDATE}Copy the code

Here is the complete addition. For your reference:

package com.example.review.vo; import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import lombok.Data; import java.util.Date; /** * Public attribute entity class ** @author luoYong * @create 2022 月29日00:00:49 */ @data Public Class BaseEntity {/** * ID generation policy is UUID */ @TableId(type = IdType.UUID) private String id; /** * @tableField (fill = FieldFill.INSERT) private String createBy; */ @tableField (fill = FieldFill.INSERT) private Date createTime; /** * @tableField (fill = FieldFill.INSERT_UPDATE) private String updateBy; */ @tableField (fill = FieldFill.INSERT_UPDATE) private Date updateTime; }Copy the code

Ok, the above is ok. If the actual measurement is specific, I will focus on explaining it in the next period. I will not instill too many knowledge points to everyone at one time.

If you want to learn more, you can check out my previous top tips. Accumulate a little weird knowledge every day, and over time, you will become a person you respect. Well, I’ll see you next time

Five, the previous recommendation 🔥

  • Springboot series (16) : Integrated easyPOI to achieve Excel export
  • Springboot series (16) : integrated easyPOI to achieve a single Word template export page
  • Springboot series (16) : Integrated easyPOI to realize word template circulation export multiple data
  • Springboot series (16) : Integrated easyPOI word template image export
  • Springboot series (16) : integrated easyPOI to achieve excel multi-sheet import
  • Springboot series (16) : integrated easyPOI to achieve excel multi-sheet export

Six, the end of the article 🔥

If you want to learn more, you can pay attention to the bug bug column “SpringBoot Zero-based Introduction”, from scratch, from zero to one! Hope I can help you.

I am bug fungus, a want to go 👣 out of the mountain to change the fate of the program ape. The next road is still very long, waiting for us to break through, to challenge. Come on, friends, let’s come on! Fighting for the future!

Finally, I like to send you two words, and you share!

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

☘️ Be who you want to be, there is no time limit, you can start whenever you want,

🍀 You can change from now on, you can also stay the same, this thing, there are no rules to speak of, you can live the most wonderful yourself.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

💌 If this article is helpful to you, please leave a like! (# ^. ^ #);

💝 if you like the article shared by bug fungus, please give bug fungus a point of concern! The danjun ‘ᴗ, you guys will have a cameo appearance with you.

💗 if you have any questions about the article, please also leave a message at the end of the article or add a group [QQ communication group :708072830];

💞 In view of the limited personal experience, all views and technical research points, if you have any objection, please directly reply to participate in the discussion (do not post offensive comments, thank you);

💕 copyright notice: original is not easy, reprint please attach the original source link and this article statement, all rights reserved, piracy will investigate!! thank you