"Mybatis-plus" primary key id generation, field auto-fill

One, the generation of primary key id

There is usually a primary key id in the database table as the unique identification of this data.

Common way

  1. Automatic database growth is very common, and it can be the only one in the entire database. Because id is sorted naturally, it is convenient for operations involving sorting.
  2. The automatic growth of UUID, although simple, is more troublesome for operations such as sub-tables. Because when you insert data in the second sheet, you need to get the id of the last data in the previous sheet. UUID is different, each time it has a random and unique value, but because it is random, there is no sorting.
  3. redisredis can also be used to generate IDs, using redis' atomic operations. The advantage is that it does not rely on the database, and the performance is good, but then the introduction of redis brings a certain degree of complexity.

mybatis-plus way

In fact, when I used mp to add data before, I didn't make the corresponding configuration, and the primary key id was attached by default.

image.png

Its configuration is also very simple, add annotations on 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) // 新增的时候update也有值
    private Date updateTime;
}

In the idea editor, you can hold down ctrl+left-click and see several enumerated values ​​of IdType.

"Mybatis-plus" primary key id generation, field auto-fill

AUTO
NONE
INPUT
UUID
ID_WORKER
ID_WORKer_STR

Regarding the generation of this 19-bit value of mp, you can learn more about it, using the snowflake algorithm.

Snowflake is Twitter's open source distributed ID generation algorithm, and the result is a long ID.

The core idea is: use 41bit as the number of milliseconds, 10bit as the machine ID (5 bits are the data center, 5 bits of the machine ID), and 12bit as the serial number within milliseconds (meaning that each node can generate every millisecond 4096 IDs), there is a sign bit at the end, which is always 0.

Two, mp realizes automatic filling

There is also a common operation when operating a database, which is the filling of some fields, such as common create_time, update_time, etc. The filling methods are the same.

In addition to manual filling each time, it can also be filled automatically. And it only takes 2 steps to set up auto-fill in MP:

1. Add comments

@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)        // 新增的时候填充数据
    private Date createTime;
    @TableField(fill = FieldFill.INSERT_UPDATE) // 新增或修改的时候填充数据
    private Date updateTime;
}

2. Custom implementation class MyMetaObjectHandler

Here we need to write an implementation class ourselves

@Component //此注解表示 将其交给spring去管理
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, add the corresponding fields create_time and update_time in the database table. After adding it, you can try it. Let's add a test data first:

//    新增
    @Test
    void addUser() {
        User user = new User();
        user.setName("老王");
        user.setAge(66);
        user.setEmail("[email protected]");
        userMapper.insert(user);
    }

View the database table after successful execution:

"Mybatis-plus" primary key id generation, field auto-fill

When adding, both fields are filled with time.

Now when only doing update operations, only update_time will be updated.

//    修改
    @Test
    void updateUser() {
        User user = new User();
        user.setId(1342322873243996161L);
        user.setName("老王修改后");
        userMapper.updateById(user);

    }

"Mybatis-plus" primary key id generation, field auto-fill

The result is correct.

Guess you like

Origin blog.csdn.net/doubututou/article/details/111684254