Insert data into the database table in MyBatisPlus. The @TableId annotation is used in MyBatisPlus to realize the function of auto-incrementing sequence id.

@TableId annotation of MyBatisPlus to realize the function of automatic insertion of auto-increment sequence id

This annotation is mainly used for the primary key attribute in the entity class of the corresponding database table.

Writing: @TableId(value="database primary key field", type = IdType. One of the six types)
For example: @TableId(value="user_id",type = IdType.AUTO)
IdType has many attributes, so we just need to figure it out at present AUTO attribute is fine, this example also mainly introduces the function of AUTO attribute

1. The meaning of the value of type in the annotation

    //IdType.ID_WORKER_STR 默认的;底层使用了雪花算法;类型为Integer
    //IdType.AUTO 数据库自增;数据库上也要勾上自增
    //IdType.NONE 没有设置主键类型;跟随全局;全局的主键策略如果没有设置,默认是雪花算法
    //IdType.INPUT 手动输入;必须手动输入,数据库自增也没用;
    //IdType.UUID 全局唯一id;无序;字符串;
    //ID_WORKER_STR 全局唯一(idWorker的字符串表示)

2. The value of type is AUTO

When MyBatisPlus inserts data into the database table, if the primary key is id, and id has been set as an auto-increment column in the database table, if @Tableld(value="id" has been written on the corresponding attribute of the java entity) ,type=IdType.Auto) annotation, then you don’t need to write the value of id when inserting data, the program will insert a value for id into the database according to the auto-increment rule, but if you don’t write the annotation @TableId on the id attribute of the java entity (value="id",type=IdType.Auto), then although a value will be added to id when inserting, it is not in accordance with the rules of self-increment but in accordance with the rules of the snowflake algorithm. The following figure is to set the java entity attribute to self-increment:

Insert picture description here

The following figure is to set the corresponding primary key field in the database to auto-increment:

Insert picture description here

In this case, there is no need to set the value of id when inserting, and the value of id will be automatically incremented and generated, as shown below:

Insert picture description here

If you just set the auto-increment sequence on the id field in the database, but not set the @Tableld(value="id",type=IdType.Auto) annotation on the corresponding id attribute of the javabean, then the id is Generated according to the snowflake algorithm.

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.

1. The result of id generated by SnowFlake algorithm is a 64bit integer, and its structure is as follows:

img

● 1 bit, no use. In the binary system, the highest bit of 1 is a negative number, but the id we generate generally uses an integer, so the highest bit is fixed at 0

● 41 bits, used to record the time stamp (milliseconds).
○ 41 bits can represent 2 41 − 1 2^{41}-12411 number,
○ If it is only used to represent a positive integer (a positive number in a computer contains 0), the range of values ​​that can be represented is: 0 to2 41 − 1 2^{41}-12411 , minus 1 is because the range of values ​​that can be represented starts from 0, not 1.
○ That means 41 bits can represent2 41 − 1 2^{41}-1241The value of 1 millisecond, converted into a unit year is(2 41 − 1) / (1000 ∗ 60 ∗ 60 ∗ 24 ∗ 365) = 69 (2^{41}-1) / (1000 * 60 * 60 * 24 * 365) = 69(2411)/(1000606024365)=6 9 years

10 bits, used to record the working machine id.
○ Can be deployed in 2 10 = 1024 2^{10} = 1024210=1 0 2 4 nodes, including 5-digit datacenterId and 5-digit workerId
○ The largest positive integer that can be represented by 5-digit (bit) is2 5 − 1 = 31 2^{5}-1 = 31251=3 1 , that is, 32 numbers 0, 1 , 2, 3, ... 31 can be used to represent different datecenterId or workerId

12 digits, serial number, used to record different IDs generated within the same millisecond.
○ The largest positive integer that can be represented by 12 bits is 2 12 − 1 = 4095 2^{12}-1 = 40952121=4 0 9 5 , that is, 4095 numbers such as 0, 1, 2, 3, ... 4094 can be used to represent the 4095 ID serial numbers generated by the same machine at the same time (milliseconds)

Since 64-bit integers in Java are of type long, the id generated by the SnowFlake algorithm in Java is stored in long.

SnowFlake can guarantee:
● All generated IDs increase according to the time trend
● Duplicate IDs will not be generated in the entire distributed system (because there are datacenterId and workerId to distinguish)

3. If the primary key is not set to increase the sequence of an exception

Assuming that the primary key id is not set to an auto-increment sequence, but the id value is not set when inserting the user, then the following abnormality will occur, as shown below:

Insert picture description here

Set the auto-increment sequence exception to the primary key and then disappear, the setting method:

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45950109/article/details/112525780