Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of id self-increasing when mybatis-plus adds data

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to solve the problem of id self-increasing when adding data in mybatis-plus. I hope you will get something after reading this article. Let's discuss it together.

Id self-increasing problem when mybatis-plus adds data

Mybatis-plus inserts data, and the id auto-increment column becomes very long. For example, the following figure:

One encountered in mybatis-plus, adding data to the database, while the id column in the database is a self-incrementing column

Using the insert method that comes with plus to add to the database, id changes for a long time.

Database id field data type is long

Solution

Add a note to the self-increasing id field of the entity class

When mybatis plus inserts a record, there are several ways:

Boolean insert (T var1); boolean insertAllColumn (T var1); boolean insertBatch (List var1); boolean insertBatch (List var1, int var2); boolean insertOrUpdateBatch (List var1); boolean insertOrUpdateBatch (List var1, int var2)

In the entity at that time, when the primary key generation method does not set the generation method, the default is self-increment. So when you set the value of the primary key, you still cannot save the primary key.

@ TableId (value = "id") private Integer id

We can set how the primary key is generated.

@ TableId (value = "id", type = IdType.INPUT) is manually entered by the primary key

The types of primary key generation are as follows (IdType):

AUTO (0, database ID self-increment)

INPUT (1, "user input ID")

ID_WORKER (2, "globally unique ID")

UUID (3, "globally unique ID")

NONE (4, "this type is not set primary key type")

ID_WORKER_STR (5, "string globally unique ID")

Description of the primary key self-increment assignment mechanism for inserting records

In the current project practice, the table design generally uses the self-increasing primary key, so it will involve a scenario processing of getting the primary key of the inserted data after data insertion.

For this situation, the mybatis framework encapsulates and provides support.

Code description

Through debug, follow up the execution path and view the corresponding logic code.

1. Find the execution entrance

There are three parameter assignments here, but the id assignment is not included. Id is incremented by the database.

Here you see a KeyGenerator type, the name name is very good, self-explanatory is very good.

2. Look at the execution logic of keyGenerator

You can see here that the id of the parameter type is still empty, but the sql execution in the figure above is over.

3. Follow the logic execution

This is part of the logic, not yet, but look at the method name assign, it's coming.

4. Move on to the next level of logic

At this time, id is still null.

5. Move on to the next level of logic

Here we see the action of set. Before execution, id is still null.

6. Key values

In the assignment logic of Object value, you can see that the data is obtained from rs, and the value is 184,

7. Key assignment

After executing the set, the id of the type object that inserted the data has a value.

Here, the assignment of id is over since the increment.

Summary

The compensation assignment of self-increasing id makes up for the deficiency of the same custom id. But why can the framework do that? See the notes below.

This is the standard interface of JDBC, which provides this port, and after the return of sql execution, you can bring the information of self-increment id, so the application layer framework can perform assignment and avoid secondary query.

Whether the actual project uses a self-increasing primary key or a custom assignment primary key, we need to fully take into account the advantages and disadvantages of both combined with the actual situation. The advantages and disadvantages can be referred to as follows:

This approach uses the self-incrementing field provided by the database as the self-incrementing primary key, and its advantages are:

Self-increasing primary key

This method uses the self-increment field provided by the database as the self-increment primary key.

The advantages are:

1. The database is numbered automatically, the speed is fast, and it grows incrementally, and it is stored sequentially, which is very beneficial for retrieval.

2. Digital type, small footprint, easy to sort, and convenient to transfer in the program.

3. If you add records through a non-system, you don't have to specify this field and you don't have to worry about duplicate primary keys.

Disadvantages:

1. Because of automatic growth, it will be troublesome to manually insert the records of the specified ID, especially when the system is integrated with other systems and data import is needed, it is difficult to ensure that the ID of the original system does not have primary key conflicts (provided that the old system is also digital).

2. If there are frequent operations to merge tables, it may be difficult to deal with distributed data tables with duplicate primary keys.

3. When the amount of data is very large, it will cause the operation of querying the database to become slow. At this point, you need to split the database horizontally and divide it into different databases, so when you add data, each table will grow itself, resulting in a primary key conflict.

UUID

Advantages:

1, can guarantee independence, the program can be migrated between different databases, the effect will not be affected. Make sure that the generated ID is not only table-independent, but also library-independent, which is especially important when you want to split the database.

Disadvantages:

1. Compared with the INT type, it takes more space to store a UUID.

2. After using UUID, URL appears lengthy and unfriendly.

3. The performance of Join is lower than that of int.

4. UUID as the primary key will be added to other indexes on the table, thus reducing performance.

After reading this article, I believe you have a certain understanding of "how to solve the problem of id self-increasing when mybatis-plus adds data". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report