zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    【Linux】Ubuntu 安装 openjdk8
    【算法】二分查找
    【算法】大规模排序
    【算法】小规模排序
    【算法】递归
    【数据结构】队列
    【Java】Windows 安装 JDK-13 并配置环境变量
    【数据库】关于 mysql 的执行顺序
    【数据结构】栈
    【数据结构】链表
  • 原文地址:https://www.cnblogs.com/lemonzhang/p/12954808.html
Copyright © 2011-2022 走看看