当前位置:网站首页>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 .
 Insert picture description here
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.
 Insert picture description here
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;
	...
}
原网站

版权声明
本文为[Wow, it's a small dish]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130626221298.html