当前位置:网站首页>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;
...
}
边栏推荐
- PMP Exam Preparation experience systematically improve project management knowledge through learning
- Serial port experiment - simple data sending and receiving
- DRF defines views and routes
- Analysis of Hessian serialization principle
- MySql数据库-事务-学习笔记
- 5A summary: seven stages of PMP learning
- [chaosblade: delete pod according to the tag, pod domain name access exception scenario, pod file system i/o failure scenario]
- Pycharm create a new file and add author information
- Entity of cesium data visualization (Part 1)
- Postman interface debugging method
猜你喜欢
Postman interface debugging method
PMP Exam Preparation experience systematically improve project management knowledge through learning
PMP examination experience sharing
Mysql database lock learning notes
Pycharm importing third-party libraries
Data association between two interfaces of postman
Run can start normally, and debug doesn't start or report an error, which seems to be stuck
正则匹配以XXX开头的,XXX结束的
答案在哪里?action config/Interceptor/class/servlet
Expérience de port série - simple réception et réception de données
随机推荐
C语言指针(下篇)
超十万字_超详细SSM整合实践_手动实现权限管理
DRF defines views and routes
NVIC中断优先级管理
Jmeters use
Troublesome problem of image resizing when using typora to edit markdown to upload CSDN
DRF authentication, permissions, and flow restrictions (only for views in DRF)
How can I apply for a PMP certificate?
Pytest+request+allure+excel interface automatic construction from 0 to 1 [five nails / flying Book notice]
十二、排序
Skill review of test engineer before interview
答案在哪里?action config/Interceptor/class/servlet
Common operating commands of Linux
PMP Exam Preparation experience systematically improve project management knowledge through learning
MySQL master-slave delay solution
Common short chain design methods
串口實驗——簡單數據收發
Jenkins modifies the system time
2020 year end summary
MySQL common statements