Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The best part of Result Maps of MyBatis

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Case: https://github.com/sun2shadow/simpleMybatis

The resultMap element is the most important and powerful element in MyBatis.

Let's start with a simple mapping:

Select id, username, hashedPassword from some_table where id = # {id}

It maps all columns to the primary key of HashMap. However, in practice, the resultType type is often defined as JavaBeans or POJOs, such as the following statement:

Select id, username, hashedPassword from some_table where id = # {id}

Advanced mapping is a common form in development:

ResultMap Conceptual View:

The constructor-class is used to inject the results into the constructor when instantiated.

IdArg-ID parameter; marking the result as ID can help improve the overall performance

Arg-A general result of injection into a constructor

Id-An ID result; marking the result as an ID can help improve overall performance

Result-A normal result injected into a field or JavaBean property

Association-A complex type association; many results will be packaged into this type

Embed the result map-the association of the result map itself, or refer to a

Collection-set of complex types

Embed the result map-the set of the result map itself, or refer to a

Discriminator-use result values to determine which result mapping to use

Embed the result mapping-in this case the result also maps itself, so it can contain many identical elements, or it can refer to an external result mapping.

Case-result mapping based on some values

The ResultMap Attributes attribute describes a unique identity in the current namespace of id, which is used to identify the fully qualified name of a result map.type class, or a type alias (the built-in alias can refer to the table above). If autoMapping is set, MyBatis will turn automatic mapping on or off for this ResultMap. This property overrides the global property autoMappingBehavior. The default value is unset.

Id&result

Both id and result map the values of a separate column to separate properties or fields of simple data types (strings, integers, double-precision floating-point numbers, dates, etc.)

The Id and Result Attributes attribute describes the fields or attributes that property maps to column results. If it matches an attribute of JavaBeans that exists with the same name as the given name, then it will be used. Otherwise, MyBatis will look for the field with the given name property. In both cases, you can navigate with complex attributes that are normal points. For example, you can map something like this: "username", or map to something complex: "address.street.number". The column name that column gets from the database, or the renaming tag for the column name. This is the same string that would normally be passed to the resultSet.getString (columnName) method parameter. JavaType a fully qualified name of a Java class, or a type alias (see the list of built-in type aliases above). If you map to a JavaBean,MyBatis, you can usually determine the type. However, if you are mapping to HashMap, then you should explicitly specify javaType to guarantee the desired behavior. The type of jdbcType in the list of supported JDBC types after this table. The JDBC type only needs to be processed for columns whose insert, update, and delete operations may be empty. This is what JDBC jdbcType needs, not MyBatis's. If you program directly with JDBC, you need to specify this type-but only for values that may be empty. TypeHandler We discussed the default type handler earlier. Using this property, you can override the default type handler. This property value is the fully qualified name of the class or an implementation of a type handler, or a type alias.

Association: associated mapping relationship is required when querying joined tables. Property: author variable in blog, external build of author in column table, entity associated with javaType

By mapping relationships to tell MyBatis how to load association relationships, MyBatis provides two ways:

Nested query: returns the expected complex type by executing another SQL mapping statement.

Nested results: use nested result mappings to deal with a subset of repeated federated results. First, let's look at the properties of this element. All you will see, it and ordinary only by select and

The result mapping of the resultMap property is different.

Associated nested queries:

SELECT * FROM BLOG WHERE ID = # {id} SELECT * FROM AUTHOR WHERE ID = # {id}

One is to load the blog and the other is to load the author. The biggest problem in this way is the Number1 query problem. Querying a blog has N authors who need to query the author table N times.

Associated nesting results:

Let's first look at a statement like this: query blog's author and co-author, author and co-author's id cannot be called author_id, otherwise it will cause conflict.

Select B.id as blog_id, B.title as blog_title, A.id as author_id, A.username as author_username, A.password as author_password, A.email as author_email, A.bio as author_bio, CA.id as co_author_id, CA.username as co_author_username CA.password as co_author_password, CA.email as co_author_email, CA.bio as co_author_bio from Blog B left outer join Author An on B.author_id = A.id left outer join Author CA on B.co_author_id = CA.id where B.id = # {id}

First define the result mapping of author:

Secondly, the resultMap of blog is defined, and the prefix is specified by columnPrefix to distinguish between the lead author and the co-author.

After reading the correlation, it is much easier to look at the collection, which is basically similar.

Private List posts

This relationship requires the use of collections, specified by ofType, and the type of data in the list is Post.

Select B.id as blog_id, B.title as blog_title, B.author_id as blog_author_id, P.id as post_id, P.subject as post_subject, P.body as post_body, from Blog B left outer join Post P on B.id = P.blog_id where B.id = # {id}

The relationship that the above query needs to map is as follows: the property attribute in collection is the variable posts in blog. See the variable declaration above.

Let's briefly explain the discriminator: choose different mappers according to the query results.

Discriminator this example is more interesting, query vehicle information, return different types of vehicles, there are cars, trucks, SUV and other different types have different mapping results.

If the type is car, map the result according to carResult:

In this case, only the doorCount property will be loaded. If Car is an instance of Vehicle. Therefore, we want the remaining properties to be loaded as well. The simple changes to the result mapping we set are as follows.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report