Deep understanding of Resulttype and Resultmap_java in MyBatis

Source: Internet
Author: User

I. Overview

In MyBatis, the return type can be resulttype for a select mapping in a query, or the return type can be directly represented by Resultmap,resulttype, whereas Resultmap is a reference to an external resultmap. But Resulttype and Resultmap cannot exist at the same time.

In MyBatis query mapping, in fact, every query out of the attributes are placed in a corresponding map, where the key is the property name, the value is its corresponding value.

① when the provided return type property is Resulttype, MyBatis assigns the key value pairs in the map to the properties corresponding to the object specified by Resulttype. So actually the return type of each query map of MyBatis is Resultmap, just
When the provided return type attribute is Resulttype, the mybatis automatically assigns the corresponding value to the property of the object specified resulttype.

② when the return type provided is Resultmap, because the map does not represent the domain model well, it needs to be further transformed into the corresponding object, which is often useful in complex queries.

Second, Resulttype

Blog.java public
class Blog {
private int id;
Private String title;
Private String content;
Private String owner;
Private list<comment> comments;
}

The corresponding database table stores the ID, title, Content, owner attribute.

<typealias alias= "Blog" type= "Com.tiantian.mybatis.model.Blog"/> <select id=
"Selectblog" parametertype= "int" resulttype= "Blog" >
select * from t_blog where id = #{id}
</select>

MyBatis automatically creates a Resultmap object and then encapsulates the key-value pair based on the lookup property name, and then sees the return type as a blog object, and then takes out the key-value pairs of the blog object from the Resultmap to assign the value.

Third, Resultmap

It is also useful when the return type is directly a resultmap, mainly for complex federated queries because it is not necessary to make simple queries. Take a look at a simple query that returns a type of Resultmap, and then look at the use of complex queries.

How to ① Simple query

<resultmap type= "Blog" id= "Blogresult" > <id "column=" id "property="
id "/> <result column=
" Title "property=" title "/>
<result column=" content "property=" content "/> <result"
owner " property= "owner"/>
</resultMap>
<select id= "Selectblog" parametertype= "int" resultmap= " Blogresult ">
select * from t_blog where id = #{id}
</select>

The value of the Resultmap in the Select map is the ID of an external resultmap that indicates which resultmap the returned result maps to, and the Type property of the outer resultmap indicates what type of the resultmap result is. Here is the blog type, then MyBatis will take it as a blog object out. The child node ID of the RESULTMAP node is used to identify the ID of the object, and the result child node is used to identify some simple properties, where the Column property represents the properties queried from the database. Property represents the property of the entity object to which the value of the queried attribute is assigned. This is how the resultmap of a simple query is written.

② Complex Query

There is a comment class, which has a blog reference, which is the comment of the blog, then in the query comment the corresponding blog will also be found to assign its blog properties.

 public class Comment {private int id; private String content; private Date commentdate
= new Date ();
Private Blog Blog; <!--from commentmapper.xml file--> <resultmap type= "Comment" id= "Commentresult" > <association property= " Blog "select=" Selectblog "column=" blog javatype= "blog"/> </resultMap> <select id= "Selectcomment" parametertype= "int" resultmap= "Commentresult" > select * from t_comment where id = #{id} </select> <select id= " Selectblog "parametertype=" int "resulttype=" Blog "> select * from t_blog where id = #{id} </select> 

A select map with a request ID of selectcomment, and then a Resultmap object with an ID of commentresult, can see that the return type of the corresponding Resultmap is a comment object. There is only one association node, and there is no like the previous simple query corresponding to the ID, result child node, but it will still give the corresponding ID attributes such as the Comment object, which is described above MyBatis has automatic encapsulation function, as long as the return type is provided, MyBatis will be based on their own judgment to use the results of the query package corresponding objects, so the simple query in the front, if not clear in the Resultmap to indicate which field the ID, title corresponding to which field, MyBatis will also be based on their own judgment to help encapsulation, MyBatis's own judgment is to compare the field of the query or its corresponding alias to the property of the returned object, and if it matches and the type matches, MyBatis assigns it. The corresponding resultmap in the above is associated with a blog attribute, its corresponding Java type for the blog, in the above writing, the association object is through the subquery to be associated, of course, can also directly through the association query. In the association node above, the property attribute represents which association property of the Resultmap return type, and for the above example is the blog attribute of comment management; Select represents which select map to map the corresponding associated property. The select map of the value corresponding to the request ID is queried to query for the Property object it is associated with; column represents the key-value pair of the current associated object in the Resultmap with ID Commentresult, which is used as a parameter to the subquery for the associated object , the value of the blog attribute queried in Selectcomment is passed as the parameter to the subquery Selectblog of the associated object blog; Javatype indicates what type of current associated object is in Java.

The above description is a one-to-one or one-to-many case, a query on the association of a pair of parties. In the practical application there is a more use of the application is a pair of one side to find the corresponding one side, in the number of the side of the same time to put a pair of party association: When the object of the blog, the corresponding comment all out, In the time to take out the comment also need to take out its corresponding blog, this is in Java in a request to take out.

 <!--from Blogmapper.xml file--> <resultmap type= "Blog" id= "Blogresult" > <id Column= "property=" id "/> <collection property=" comments "select=" Selectcommentsbyblog "column=" id "ofType=" Comment "></collection> </resultMap> <resultmap type=" Comment "id=" Commentresult "> < Association property= "Blog" javatype= "blog" column= "blog" select= "Selectblog"/> </resultMap> <select id= " Selectblog "parametertype=" int "resultmap=" Blogresult "> select * from t_blog where id = #{id} </select> <selec T id= "Selectcommentsbyblog" parametertype= "int" resultmap= "Commentresult" > select * from t_comment where blog = #{blog ID} </select> 

The entry for the above request is a select map with an ID of Selectblog, which returns the resultmap,id with the ID of Blogresult as the type of Blogresult, which specifies the attribute and field of the ID. Specifying IDs will have a very large effect on the MYBATIS internal structure. A comments object is associated with it, because a blog can have a lot of comment, the comments is a collection, so it is mapped with a collection collection, where the select still indicates which subquery to query for the corresponding comments. column indicates which field value is passed as a parameter to the subquery, and OfType also represents the return type, where the return type is the type within the collection, and the reason why the oftype rather than the type is mybatis internal for the purpose of distinguishing it from the associated association.

public void Selectcommentsbyblogtest () {
sqlsession session = Util.getsqlsessionfactory (). Opensession ();
Commentmapper commentmapper = Session.getmapper (commentmapper.class);
List<comment> comments = Commentmapper.selectcommentsbyblog (6);
for (Comment comment:comments)
System.out.println (Comment);
Session.close ();
}
public void Testselectone () {
sqlsession session = Util.getsqlsessionfactory (). Opensession ();
Blogmapper blogmapper = Session.getmapper (blogmapper.class);
Blog blog = blogmapper.selectblog (6);
List<comment> comments = blog.getcomments ();
if (comments!= null) {for
(Comment comment:comments)
System.out.println (Comment);
}
Session.close ();
}

The above is a small set to introduce the MyBatis in the Resulttype and Resultmap, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.