This article is the sixth in the mybatis series. Please visit the following website for the first five articles.
- Integration of SpringBoot quickly start to add, delete, change and check
- MybatisPlus part 2 – Application and summary of conditional builders
- MybatisPlus 3 – Custom SQL
- Table pagination and drop-down page query
- MybatisPlus chapter 5 -Active Record mode introduction
Mybatis Plus provides three ways to set the primary key generation strategy. Their order of precedence is: local annotations > Global > Default (Snowflake algorithm). Let’s introduce them one by one
Default primary key generation strategy: Snowflake algorithm
Mybatis Plus uses the snowflake algorithm by default if it does not do any primary key policy configuration. This policy generates the primary key ID based on the Snowflake algorithm. The primary key ID is either Long or String (BIGINT and VARCHAR in MySQL). The strategy to use the method of interface IdentifierGenerator nextId (default implementation class for DefaultIdentifierGenerator snowflakes algorithm)
The Snowflake algorithm is Twitter’s open-source distributed ID generation algorithm that results in an ID of type Long. The idea is to use 41bit as the number of milliseconds, 10bit as the machine ID (5bit data center, 5bit machine ID), 12bit as the serial number within milliseconds (meaning each node can generate 4096 ids per millisecond), and finally a symbol bit, always 0.
2. Customize primary key policies
After Mybatis -plus3.3.0, there are five primary key generation strategies.
Public enum IdType {/** * the database ID must be self-increasing. The database, such as MySQL, supports the self-increasing primary key. /** * This is an unset primary key type. By default, snowflake algorithm is used to generate */ NONE(1). </p> */ INPUT(2), /* Only if the ID of the inserted object is empty, it will be automatically filled. */ ID_WORKER(3), /** * global unique ID (UUID, */ UUID(4), /** * string globally unique ID(idWorker string representation), database must also ensure the same character type */ ID_WORKER_STR(5); }Copy the code
Third, local annotation configuration policy
We set the primary key policy for primary keys using annotations as
@TableId(type = IdType.AUTO)
private long userId;
Copy the code
4. Configure global policies
mybatis-plus:
global-config:
db-config:
id-type: autoCopy the code
Five, expand the use
5.1.INPUT User INPUT ID policy usage
Input (user Input ID), this ID source can have two kinds
- The user sets the ID and sets the primary key value before the INSERT
- Some databases have sequences, such as Oracle, SQLServer, etc., for these databases we can use the sequence to fill in the ID field
Mybatis-Plus has the following built-in database primary key sequences (if the built-in support does not meet your needs, you can implement the IKeyGenerator interface for extension) :
- DB2KeyGenerator
- H2KeyGenerator
- KingbaseKeyGenerator
- OracleKeyGenerator
- PostgreKeyGenerator
For example, use Oracle Sequence as follows: Add the @bean
@Bean
public OracleKeyGenerator oracleKeyGenerator(){
return new OracleKeyGenerator();
}Copy the code
Set the primary key policy to idtype.input. Set the primary key policy to IDtype.input.
@Data
@KeySequence(value = "SEQ_USER" , clazz = Long.class)
public class User {
@TableId(value = "ID",type = IdType.INPUT)
private Integer id;
Copy the code
Welcome to my blog, where there are many fine collections
- This article is reprinted with a credit (must be accompanied by a link, not only the text) : Antetokounmpo blog.
Feel helpful to you, help me like, share! Your support is my inexhaustible creative power! . In addition, the author recently a period of time output as follows boutique content, looking forward to your attention.
- Spring Boot2.0 by Hand
- Spring Security- JWT-OAUTH2
- RBAC Authority Management System for Actual Combat Front-end and Back-end Separation
- “Actual SpringCloud Micro-service from Bronze to King”
- VUE Series