MyBatis----cascading queries one-to-many (collection mappings)

Source: Internet
Author: User

There are two ways to correlate nested queries and nested results, which are described in this way by nested results.

The previous chapter introduced the relationship of many to one and used <association></association> This is a complex type of association. We select an example to review, such as: a blog has a user, the correlation map works on this result. First look at the table structure fields we'll use in this article:

[SQL] view plain Copy blog Blog:id title author_id author author:id username password email Bio fa    Vourite_section article Post:id blog_id author_id created_on section subject draft body comment Comment:id post_id name Comment Label T:id name we associate a blog with a user, like this:

[SQL]  View plain  copy <select id= "selectblog"  parametertype= "int"  resultmap= "BlogResult" >              select                   b.id as  blog_id,                   b.title as blog_title,                   b.author_id as blog_author_id                   a.id as author_id,                   a.username as  author_username,                   a.password as author_passowrd,                   a.email as auhtor_email,                   a.bio as author_bio                from  blog b left outer  join author a on b.author_id=a.id                     where b.id=#{id}        </select>  


Note that this federated query, and all results are renamed with a unique and clear name. This makes the mapping very simple. Now we can map this result:

[SQL] view plain copy <resultmap id= "Blogresult" type= "Blog" > <id property= "id" column= "blog_id "/> <result property=" title "column=" Blog_title "/> <!--and a user association, the blog class inside the attribute when author, the associated Column is the original blog.author_id--> <association property= "author" column= "blog_author_id" javatype= "Author" resultmap= "Authorresult"/> </resultMap>

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.