MyBatis in Resultmap detailed

Source: Internet
Author: User
Tags constructor

In MyBatis, the return type can be used with Resulttype when the query is in the select Map, or it can be resultmap,resulttype directly
Represents the return type, while Resultmap is a reference to an external resultmap, but Resulttype and Resultmap cannot exist at the same time.

1.resultType

In the MyBatis query mapping, in fact, each of the properties of the query is placed in a corresponding map inside, where the key is the property name,
The value is its corresponding value. When the provided return type attribute is Resulttype, MyBatis will take the key-value pair out of the map and assign
Resulttype the property that corresponds to the specified object. So actually the return type of each query map of MyBatis is Resultmap,
Just when we provide the return type property is Resulttype, MyBatis to automatically give us the corresponding value assigned to Resulttype
The property of the specified object, and when the return type we provide is resultmap, because map does not represent the domain model well,
We need to transform it into a corresponding object, which is often useful in complex queries.

It is emphasized here that MyBatis is mapped for each row of the returned result. Therefore, the following statement returns an integer instead of a list

XML code <select id= "Count" parametertype= "Areadto" resulttype= "Java.lang.Integer" > select ID from USER & Lt;/select>

Returns an int

XML code <select id= "Count" parametertype= "Areadto" resulttype= "Java.lang.Integer" > select COUNT (*) from US ER </select>

Return map XML code <select id= "Selectusers" parametertype= "int" resulttype= "HashMap" > select ID, username, HASHEDPA ssWOrd from some_table where id = #{id} </select>

Such a statement simply acts on all columns that are automatically mapped to the HashMap key, which is specified by the Resulttype property. This is useful in many cases, but HashMap does not describe a domain model well. That way your application will use JavaBeans or POJOs (Plain old Java Objects, plain Java objects) as the domain model

Returns the JavaBean object

XML code <select id= "Count" parametertype= "Areadto" resulttype= "User" > select * from User </select>

Remember that the type alias is your partner. You can use them without entering the full path of the class.

XML code <typealias type= "Com.someapp.model.User" alias= "User"/>

In these cases, MyBatis will automatically create a resultmap behind the scenes, mapping columns to JavaBean properties based on the property name

2.resultMap

MyBatis automatically creates a Resultmap object, then encapsulates the key-value pair based on the found property name, and then sees that the return type is a blog object, which is then removed from the Resultmap and assigned to the key-value pair corresponding to the blog object.

It is also useful when the return type is directly a resultmap, which is mostly used for complex federated queries, because there is no need to make simple queries.

Simple RESULTMAP Configuration

XML code <resultmap type= "Com.liulanghan.Blog" id= "Blogresult" > <id column= "id" property= "id"/> <result column= "title" property= "title"/> <result column= "content" property= "content"/> <r Esult column= "owner" property= "owner"/> </resultMap> <select id= "Selectblog" parametertype= "int" re sultmap= "Blogresult" > select * from t_blog where id = #{id} </select>

Results set of columns than Resultmap more error.
No, only the columns in the Resultmap are mapped.

The result set columns than Resultmap rarely error.
No, only the columns in the result set are mapped.

Advanced result Mapping

XML code <resultmap id= "Detailedblogresultmap" type= "Blog" > <constructor> <idarg column= "Blo g_id "javatype=" int "/> </constructor> <result property=" title "column=" Blog_title "/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.