Introduction of mapping method between entity class attribute and data list in mybatis

Mybatis is not as automated as hibernate. It uses the @ column annotation or directly uses the attribute name of the entity class as the data column name. Instead, it needs to specify the entity class attribute and

The mapping relationship between column names in the data table is not used to people who are used to hibernate. Fortunately, after exploration, three methods to establish mapping relationship have been found, among which there are always relatively simple ones.

First, define an entity class as follows:

1. Through the resultmap in the XML Mapping File

This method is the most common, similar to the following:

The mapping relationship is established through the ID tag and result tag inside. The entity class attribute and the column name of the data table are specified by property and column respectively.

2. Annotate @ results and @ result

These two annotations correspond to the tags in the XML file:

@Results corresponds to resultmap @ result corresponds to result

These two annotations are applied at the method level, that is, on the mapper method, as follows:

Disadvantages:

Because annotations are specific to methods, each database operation method in mapper must have the same annotation to complete the establishment of mapping relationship, resulting in many repeated configurations;

To avoid the problem of duplicate configuration, you can configure the resultmap in the XML configuration file, and then reference the resultmap through the ID attribute in @ result,

However, it is troublesome (because two configuration methods are used), so it is better to directly use the resultmap configuration method based on XML;

3. Complete mapping through attribute configuration

The most unfamiliar thing for users is to complete the mapping by configuring attributes. Mybatis provides us with a mapping method. If the attribute naming follows the hump naming method and the data column name follows the underscore naming method,

Then you can use this method, similar to the following:

Username corresponds to user_ name; Userid corresponds to user_ id;

The configuration code is as follows:

4. Complete the mapping by using the alias defined in the SQL statement

This method is the most direct. You can directly create an alias in the SQL statement to complete the mapping, as follows:

summary

The above is all about the mapping method between entity class attributes and data lists in mybatis. I hope it will be helpful to you. Interested friends can continue to refer to this site: Hibernate implementation of pessimistic lock and optimistic lock code introduction, hibernate core idea and interface introduction, etc. Here are some interesting games on this site. It is also necessary to relax after study and work. I also hope you can support the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>