In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to solve the problem of too long database id generated automatically by Mybatis-Plus". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to solve the problem of too long database id generated automatically by Mybatis-Plus".
First, problems
As a new development engineer who used mybatis-plus for the first time, he encountered a problem in the process of project development.
When you use the mybatis-generate that comes with mybatis-plus to generate the DO file, as shown in the following figure
The DO class is decorated by the annotation @ Table, and the primary key id is decorated by the annotation @ Id,@GeneratedValue. However, when using such a default DO for database operations, the primary key id that causes the database to be automatically generated is too long, as shown below
There are some problems with such a 19-bit id:
1) after the front end gets such an id, the Number precision will be lost, resulting in a change in the id value, resulting in inconsistent id between the front end and the back end, which makes it impossible to operate with id.
2) the index and record structure of the InnoDB storage engine is as follows:
The structure of its index and record is as follows:
(1) the primary key index is stored with the record; when InnoDB queries through the primary key index, it can locate the row record directly.
(2) ordinary index stores primary key (this is no longer a pointer)
In this way, when the primary key id is a long value, each index stores this value. In the case of large amount of data and precious memory, the limited buffer of MySQL will reduce the index and data stored, the disk space occupied by the index will also increase, and the probability of disk IO will increase.
II. Solutions
By asking the brothers and developers, we have solved this problem. The solution is as follows:
Change the comment of the DO class to @ TableName and the comment of the primary key id to @ TableId, so that the automatically generated primary key id is the normal number of digits.
As to why this is so, I have come to a conclusion by consulting the data.
Third, principle
1. First, take a look at the use of @ GeneratedValue. @ GeneratedValue is one of the JPA annotations. JPA maps hibernate entities through annotation. The hibernate primary key based on annotation is identified as @ Id, and its generation rule is set by @ GeneratedValue.
JPA provides four standard uses, as can be seen from the source code of @ GeneratedValue:
@ Target ({METHOD,FIELD}) @ Retention (RUNTIME) public @ interface GeneratedValue {GenerationType strategy () default AUTO; String generator () default "";}
GenerationType contains four strategies:
Public enum GenerationType {/ / uses a specific database table to hold the primary key. TABLE, / / generates the primary key based on the sequence of the underlying database, provided that the database supports the sequence. SEQUENCE, / / the primary key is automatically generated by the database (mainly auto-growing) IDENTITY, and / / the primary key is controlled by the program. AUTO; private GenerationType () {}}
The AUTO type defaults to AUTO if the primary key generation policy is not specified when the primary key is specified.
That is,
@ Id # default generation policy is AUTO
The effect is equivalent to
@ Id @ GeneratedValue (strategy = GenerationType.AUTO)
Thus, the automatically generated id has nothing to do with annotations, and it may be the primary key generation logic of mybatis-plus.
By querying the documents of mybatis-plus
It is found that the default primary key generation of mybatis-plus is a globally unique UUID, which causes the generated id to be too long.
And officials also provide solutions, as shown in the figure below.
But this will only prevent the loss of precision when receiving at the front end, and it does not solve my problem.
According to the documentation, you can come up with a new solution without changing the DO class code:
Just set the id-type configuration described in the document to 0.
A brief description of the problems of Mybatis-Plus id primary key generation
Because mybatis-plus automatically inserts an id into the entity object, whether you encapsulate it or not, sometimes something unexpected happens
The default is to generate a long numeric string (different encodings may end with letters)
Error
Ested exception is org.apache.ibatis.reflection.ReflectionException: Could not set property 'id' of' class com.xxx' with value '1110423703487479810' Cause: java.lang.IllegalArgumentException: java.lang.ClassCastException@14041406
This is roughly due to the fact that an id1110423703487479810 is automatically generated, but cannot be put into the integer
Solution one
1. Modify id field type
Change the id field type to long, which ensures that there are enough bits to put into the generated id
two。 Adjust the database id field type
Change the length of the id field of the database (to 20 bits)
Solution two
If you want to use the self-added id, you need to turn off the id-generated function of mybatis-plus.
Add comment
Just add the following note to the id field
@ TableId (value = "id", type = IdType.AUTO)
Introduction to other type types
AUTO:AUTO (0, database ID self-increment)
INPUT:INPUT (1, "user input ID")
ID_WORKER:ID_WORKER (2, "globally unique ID")
UUID:UUID (3, "globally unique ID")
NONE:NONE (4, "this type is not set primary key type")
ID_WORKER_STR:ID_WORKER_STR (5, "string globally unique ID")
Thank you for your reading, the above is the content of "how to solve the problem of long database id generated automatically by Mybatis-Plus". After the study of this article, I believe you have a deeper understanding of how to solve the problem of long database id generated automatically by Mybatis-Plus, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.