编程知识 cdmana.com

[mybatis plus] primary key ID generation and field auto filling

One 、 Primary key id Generation

Database tables usually have a primary key id, As the unique identifier of this data .

Common ways

  1. Automatic database growth
    This is very common , It can be unique in the whole database . because id It's natural , It is convenient for operations involving sorting .

  2. UUID
    The automatic growth above , Simple though , But for the operation of sub table, it is more troublesome . Because when you insert the data in the second one , You need to get the last data from the last table id.
    UUID Is different , A random unique value every time , But because it's random , So there's no sort .

  3. redis
    redis It can also be used to generate id, utilize redis Atomic operation of . The advantage is that you don't have to rely on the database , Good performance , But then because redis The introduction of , It brings a certain degree of complexity .

mybatis-plus The way

In fact, before the use of mp When adding new data , I didn't configure it , By default, it will have a primary key id.

It's also very simple to configure , Annotate the primary key ,@TableId(type = IdType.ID_WORKER)

@Data
public class User {
    @TableId(type = IdType.ID_WORKER)
    private Long id;
    private String name;
    private Integer age;
    private String email;

    @TableField(fill = FieldFill.INSERT)
    private Date createTime;
    @TableField(fill = FieldFill.INSERT_UPDATE) //  When it's added update It's worth it 
    private Date updateTime;
}

stay idea In the editor , You can hold down the ctrl+ Left click , notice IdType Several enumeration values of .

  • AUTO: Is to use the database to increase
  • NONE: Not set , The value needs to be set manually
  • INPUT: The value needs to be set manually
  • UUID: Generate UUID Primary key of type id
  • ID_WORKER: Generate 19 The value of a , Numeric type
  • ID_WORKer_STR: Generate 19 The value of a , String type

About mp This 19 Generation of bit values , You can make an understanding of , Take advantage of snowflake Algorithm .
snowflake yes Twitter Open source distributed ID generating algorithm , The result is a long Type ID.
The central idea is this : Use 41bit As milliseconds ,10bit As a machine ID(5 individual bit It's the data center ,5 individual bit Machine ID),
12bit As a serial number in milliseconds ( It means that every node can produce 4096 individual ID), And finally, there's a sign bit , Forever 0.

Two 、mp Realize automatic filling

There is also a common operation when operating a database , It's just the filling of some fields , For example, the common create_time、update_time etc. , It's all filled in the same way .
So in addition to each manual fill , It can also automatically fill in . And in the MP Set auto fill in as long as 2 Step by step :

1. adding annotations

@TableField(fill = FieldFill.INSERT)@TableField(fill = FieldFill.INSERT_UPDATE).

@Data
public class User {
    @TableId(type = IdType.ID_WORKER)
    private Long id;
    private String name;
    private Integer age;
    private String email;

    @TableField(fill = FieldFill.INSERT)        //  Fill in the data when adding new ones 
    private Date createTime;
    @TableField(fill = FieldFill.INSERT_UPDATE) //  Fill in data when adding or modifying 
    private Date updateTime;
}

2. Custom implementation class MyMetaObjectHandler

Here we need to write an implementation class ourselves

@Component // This annotation indicates   Give it to spring To manage 
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createTime", new Date(), metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }


    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }
}

by the way , To add the corresponding field in the database table create_timeupdate_time. You can try it after adding it , First, add a new test data :

//     newly added 
    @Test
    void addUser() {
        User user = new User();
        user.setName(" Lao Wang ");
        user.setAge(66);
        user.setEmail("laowang@123.com");
        userMapper.insert(user);
    }

After successful execution, view the database table :


When it's added ,2 All the time fields are filled in .

Now when you only do the update operation , It's just going to update update_time.

//     modify 
    @Test
    void updateUser() {
        User user = new User();
        user.setId(1342322873243996161L);
        user.setName(" After Lao Wang's modification ");
        userMapper.updateById(user);

    }


Results the correct .

版权声明
本文为[Bite the apple and cry]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201225122311477m.html

Scroll to Top