zoukankan      html  css  js  c++  java
  • 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).

  • 相关阅读:
    鸣人和佐助(OJ 6044)
    Papa的坦克(原BZOJ 1603 打谷机)
    linux基础学习之用户和用户组
    出现log4j:WARN No appenders could be found for logger 解决方法
    myeclipse当中,如何导入或者导出项目
    linux 基础学习之目录与文件处理命令
    linux 基础学习之权限管理命令
    hadoop环境搭建之伪分布集群环境搭建(单节点)
    linux基础学习之各目录的作用
    linux 基础学习之 磁盘,分区,MBR与GPT
  • 原文地址:https://www.cnblogs.com/huey/p/5230434.html
Copyright © 2011-2022 走看看