MyBatisPlus (14) Primary key strategy (snowflake algorithm + UUID + primary key auto-increment + user input)

Description

MyBatis Plus integrates a variety of primary key strategies to help users quickly generate primary keys.

  1. Snowflake algorithm ID (default strategy) (recommended)
  2. UUID
  3. Auto-increment ID
  4. The user enters the ID (the user must manually pass in the ID every time he inserts data)

Snowflake algorithm ID: IdType.ASSIGN_ID (recommended)

By default, the snowflake algorithm ID is used globally. In other words, when the id field does not specify any primary key strategy, the inserted data is the ID generated by the snowflake algorithm.

Annotations

If the snowflake algorithm ID is used globally, this annotation does not need to be added.

@TableId(type = IdType.ASSIGN_ID)

Code

package com.example.web.entity;

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

@Data
public class User {<!-- -->
    @TableId(type = IdType.ASSIGN_ID)
    private Long id;
    private String name;
    private Integer age;
    private String email;
}
 @Test
    public void insert() {<!-- -->
        User user = new User();
        user.setName("Zhao Yi");
        user.setAge(26);
        user.setEmail("[email protected]");

        mapper.insert(user);
    }

Effect

Snowflake algorithm ID can ensure ordering.

Insert four pieces of data in sequence

 @Test
    public void insert() {<!-- -->
        insert("Zhao Yi");
        insert("Zhao Er");
        insert("Zhao San");
        insert("Zhao Si");
    }


    public void insert(String name) {<!-- -->
        User user = new User();
        user.setName(name);
        user.setAge(26);
        user.setEmail("[email protected]");

        mapper.insert(user);
    }

UUID: IdType.ASSIGN_UUID (not recommended)

Annotations

@TableId(type = IdType.ASSIGN_UUID)

Code

package com.example.web.entity;

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

@Data
public class User {<!-- -->
    @TableId(type = IdType.ASSIGN_UUID)
    private Long id;
    private String name;
    private Integer age;
    private String email;
}
 @Test
    public void insert() {<!-- -->
        User user = new User();
        user.setName("Zhao Yi");
        user.setAge(26);
        user.setEmail("[email protected]");

        mapper.insert(user);
    }

Effect


Reason for not recommending

Continuously inserted data cannot be guaranteed to be in order using UUID.

In a system with heavy load and high concurrency, it is necessary to ensure the orderliness of primary key ID to ensure the performance of the database. The IDs are out of order, which will cause data to be inserted into the middle of the table, resulting in frequent disk IO.

Examples are as follows

Code

 @Test
    public void insert() {<!-- -->
        insert("Zhao Yi");
        insert("Zhao Er");
        insert("Zhao San");
        insert("Zhao Si");
    }


    public void insert(String name) {<!-- -->
        User user = new User();
        user.setName(name);
        user.setAge(26);
        user.setEmail("[email protected]");

        mapper.insert(user);
    }

Data in database

Auto-increment ID: IdType.AUTO

For this type, please make sure that the database is set with ID auto-increment, otherwise it will be invalid (an error will be reported).

For error reporting information, see “Error Reporting Examples” at the end of the article

Annotations

@TableId(type = IdType.AUTO)

Code and testing

package com.example.web.entity;

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

@Data
public class User {<!-- -->
    @TableId(type = IdType.AUTO)
    private Long id;
    private String name;
    private Integer age;
    private String email;
}
 @Test
    public void insert() {<!-- -->
        User user = new User();
        user.setName("Zhao Yi");
        user.setAge(26);
        user.setEmail("[email protected]");

        mapper.insert(user);
    }

Effect


User input ID: IdType.INPUT

Annotations

@TableId(type = IdType.INPUT)

Code

package com.example.web.entity;

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

@Data
public class User {<!-- -->
    @TableId(type = IdType.INPUT)
    private Long id;
    private String name;
    private Integer age;
    private String email;
}
 @Test
    public void insert() {<!-- -->
        User user = new User();
        user.setId(9L);
        user.setName("Zhao Yi");
        user.setAge(26);
        user.setEmail("[email protected]");

        mapper.insert(user);
    }

Effect

After specifying the id, the data was successfully inserted

Error report if ID is not specified

If the id is not specified, that is, the setId() method is not called, an error will be reported:

Column id’ cannot be null

Supplement: Error report example (IdType.AUTO)

When the table ID in the MySQL database does not increase by itself, but the ID in the code does, if a piece of data is added at this time, an error will be reported.

Entity class

package com.example.web.entity;

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

@Data
public class User {<!-- -->
    @TableId(type = IdType.AUTO)
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

Insert data method

 @Test
    public void insert() {<!-- -->
        User user = new User();
        user.setName("Zhao Yi");
        user.setAge(26);
        user.setEmail("[email protected]");

        mapper.insert(user);
    }

Error message

Algorithm introduction

Snowflake algorithm


UUID (MyBatis Plus)

UUID (General)