当前位置:网站首页>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;
...
}
边栏推荐
- Jenkins task grouping
- Original collection of hardware bear (updated on May 2022)
- Leetcode daily questions (2316. count unreachable pairs of nodes in an undirected graph)
- Several common database connection methods
- MySQL common statements
- Postman data driven
- Count the number of words in the string c language
- Skill review of test engineer before interview
- Locust performance test 2 (interface request)
- Three updates to build applications for different types of devices | 2022 i/o key review
猜你喜欢
C语言指针(中篇)
Port multiplexing and re imaging
C语言指针(特别篇)
STM32串口寄存器库函数配置方法
STM32的时钟系统
寄存器地址名映射
Locust performance test 3 (high concurrency, parameter correlation, assembly point)
Skill review of test engineer before interview
Expérience de port série - simple réception et réception de données
Pycharm importing third-party libraries
随机推荐
Do you have any certificates with high gold content?
Led analog and digital dimming
PMP examination experience sharing
The use of recycling ideas
Run can start normally, and debug doesn't start or report an error, which seems to be stuck
When inputting an expression in the input box, an error is reported: incorrect string value:'\xf0\x9f... ' for column 'XXX' at row 1
【ChaosBlade:节点磁盘填充、杀节点上指定进程、挂起节点上指定进程】
Postman interface debugging method
What is the rating of Huishang futures company? Is it safe to open an account? I want to open an account, OK?
Zen - batch import test cases
Locust performance test 4 (custom load Policy)
[chaosblade: node disk filling, killing the specified process on the node, suspending the specified process on the node]
Serializer & modelserializer of DRF serialization and deserialization
Upgrade Alibaba cloud RDS (relational database service) instance to com mysql. jdbc. exceptions. Troubleshooting of jdbc4.communicationsexception
C语言指针(习题篇)
stm32和电机开发(从单机版到网络化)
Original collection of hardware bear (updated on June 2022)
嵌套(多级)childrn路由,query参数,命名路由,replace属性,路由的props配置,路由的params参数
超十万字_超详细SSM整合实践_手动实现权限管理
Synchronized underlying principle, volatile keyword analysis