This is the 7th day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021
Insert
insert
// 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
AUTO
Database:ID
Since the increase- Need to be added on the entity class field
@TableId(type = IdType.AUTO)
- Database fields must also be incremented.
- Again, we found that
id
Since the increase1
- Need to be added on the entity class field
NONE
: Default scheme, no primary key is setINPUT
: The primary key is set manually before insertion and needs to be configured once it is setid
theASSIGN_ID
Distribution of:ID
The primary key type isNumber
(Long
andInteger
) orString
) (Since 3.3.0
), using interfacesIdentifierGenerator
The method ofnextId
The default implementation class isDefaultIdentifierGenerator
Snowflake algorithm)ASSIGN_UUID
Distribution of:UUID
, the primary key type isString
(Since 3.3.0
), using interfacesIdentifierGenerator
The method ofnextUUID
(the defaultdefault
Methods): distributed globally uniqueID_WORKER
ID
, primary key typeLong
, you are advised to use it laterASSIGN_ID
:UUID
32
forUUID
String, primary key typeString
, you are advised to use it laterASSIGN_UUID
: distributed globally uniqueID_WORKER_STR
ID
, 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 +
UUID
The version number is divided into three sections16
A character (60bit
+4bit
), Clock Sequence
Number with reserved field, occupy4
A character (13bit
+3bit
),- Node id accounting
12
A character (48bit
), - Example:
3F2504E0-4F89-11D3-9A0C-0305E82C3301