Primary key ID generation

A database table usually has a primary key ID as a unique identifier for this data.

Common ways

  1. Automatic database growth is very common, can do the whole library unique. Because ids are naturally sorted, it’s handy for operations that involve sorting.
  2. The automatic increment above the UUID, while simple, is more cumbersome for operations like table splitting. Because when you insert data in the second table, you need to get the ID of the last data in the previous table. UUID, on the other hand, is a random unique value each time, but since it is random, there is no sort.
  3. Redis Redis can also be used to generate ids, using the atomic operations of Redis. The benefit is that it does not rely on the database and has good performance, but with the introduction of Redis, there is a certain amount of complexity.

Mybatis – plus the way

In fact, when I used MP to add data before, I did not carry out the corresponding configuration, and the default will be with the primary key ID.

It’s also easy to configure, annotating the primary key with @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) private Date updateTime; }Copy the code

In the IDEA editor, you can hold down CTRL + left mouse click to see several enumerated values of IdType.

  • AUTO: is to use the database increment
  • NONE: No value. You need to manually set the value
  • INPUT: You need to manually set the value
  • UUID: Generates a primary key ID of the UUID type
  • ID_WORKER: Generates a 19-bit value of numeric type
  • ID_WORKer_STR: Generates a 19-bit value of string type

The 19-bit mp value is generated using the Snowflake algorithm. Snowflake is Twitter’s open source distributed ID generation algorithm that results in a long ID. The idea is to use 41bits as the number of milliseconds, 10bits as the machine ID (5 bits for the data center, 5 bits for the machine ID), 12bits as the serial number within milliseconds (meaning each node can generate 4096 ids per millisecond), and finally a symbolic bit, always 0.

Two, MP automatic filling

Another common operation in a database is to fill some fields, such as create_time and update_time, in the same way. So in addition to manually filling every time, you can also automatically fill. Setting up auto-fill in MP takes only 2 steps:

1. Add 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) private Date createTime; @tableField (fill = FieldFill.INSERT_UPDATE) // Fill data when adding or modifying data private Date updateTime; }Copy the code

2. User-defined implementation class MyMetaObjectHandler

We need to write our own implementation class here

Public class MyMetaObjectHandler implements MetaObjectHandler {@override public void MyMetaObjectHandler implements MetaObjectHandler 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); }}Copy the code

Add create_time and update_time to the database table. Add test data:

// add @test void addUser() {User User = new User(); User. Elegantly-named setName (" wang "); user.setAge(66); user.setEmail("[email protected]"); userMapper.insert(user); }Copy the code

Query the database table after executing the command successfully:



When added, both fields are filled with time.

Now when you only do updates, you only update update_time.

@test void updateUser() {User User = new User(); user.setId(1342322873243996161L); User.setname (" user.setName "); userMapper.updateById(user); }Copy the code



The result is correct.