Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities
This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money
The insert
insert
@test public void testInsert(){User User = new User(); user.setName("xxx"); user.setAge(3); user.setEmail("[email protected]"); int result = userMapper.insert(user); Id system.out. println(result); // Number of affected rows system.out.println (user); // The id is automatically generated}Copy the code
Default value of database insert ID: global unique ID
Primary key generation policy
Default ID_WORKER Global unique ID
Distributed system is a unique id generated: www.cnblogs.com/haoxinyue/p…
Snowflake algorithm:
Nowflake is Twitter’s open source distributed ID generation algorithm 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 the world unique
The primary key from the increase
We need to configure primary key increment
@tableId (type = idType.auto)
2, the database field must be self-increment
3, the test
The rest of the source code interpretation
AUTO(0),// database ID increment NONE(1),// primary key INPUT(2) is not set,// manually enter the string representation of ID_WORKER (3),// default global ID UUID(4),// global unique ID ID_WORKER_STR(5); // the string representation of ID_WORKERCopy the code
user.setId(6L);/ / input id
@TableId(type = IdType.INPUT )
Copy the code