How to deal with the inconsistency between the Mybatis entity class attribute name and the field name in the table

I. Introduction

Recently, a student of Brother Yao went out for an interview and was asked "What should I do if the attribute name of the Mybatis entity class is inconsistent with the field name in the table?" This is actually a very classic interview question. Next, Brother Yao will explain it in detail for you. This interview question.

2. Analysis

2.1 Consequences of inconsistent entity classes and field names

In order to clarify this issue, we first design the following case.

The design of the table is shown in the figure below:

image.png

The entity class is shown in the figure below:

image.png

The mapper.xml file looks like this:

image.png

According to the method of querying the TbAccount object by the primary key, we found that [ only the fields in the database and the attribute names of the entity class match, can be found out!

image.png

So how to solve the above problem? Brother Yao proposed the following two solutions for everyone.

2.2 Solution 1: Replace resultType with resultMap

We need to create resultMap first in the mapper.xml file.

image.png

Then in the select method node of the mapper.xml file, replace resultType with resultMap.

image.png

Once again, the method of querying the TbAccount object according to the primary key, we will find at this time that even if [the column name of the database and the attribute name of the entity class do not match the field, it can be found out ]

image.png

2.3 Solution 2: Configure the mapping relationship between underscore and hump

The second solution is to first add the underscore and hump mapping configuration to the mybatis core configuration file.

image.png

Then restore the resultMap in the select query node in the mapper.xml file to resultType.

image.png

Once again, according to the method of querying the TbAccount object based on the primary key, we will find that [ the column name of the database and the attribute name of the entity class do not match the field, and it can still be found out at this time ]

image.png

3. Conclusion

In the past, students often asked Brother Yao , [Since there will be problems with the attribute names of the entity class and the column names of the database, when we create the entity class, let the attribute names of the entity class be consistent with the field names of the database. Does that solve the problem? ]

In fact, this is the case. Now all companies have inconsistent naming conventions for Java variables and database column names! Java's variable naming convention is camel case naming, and database column naming conventions are underscore-differentiated word naming, so that the inconsistency between Java entity classes and database column names becomes a norm.

In order to avoid problems caused by such inconsistencies as much as possible, we can refer to Alibaba's development regulations when developing projects, as shown in the following figure:

image.png

image.png

Now you know how to solve and answer this interview question? If you have other questions, you can leave me a message in the comment area.

Qianfeng Education Java Introduction Full Set of Video Tutorials (java core technology, suitable for java zero foundation, necessary for self-study of Java)

Guess you like

Origin blog.csdn.net/longz_org_cn/article/details/132096855