1. 程式人生 > >使用association標籤實現結果集的巢狀

使用association標籤實現結果集的巢狀

當需要實現多表查詢的時候,通常需要使用association標籤來進行結果集的巢狀。

ex:

考慮如下情形:

員工表的d_id欄位和部門表的id欄位有一個外來鍵對映關係,現在需要把對應id的員工以及部門全部查找出來。

 <select id="getEmpAndDept" resultMap="complexEmp2">
            SELECT e.id id,e.last_name last_name,e.email email,e.gender gender,e.d_id d_id,
		    d.id did,d.dept_name dept_name FROM tb1_emplyee e,tb1_dept d
		    WHERE e.d_id=d.id AND e.id=#{id}
 </select>
使用<resultMap>標籤封裝結果集
<resultMap id="complexEmp" type="com.test.beans.Employee">
            <id column="id" property="id"/>
            <result column="last_name" property="lastName"/>
            <result column="email" property="email"/>
            <result column="gender" property="gender"/>
            <!--<association property="dept" javaType="com.test.beans.Department">
                <id column="did" property="id"/>
                <result column="dept_name" property="departmentName"/>
            </association>-->
            <result column="did" property="dept.id"></result>
            <result column="dept_name" property="dept.departmentName"/>
        </resultMap>

上邊是使用通過級聯屬性的方式封裝結果集

我們也可以通過association標籤來完成同樣的功能:

  <resultMap id="complexEmp2" type="com.test.beans.Employee">
            <id column="id" property="id"/>
            <result column="last_name" property="lastName"/>
            <result column="email" property="email"/>
            <result column="gender" property="gender"/>
            <association property="dept" javaType="com.test.beans.Department">
                <id column="did" property="id"/>
                <result column="dept_name" property="departmentName"/>
            </association>
  </resultMap>
當然association標籤的功能不止於此,還可以通過它實現分段查詢的功能

上面的操作可以劃分為下面的幾步:

1、先按照員工id查詢員工資訊
2、根據查詢員工資訊中的d_id值去部門表查出部門資訊
3、部門設定到員工中;

<select id="getEmpByIdStep" resultMap="MyEmpByStep">
        select * from tb1_emplyee where id=#{id}
</select>

<resultMap type="com.test.beans.Employee" id="MyEmpByStep">
        <id column="id" property="id"/>
        <result column="last_name" property="lastName"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
        <!-- association定義關聯物件的封裝規則
            select:表明當前屬性是呼叫select指定的方法查出的結果
            column:指定將哪一列的值傳給這個方法

            流程:使用select指定的方法(傳入column指定的這列引數的值)查出物件,並封裝給property指定的屬性
         -->
        <association property="dept"
                     select="com.test.dao.DepartMentMapper.getDeptById"
                     column="d_id">
        </association>
    </resultMap>

對應的Department對映檔案:
<mapper namespace="com.test.dao.DepartMentMapper">
        <select id="getDeptById" resultType="com.test.beans.Department">
            SELECT  id,dept_name departmentName FROM  tb1_dept WHERE id=#{id}
        </select>
</mapper>

注:對應的介面都沒有給出,因為比較簡單,自己實現一下即可。

當然association標籤還可以實現懶載入的功能

什麼是懶載入呢?

前面的分步查詢,每查詢一次都會執行兩次sql(一次查詢員工,一次查詢部門)

有時候我們並不需要插敘部門的情況,所以懶查詢就可以幫我們解決這個問題,提高效率,減少資源的佔用

懶載入的實現也非常簡單,只要在全域性配置檔案中新增如下的配置程式碼即可:

<settings>
		<!--顯示的指定每個我們需要更改的配置的值,即使他是預設的。防止版本更新帶來的問題  -->
		<setting name="lazyLoadingEnabled" value="true"/>
		<setting name="aggressiveLazyLoading" value="false"/>
</settings>