This is the 7th day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

Insertinsert

// Test the insert
@Test  
public void testInsert(a) {  
    User user = new User();  
    user.setName("zbc");  
    user.setAge(20);  
    user.setEmail("[email protected]");  
  
    // Help us automatically generate id
    int result = userMapper.insert(user);  
    // The number of rows affected
    System.out.println(result);  
    // The id is automatically backfilled
    System.out.println(user);  
}
Copy the code
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@18715bb] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@1180105925 wrapping com.mysql.cj.jdbc.ConnectionImpl@2c16677c] will not be managed by Spring
==>  Preparing: INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? ) 
==> Parameters: 1456795944390905857(Long), zbc(String), 20(Integer), [email protected](String)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@18715bb]
1
User(id=1456795944390905857, name=zbc, age=20, [email protected])
Copy the code

The id inserted into the database is a globally unique ID. 1456795944390905857 is generated based on the Snowflake algorithm.

Primary key generation policy

There is a special annotation in MybatisPlus about the primary key generation strategy (@tableID)

@Documented  
@Retention(RetentionPolicy.RUNTIME)  
@Target({ElementType.FIELD})  
public @interface TableId {  
    String value(a) default "";  
  
    IdType type(a) default IdType.NONE;  
}
Copy the code

IdType indicates the type of the policy

public enum IdType {  
    AUTO(0),  
    NONE(1),  
    INPUT(2),  
    ASSIGN_ID(3),  
    ASSIGN_UUID(4);  
  
    private final int key;    
}
Copy the code
  • AUTODatabase:IDSince the increase
    • Need to be added on the entity class field@TableId(type = IdType.AUTO)
    • Database fields must also be incremented.
    • Again, we found thatidSince the increase1
  • NONE: Default scheme, no primary key is set
  • INPUT: The primary key is set manually before insertion and needs to be configured once it is setidthe
  • ASSIGN_IDDistribution of:IDThe primary key type isNumber(LongandInteger) orString) (Since 3.3.0), using interfacesIdentifierGeneratorThe method ofnextIdThe default implementation class isDefaultIdentifierGeneratorSnowflake algorithm)
  • ASSIGN_UUIDDistribution of:UUID, the primary key type isString(Since 3.3.0), using interfacesIdentifierGeneratorThe method ofnextUUID(the defaultdefaultMethods)
  • ID_WORKER: distributed globally uniqueID, primary key typeLong, you are advised to use it laterASSIGN_ID
  • UUID:32forUUIDString, primary key typeString, you are advised to use it laterASSIGN_UUID
  • ID_WORKER_STR: distributed globally uniqueID, primary key typeString, you are advised to use it laterASSIGN_ID

Snowflake algorithm: Snowflake is an open source distributed ID generation algorithm for Twitter 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. Can guarantee almost unique in the world.

UUID: a 128bit number, or 32 hexadecimal characters separated by -.

  • The time stamp +UUIDThe version number is divided into three sections16A character (60bit+4bit),
  • Clock SequenceNumber with reserved field, occupy4A character (13bit+3bit),
  • Node id accounting12A character (48bit),
  • Example:3F2504E0-4F89-11D3-9A0C-0305E82C3301