当前位置:网站首页>Integer or int? How to select data types for entity classes in ORM
Integer or int? How to select data types for entity classes in ORM
2022-07-07 09:20:00 【Wow, it's a small dish】
The company's ORM It's using Spring Data JPA, The former owner's is mybatis, The code specification of the last company specifically ordered , Entity classes use wrapper classes , Do not use basic data types . So a little question arises ,ORM What type of entity class should be used is right .
The difference between packaging class and basic class
1. The biggest difference between wrapper classes and basic data types is , A wrapper class is an object , But the basic data type has only one value . in other words , Wrapper classes have a place to allocate memory space in heap memory , But the value of basic data type only exists in stack memory .
2. The wrapper class can be null, But the basic type cannot be null.
int a; // Even if you don't give it literal , There will also be default values 0
Integer b; // Because there is no instantiated object , therefore b by null
Remember the difference , This difference is an important reference reason for entity class selection !!!
Why do we need packing
The emergence of packaging , Personal feeling is that generics must pass a Object.List<E>, If there is only basic data type , The use of generics can be very limited . As for why generics must be passed Object, When I have time, I will continue to sort it out and specifically say .
JDK Where we can't see , Help us with packing and unpacking .
// Automatic boxing
List<Integer> numList = new ArrayList<Integer>();
numList.add(1); // This is the time , It's actually numList.add(Integer.valueOf(1));
// Automatic dismantling
Integer a = new Integer(3);
if(a==3){ // First a.intValue()l; Automatic unpacking into basic type , And then 3 Compare the literal values for equality
....
}
A comparison between technical data type and packaging class == When , It will automatically unpack and compare the value , But if it's packaging and packaging , for example :new Integer(200)==new Integer(200)?, The comparison is Memory address , Therefore, it is necessary to use equals.
Entity class data type selection strategy
1. When a certain value must not be null when , Basic data types can be used , for example : Status flags , Classification flag bit , As long as there is this data, the field value must have a value , And it needs to be transmitted to the front end .
Because if Entity The basic data type is used in the class private int number; But the database is null When , If you return one null, Interface report 500,Null value was assigned to a property.
So be careful when using .
2.JpaBuilder Of Specification There's a... At the bottom Root Generic interface , I mentioned earlier , Whenever generics are involved , The performance of the wrapper class is definitely better than the basic data type , Even if JDK Can do automatic unpacking and packing , It is absolutely impossible for null.
therefore , In subsequent extensions , If specifically for null If there is a treatment or agreement , Basic data types are impossible .
MybatisPlus Of QueryWrapper It's also a problem , and QueryWrapper Yes null The package is more perfect , Can't pass on null It will bring a lot of problems .
in summary , If there is no requirement , Packaging is preferred .
When the data is required to never be empty , There are two solutions
(1) You can add constraints when creating tables ,
CREATE TABLE `onetable` (
`id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT '' COMMENT ' Primary key id',
`node` bigint(20) DEFAULT 0 not null COMMENT ' Vehicle brand ',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=DYNAMIC;
(2)Entity Class directly assigns the initial value :
@Data
public class Entity(){
...
private Integer number = 0;
...
}
边栏推荐
- Skill review of test engineer before interview
- Do you have any certificates with high gold content?
- UnityShader入门精要个人总结--基础篇(一)
- What is the use of PMP certificate?
- Systick tick timer
- Simulation volume leetcode [general] 1557 The minimum number of points that can reach all points
- Run can start normally, and debug doesn't start or report an error, which seems to be stuck
- C语言指针(下篇)
- Original collection of hardware bear (updated on June 2022)
- 5A summary: seven stages of PMP learning
猜你喜欢

Jenkins automated email

Locust performance test 3 (high concurrency, parameter correlation, assembly point)

【SVN】SVN是什么?怎么使用?

C language pointer (special article)

Error: selenium common. exceptions. WebDriverException: Messag‘geckodriver‘ execute

PMP Exam Preparation experience systematically improve project management knowledge through learning

C language pointer (exercises)

Led analog and digital dimming

Data association between two interfaces of postman

端口复用和重映像
随机推荐
H3C vxlan configuration
The use of recycling ideas
Summary of PMP learning materials
Postman data driven
Ppt template and material download website (pure dry goods, recommended Collection)
Locust performance test 4 (custom load Policy)
端口复用和重映像
Cesium does not support 4490 problem solution and cesium modified source code packaging scheme
Jenkins modifies the system time
NVIC interrupt priority management
OpenGL frame buffer
2022-07-06 unity core 9 - 3D animation
Jenkins automated email
Detailed learning notes of JVM memory structure (I)
Several stages of PMP preparation study
[SVN] what is SVN? How do you use it?
Pytest installation (command line installation)
Regularly modify the system time of the computer
Locust performance test 3 (high concurrency, parameter correlation, assembly point)
Synchronized underlying principle, volatile keyword analysis