Spring boot + mybatis-plus encountered irregular creation of database fields with large hump and underscores, which caused the front-end to pass parameters and the back-end to fail to receive parameters. Solution

Recently, I used springboot to connect to a sqlserver database.
Due to the age of the database, the fields in the table are not standardized.

However, Spring boot + mybatis-plus in java generates entity classes in strict accordance with the small hump format.
If it is not in the small hump format,
the @Data annotation
get set method
requests parameters at the front end.
Use this class to receive front-end parameters and find that the value of the entity class cannot be obtained.

And this entity class prints out the request parameters and does not receive them.

It would be very annoying if you manually modify the fields one by one to small humpcase

After groping, I found a lazy solution

By default, mybatis-plus is used to automatically generate entity classes. The
generated class file
insert image description here
problem:
This kind of large hump naming convention or irregular naming convention generated according to the database field uses this class to receive the parameters of the front-end request. It
is found that the parameters of the front-end cannot be received.

Found an annotation
@JsonProperty("SIsClose") by groping

The role of @JsonProperty

@JsonProperty acts on the properties of the entity class, serializes the name of the property into another name, and the name of the property and the name in @JsonProperty("") is a mapping relationship. For example, the database uses an underscore to name the field user_id, but the attribute name of the entity class is the hump-style userId. By adding @JsonProperty("user_id") to the userId, the output field can be controlled when the database query is returned to the client.

 简单的说,就是在给实体类属性名起别名,应用在不同的场合。

The resulting entity class
insert image description here

But if there are too many fields in the database,
adding this annotation one by one will also cause headaches.

Finally, modify the template of mybatis-plus's automatic generation of entity classes
and add one
insert image description here
, so that the automatic generation of entity classes
will automatically add this annotation for me
, which is very convenient

after testing

The naming is not standardized
, so that using this class to connect to the front-end parameters will not get the corresponding data.
However
, if you use the java set method to manually assign a value to an irregularly named field in this class, you
can use the get method to obtain it.
For example, the new object and then get set is perfectly fine to use

So I feel that springboot must use the small hump naming convention when receiving front-end parameters

So in order to solve the problem of receiving parameters, just add @JsonProperty("SIsClose") annotation to the field

The body form of the request parameter after consulting the data

1. Comply with the Java Api specification and use the hump naming format.
2. The attributes of the java class must conform to the code specification and use the hump attribute

Springboot uses the jackson component by default, and you only need to add corresponding annotations to the properties with irregular names to map the
jackson component: com.fasterxml.jackson.annotation.JsonProperty;

Note: In this form without @RequestBody, using the @JsonProperty annotation will not work

Guess you like

Origin blog.csdn.net/Drug_/article/details/129306062