resultMap自定義映射(多對一)


自定義resultMap,處理復雜的表關系,實現高級結果集映射
  1) id :用於完成主鍵值的映射
  2) result :用於完成普通列的映射
  3) association :一個復雜的類型關聯;許多結果將包成這種類型
  4) collection : 復雜類型的集

1、多對一的查詢,員工與員工部門:

  1)直接通過resultMap進行處理:

    <!-- 
        <resultMap>:自定義映射,處理復雜的表關系
        <id column="eid" property="eid"/>
        <id>:設置主鍵的映射關系,column設置字段名,property設置屬性名
        <result column="ename" property="ename"/>
        <result>:設置非主鍵的映射關系,column設置字段名,property設置屬性名
     -->
    <resultMap type="Emp" id="empMap">
        <id column="eid" property="eid"/>
        <result column="ename" property="ename"/>
        <result column="age" property="age"/>
        <result column="sex" property="sex"/>
        <result column="did" property="dept.did"/>
        <result column="dname" property="dept.dname"/>
    </resultMap>
    

    <!-- List<Emp> getAllEmp(); -->
    <select id="getAllEmp" resultMap="empMap">
        select e.eid,e.ename,e.age,e.sex,e.did,d.dname from emp e left join dept d on e.did = d.did
    </select>     

  2)POJO中的屬性可能會是一個對象,我們可以使用聯合查詢,並以級聯屬性的方式封裝對象.使用association標簽定義對象的封裝規則

public class Dept {
    private Integer did;
    private String dname;
    // 省略 get/set方法
}
public class Emp {
    private Integer eid;
    private String ename;
    private Integer age;
    private String sex;
    private Dept dept;
    // 省略 get/set方法
}

查詢某一員工的所有信息,包括其的部門信息,涉及到兩表聯查:

   <resultMap type="Emp" id="empMap">
        <id column="eid" property="eid"/>
        <result column="ename" property="ename"/>
        <result column="age" property="age"/>
        <result column="sex" property="sex"/>
        <association property="dept" javaType="Dept">
            <id column="did" property="did"/>
            <result column="dname" property="dname"/>
        </association>
    </resultMap>
    
    <!-- List<Emp> getAllEmp(); -->
    <select id="getAllEmp" resultMap="empMap">
        select e.eid,e.ename,e.age,e.sex,e.did,d.dname from emp e left join dept d on e.did = d.did
    </select>

  3)association 分步查詢
    實際的開發中,對於每個實體類都應該有具體的增刪改查方法,也就是DAO層, 因此對於查詢員工信息並且將對應的部門信息也查詢出來的需求,就可以通過分步的方式完成查詢。
    ① 先通過員工的id查詢員工信息
    ② 再通過查詢出來的員工信息中的外鍵(部門id)查詢對應的部門信息.

<!-- 
        <resultMap>:自定義映射,處理復雜的表關系
     -->
    <resultMap type="Emp" id="empMapStep">
        <id column="eid" property="eid"/>
        <result column="ename" property="ename"/>
        <result column="age" property="age"/>
        <result column="sex" property="sex"/>
        <!-- select:分步查詢的SQL的id,即接口的全限定名.方法名或namespace.SQL的id column:分步查詢的條件,注意:此條件必須是從數據庫查詢過得 -->
        <association property="dept" select="com.atguigu.mapper.DeptMapper.getDeptByDid" column="did"/>
    </resultMap>
    
    <!-- Emp getEmpStep(String eid); -->
    <select id="getEmpStep" resultMap="empMapStep">
        select eid,ename,age,sex,did from emp where eid = #{eid}
    </select>
<mapper namespace="com.atguigu.mapper.DeptMapper">
    
    <!-- Dept getDeptByDid(String did); -->
    <select id="getDeptByDid" resultType="Dept">
        select did,dname from dept where did = #{did}
    </select>
    
</mapper>

    ③association 分步查詢使用延遲加載:在分步查詢的基礎上,可以使用延遲加載來提升查詢的效率,只需要在全局的Settings中進行如下的配置:

    <settings>
        <!-- 開啟延遲加載 -->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!-- 是否查詢所有數據 -->
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM