zoukankan      html  css  js  c++  java
  • resultMap自定义映射(一对多)

    collection:处理一对多和多对多的关系
      1) POJO中的属性可能会是一个集合对象,我们可以使用联合查询,并以级联属性的方式封装对象.使用collection标签定义对象的封装规则

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

    查询某一部门下的所有员工信息:

        <resultMap type="Dept" id="deptMap">
            <id column="did" property="did"/>
            <result column="dname" property="dname"/>
            <!-- 
                <collection>:处理一对多和多对多的关系
                ofType:指集合中的类型,不需要指定javaType
             -->
            <collection property="emps" ofType="Emp">
                <id column="eid" property="eid"/>
                <result column="ename" property="ename"/>
                <result column="age" property="age"/>
                <result column="sex" property="sex"/>
            </collection>
        </resultMap>
        
        <!-- Dept getDeptEmpsByDid(String did); -->
        <select id="getDeptEmpsByDid" resultMap="deptMap">
            select d.did,d.dname,e.eid,e.ename,e.age,e.sex from dept d left join emp e on d.did = e.did where d.did = #{did}
        </select>

       2)collection 分步查询
      实际的开发中,对于每个实体类都应该有具体的增删改查方法,也就是DAO层, 因此对于查询部门信息并且将对应的所有的员工信息也查询出来的需求,就可以通过分步的方式完成查询。
        ① 先通过部门的id查询部门信息
        ② 再通过部门id作为员工的外键查询对应的信息.

       
        <!-- List<Emp> getEmpListByDid(String did); -->
        <select id="getEmpListByDid" resultType="Emp">
            select eid,ename,age,sex from emp where did = #{did}
        </select>
        
        <resultMap type="Dept" id="deptMapStep">
            <id column="did" property="did"/>
            <result column="dname" property="dname"/>
            <collection property="emps" select="com.atguigu.mapper.EmpDeptMapper.getEmpListByDid" column="{did=did}" fetchType="eager"></collection>
        </resultMap>
        
        <!-- Dept getOnlyDeptByDid(String did); -->
        <select id="getOnlyDeptByDid" resultMap="deptMapStep">
            select did,dname from dept where did = #{did}
        </select>

     column="{did=did}中如果有多个参数,则以map键值对的方式,但要注意在后面的sql语句中要对应好键的名称。

    扩展: association 或 collection的 fetchType属性
      1) 在<association> 和<collection>标签中都可以设置fetchType,指定本次查询是否要使用延迟加载。默认为 fetchType= ”lazy” ,如果本次的查询不想使用延迟加载,则可设置为fetchType=”eager”.
      2) fetchType可以灵活的设置查询是否需要使用延迟加载,而不需要因为某个查询不想使用延迟加载将全局的延迟加载设置关闭.

  • 相关阅读:
    [LeetCode] Implement Stack using Queues
    [LeetCode] String Compression
    [国嵌攻略][060][LCD工作原理解析]
    [国嵌攻略][059][2440-DMA程序设计]
    [国嵌攻略][057][串口控制台建立]
    [国嵌攻略][056][串口驱动程序设计]
    [国嵌攻略][054][NandFlash驱动设计_写]
    [问题笔记][指针相加翻译成汇编右移2位]
    [国嵌攻略][053][6410和210按键中断编程]
    [国嵌攻略][050][2440按键中断编程]
  • 原文地址:https://www.cnblogs.com/lemonzhang/p/12955241.html
Copyright © 2011-2022 走看看