码迷,mamicode.com
首页 > 移动开发 > 详细

MyBatis(3.2.3) - One-to-one mapping using nested ResultMap

时间:2016-03-01 12:33:18      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:

We can get Student along with the Address details using a nested ResultMap as follows:

<resultMap type="Address" id="AddressResult">
    <id property="addrId" column="addr_id"/>
    <result property="street" column="street"/>
    <result property="city" column="city"/>
    <result property="state" column="state"/>
    <result property="zip" column="zip"/>
    <result property="country" column="country"/>
</resultMap>
<resultMap type="Student" id="StudentWithAddressResult">
    <id property="studId" column="stud_id"/>
    <result property="name" column="name"/>
    <result property="email" column="email"/>
    <association property="address" resultMap="AddressResult"/>
</resultMap>

<select id="selectStudentWithAddress" parameterType="int" resultMap="StudentWithAddressResult">
    SELECT STUD_ID, NAME, EMAIL, PHONE, A.ADDR_ID, STREET, CITY, STATE, ZIP, COUNTRY
    FROM STUDENTS S LEFT OUTER JOIN ADDRESSES A 
    ON S.ADDR_ID = A.ADDR_ID
    WHERE STUD_ID = #{studId}
</select>

The <association> element can be used to load the has-one type of associations. In the preceding example, we used the <association> element, referencing another <resultMap> that is declared in the same XML file.

We can also use <association> with an inline resultMap query as follows:

<resultMap type="Student" id="StudentWithAddressResult">
    <id property="studId" column="stud_id"/>
    <result property="name" column="name"/>
    <result property="email" column="email"/>
    <association property="address" javaType="Address">
        <id property="addrId" column="addr_id"/>
        <result property="street" column="street"/>
        <result property="city" column="city"/>
        <result property="state" column="state"/>
        <result property="zip" column="zip"/>
        <result property="country" column="country"/>
    </association>
</resultMap>

Using the nested ResultMap approach, the association data will be loaded using a single query (along with joins if required).

 

MyBatis(3.2.3) - One-to-one mapping using nested ResultMap

标签:

原文地址:http://www.cnblogs.com/huey/p/5230434.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!