当前位置:网站首页>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;
...
}
边栏推荐
- C语言指针(特别篇)
- Troublesome problem of image resizing when using typora to edit markdown to upload CSDN
- Unittest simple project
- Reflections on the way of enterprise IT architecture transformation (Alibaba's China Taiwan strategic thought and architecture practice)
- LeetCode每日一题(2316. Count Unreachable Pairs of Nodes in an Undirected Graph)
- Record of structured interview
- Mysql database lock learning notes
- C language pointer (special article)
- Postman interface test (I. installation and use)
- Variable parameter of variable length function
猜你喜欢

寄存器地址名映射

Mysql database lock learning notes

STM32串口寄存器库函数配置方法

C语言指针(习题篇)

Postman interface test (II. Set global variables \ sets)

Led analog and digital dimming
![[istio introduction, architecture, components]](/img/2b/f84e5cdac6ed9b429e053ffc8dbeb0.png)
[istio introduction, architecture, components]

Why is access to the external network prohibited for internal services of the company?

外部中断实现按键实验

Jemter operation
随机推荐
STM32的时钟系统
5A summary: seven stages of PMP learning
寄存器地址名映射
Serial port experiment - simple data sending and receiving
Interpretation of MySQL optimization principle
Cesium does not support 4490 problem solution and cesium modified source code packaging scheme
How can I apply for a PMP certificate?
华为HCIP-DATACOM-Core_03day
Sublime Text4 download the view in bower and set the shortcut key
OpenGL frame buffer
JVM 垃圾回收 详细学习笔记(二)
Pytest+request+allure+excel interface automatic construction from 0 to 1 [five nails / flying Book notice]
Panel display technology: LCD and OLED
JVM 内存结构 详细学习笔记(一)
Storage of data in memory
Reading notes of pyramid principle
Simulation volume leetcode [general] 1567 Length of the longest subarray whose product is a positive number
Cesium load vector data
How does the project manager write the weekly summary and weekly plan?
二叉树高频题型