Introduction to resultmap of mybatis)

Source: Internet
Author: User

This article from: http://haohaoxuexi.iteye.com/blog/1337009

When querying select ing in mybatis, the return type can be resulttype or resultmap. resulttype indicates the return type directly, while resultmap references the external resultmap, however, the resulttype and resultmap cannot exist at the same time. InWhen mybatis is used for query ing, the results of each query are put in a corresponding map. The key is the database field name and the value is the corresponding value. When the returned type attribute is resulttype, mybatis extracts the key-value pairs in the map and assigns them to the attributes of the object specified by resulttype.. Only when the returned type attribute we provide is resulttype, mybatis automatically assigns the corresponding value to the attribute of the object specified by resulttype.

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; Public int GETID () {return ID ;} public void setid (int id) {This. id = ID;} Public String gettitle () {return title;} public void settitle (String title) {This. title = title;} Public String getcontent () {return content;} public void setcontent (string content) {This. content = content;} Public String getowner () {return owner;} public void setowner (string owner) {This. owner = owner;} public list <comment> getcomments () {return comments;} public void setcomments (list <comment> comments) {This. comments = comments ;}@ overridepublic string tostring () {return "---------------- blog ----------------- \ n ID:" + ID + "\ n title:" + title + "\ N content: "+ content +" \ n owner: "+ owner ;}}

The corresponding database tables store the ID, title, content, and owner attributes. When we perform the following query ing

XML Code

 

<Typealias alias = "blog" type = "com. Tiantian. mybatis. model. blog"/> <! -- Mybatis_config.xml configuration file from mybatis --> <select id = "selectblog" parametertype = "int" resulttype = "blog"> select * From t_blog where ID =#{ ID} </ select> <! -- From the SQL ing file blogmapper. xml -->

Mybatis will automatically create a resultmap object, encapsulate the key-value pairs based on the searched attribute names, and then see that the returned type is a blog object, then, retrieve the key-value pairs corresponding to the blog object from resultmap and assign values. It is also very useful when the returned type is directly a resultmap, which is mainly used for complex Union queries, because simple queries are not necessary. Let's first look at a simple query with the returned type resultmap, and then look at the usage of complex queries.

Simple Query Method

XML Code
<Resultmap type = "blog" id = "blogresult"> <ID column = "ID" property = "ID"/> <result column = "title" property = "title "/> <result column = "content" property = "content"/> <result column = "owner" property = "owner"/> </resultmap> <select id = "selectblog" parametertype = "int" resultmap = "blogresult"> select * From t_blog where ID =#{ ID} </SELECT>

The ID of the resultmap is used to identify this element.In the select ing, the resultmap attribute is referenced. The type attribute of resultmap indicates the type of the resultmap result (here it is a blog type, then mybatis will retrieve it as a blog object).The subnode ID of the resultmap node is used to identify the ID of the table in the database corresponding to the object (the primary key of the table), and the result subnode is used to indicate the correspondence between the database field and the object attribute, the column attribute indicates the name of the corresponding field of the table and the attribute of the property object.. The method of resultmap for simple queries is as follows. Next, let's look at a complex query. There is a comment class, with a reference to a blog, indicating which blog is comment, when querying comment, we need to find out the corresponding blog and assign it the blog attribute.

Java code

 

Import Java. util. date; public class comment {private int ID; private string content; private date commentdate = new date (); private blog; Public int GETID () {return ID ;} public void setid (int id) {This. id = ID;} Public String getcontent () {return content;} public void setcontent (string content) {This. content = content;} public date getcommentdate () {return commentdate;} public void setcommentdate (date commentdate) {This. commentdate = commentdate;} public blog getblog () {return blog;} public void setblog (blog) {This. blog = blog;} Public String tostring () {return blog + "\ n ---------------- comment ----------------- \ n ID:" + ID + "\ N content: "+ content +" \ n commentdate: "+ commentdate ;}}

The statement is as follows:

XML Code
<! -- 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>

In the Association subnode above, the property attribute indicates the name of the Type Association attribute returned by resultmap (the blog attribute of the class comment ); the select attribute indicates which select ing is used to fill the corresponding attribute (here, the attribute object will be filled through the query where the ID is selectblog ); column is the corresponding associated foreign key (here, it is the foreign key of the comment table associated with the blog table: Blog); javatype indicates the type of the current associated object in Java. In the preceding write method, the correlated object is associated through subqueries. Of course, you can also associate objects directly through association queries. we can see that the return type of the corresponding resultmap is a comment object, and there is only one association node, instead of the ID corresponding to the simple query described above, the result subnode, however, it still assigns the corresponding attributes such as ID to the comment object. This is the previous description that mybatis has the automatic encapsulation function, as long as you provide the return type, mybatis uses the query results to encapsulate the corresponding objects based on its own judgment. In the same way, in the previous query example, even if the resultmap does not explicitly specify the field corresponding to the ID and the field corresponding to the title, mybatis will also help you seal it based on your own judgment. Mybatis compares the queried field or its corresponding alias with the returned object attributes. If they match and their types match, mybatis assigns a value to them.The access is like this. First, the request ID is the select ing of selectcomment, and then a resultmap object with ID as commentresult is obtained.

 

The preceding section describes one-to-one or one-to-many join queries for one party. In practical applications, another application that is widely used is to find the corresponding "multiple" through "one ", at the same time, we also need to associate "1" when taking out "more", that is, in the above example, when taking out the blog object, we will take out all the corresponding comments, when you pull out comment, you still need to take out the corresponding blog, which is obtained through a request in Java. The statement is as follows:

XML Code

 
<! -- From blogmapper. XML file --> <resultmap type = "blog" id = "blogresult"> <ID column = "ID" 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> <! -- Search for comment through blog --> <select id = "selectcommentsbyblog" parametertype = "int" resultmap = "commentresult"> select * From t_comment where blog =#{ blogid} </select>

The preceding request entry is the select ing with ID as selectblog. The returned result is the resultmap with ID as blogresult, and the type of ID as blogresult is blog. The attributes and fields of ID are specified, the specified ID will have a great effect on the internal structure of mybatis. A comments object is associated. Because a blog can have many comments, this comments is a set, so the collection is used for ing, the SELECT statement indicates which subquery is executed to query the corresponding comments,Column indicates which field value is obtained as a parameter and is passed to the subquery., Oftype also indicates the return type. Here, the return type is the internal type of the set. Instead of using oftype, the internal type of mybatis is used to distinguish it from associated Association.

Test code:

Java code
@ Testpublic void selectcommentsbyblogtest () {sqlsession session = util. getsqlsessionfactory (). opensession (); commentmapper = session. getmapper (commentmapper. class); List <comment> comments = commentmapper. selectcommentsbyblog (6); For (Comment comment: Comments) system. out. println (comment); Session. close ();}/*** query a single record */@ testpublic void testselectone () {sqlsession session = util. getsqlses Sionfactory (). opensession (); blogmapper = session. getmapper (blogmapper. class); blog = blogmapper. selectblog (6); List <comment> comments = blog. getcomments (); If (comments! = NULL) {system. out. println ("-------------- comments size ------------" + comments. size (); For (Comment comment: Comments) system. out. println (comment);} session. close ();}

 

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.