MyBatis-Plus primary key generation strategy

MyBatis-Plus primary key generation strategy

Continued from the previous blog: Get started with MyBatis-Plus development quickly

Execute the Insert operation in the test class to insert records into the data table:

//测试插入操作
@Test
public void testInsert(){
    
    
    User user = new User();
    user.setName("Cloud");
    user.setAge(3);
    user.setEmail("[email protected]"); //MP自动生成id
    int res = userMapper.insert(user);
    System.out.println(res);  //受影响的行数
    System.out.println(user);
}

Through the log printed on the console, it is found that MyBatis-Plus userautomatically assigns the idproperty of the object, and the assignment uses the snowflake algorithm;

@TableId: Set the primary key mapping, valuemap the primary key field name, and typeset the primary key type, that is, the primary key generation strategy. for example:@TableId(value = "id", type = IdType.AUTO)

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor

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

IdTypeIt is an enumeration class. There are mainly 4 different types of values ​​(AUTO, NONE, INPUT, ASSIGN_ID and ASSIGN_UUID). The source code is as follows:

public enum IdType {
    
    
    AUTO(0),
    NONE(1),
    INPUT(2),
    ASSIGN_ID(3),
    ASSIGN_UUID(4),
    
    //下面的表示已经弃用了,并以上面几种代替
    /** @deprecated */
    @Deprecated
    ID_WORKER(3),
    /** @deprecated */
    @Deprecated
    ID_WORKER_STR(3),
    /** @deprecated */
    @Deprecated
    UUID(4);
}
value describe
AUTO Primary key field auto-increment
NONE Primary key not set
INPUT Requires developers to manually assign values
ASSIGN_ID MP allocation ID, Number / String type
ASSIGN_UUID MP allocates UUIN, String type

1. AUTO: The primary key field is incremented automatically

① Annotation of the primary key field of the entity class @TableId(type = IdType.AUTO);

② The fields corresponding to the data table must also be set to auto-increment;

If only annotations are added, but the primary key field corresponding to the data table is not set to auto-increment, the following exception will occur!

Note: When using type = IdType.AUTOannotations , be sure to set the corresponding primary key field to [Auto Increment].

2. NONE: The primary key is not set

Since each data table has a primary key, there is almost no need to add this annotation.

3. INPUT: requires developers to manually assign values

INPUT If the developer does not set the value manually, the database assigns the primary key by setting the auto-increment method ( MP does not intervene in the generation of the primary key ). Similarly, if the database does not set the auto-increment, an exception will be thrown ; if the developer manually assigns the value, directly Store this value.

You can see userthat the primary key of the object is the idvalue null, but the object was successfully inserted into the database because the database sets the policy of auto-incrementing the primary key.

One thing to keep in mind: @TableId=(type = IdType.INPUT )MyBatis-Plus does not interfere with primary key generation under the annotation.

4. ASSIGN_ID: MyBatis-Plus automatically assigns

ASSIGN_ID If the user or developer does not assign the primary key, MyBatis-Plus uses the snowflake algorithm (also known as the snowflake algorithm) to assign an ID to the primary key field. This ID can be of Number(Long) type or String type.

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 are the machine ID), and 12bit as the serial number in milliseconds (meaning that each node can generate in every millisecond 4096 IDs), and a sign bit at the end, which is always 0. Guaranteed to be almost the only one in the world!

If you don't @TableIdannotate , it defaults to @TableId=(type = IdType.ID_WORKER), since this enumeration item has been deprecated, and is used ASSIGN_IDinstead . If the user does not assign the primary key at this time, then MP will be assigned automatically, and it is not necessary whether the database primary key field is set to auto-increment at this time.

5. ASSIGN_UUID: MyBatis-Plus assigns UUID, which must be of String type

The usage of ASSIGN_UUID is similar to ASSIGN_ID, which can represent a longer unique ID, but the data type of its primary key must be String, and MyBatis-Plus automatically generates UUID for assignment;

6. Summary

Since the primary key field of the data table cannot be empty, the general rules of the primary key generation strategy are as follows:

If the user or developer assigns the primary key, the value is directly stored; if the user does not assign the primary key, the MP (short for MyBatis-Plus) assigns the primary key; if neither the user nor the MP assigns the primary key a value, then The primary key auto-increment is set by the data table of the database to complete the assignment operation; if neither the user, MyBatis-Plus nor the database assigns the primary key, the program will throw an exception .

Guess you like

Origin blog.csdn.net/qq_41775769/article/details/123045089