MyBatis's Resultmap use method, object association notation

Source: Internet
Author: User

The Basics section can see my other blog http://haohaoxuexi.iteye.com/blog/1333271

In MyBatis, when a query is in a select map, the return type can be Resulttype, or resultmap,resulttype is a direct representation of the return type, and Resultmap is a reference to an external resultmap. But Resulttype and Resultmap cannot exist at the same time. 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 property is Resulttype, MyBatis will remove the key value pair from the map to the property assigned to the object specified by Resulttype. So in fact, each of the MyBatis query map return type is Resultmap, but 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 we provide the return type is RESULTMAP, because map does not represent the domain model well, we need to further convert it to the corresponding object, which is often useful in complex queries.

There is such a Blog.java file

Java code Import java.util.List;          public class Blog {private int id;          Private String title;          Private String content;              Private String owner;         Private list<comment> comments; Getter Setter ...
}

The corresponding database table is stored with the Id,title,content,owner attribute, so when we make the following query mapping

XML code <typealias alias= "Blog" type= "Com.tiantian.mybatis.model.Blog"/><!--configuration file from MyBatis Mybatis_ config.xml--> <select id= "Selectblog" parametertype= "int" resulttype= "Blog" > select * from T_blog whe Re id = #{id} </select><!--from SQL mapping file blogmapper.xml-->

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. Let's take a look at a simple query with a return type of Resultmap, and then look at the use of complex queries.

How to do simple queries

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

The value of Resultmap in the Select map is the ID of an external resultmap that indicates which resultmap the returned result is mapped to, and the Type property of the external resultmap indicates what type the result of the Resultmap is. This is the type of blog, 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 property, where the column property represents the property queried from the database. property indicates which property of the entity object the value that corresponds to the queried attribute is assigned to. This is how the resultmap of a simple query is written. Next, look at a more complex query.

There is a comment class, which has a reference to the blog, which is the comment of the blog, then we in the query comment when the corresponding blog should also be found to assign its blog attributes.

Java code Import java.util.Date;              public class Comment {private int id;              Private String content;              Private date Commentdate = new Date ();          Private Blog Blog; Getter Setter ...}

It's written like this.

  XML Code    <!--from Commentmapper.xml file     -->        <resultmap type= "Comment"  id= "Commentresult" >            <association property= "blog"  select= "Selectblog"  column= "blog"   Javatype= "Blog"/>      

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.