• MyBatis(3.2.3)


    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).

  • 相关阅读:
    Go语言学习资源
    优秀编程学习网站
    我对架构的理解
    【转】TCP协议中的三次握手和四次挥手(图解)
    【转】asp.net c# 网上搜集面试题目大全(附答案)
    spring framework 4 源码阅读
    浮点类型
    把二元查找树转变成排序的双向链表
    用模板写快速排序-链表
    用模板写快速排序-数组
  • 原文地址:https://www.cnblogs.com/huey/p/5230434.html
Copyright © 2020-2023  润新知