Mybatis-自定义映射resultMap02

    延迟加载:标识当前我们访问哪些信息,就回去执行相应的sql语句,没有访问的话,就不会去执行对应sql语句

    分步查询的优点:可以实现延迟加载,但是必须在核心配置文件中设置全局配置信息:

    lazyLoadingEnabled:延迟加载的全局开关。当开启时,所有关联对象都会延迟加载

    aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。 否则,每个属性会按需加载

    此时就可以实现按需加载,获取的数据是什么,就只会执行相应的sql。此时可通过association和 collection中的fetchType属性设置当前的分步查询是否使用延迟加载,fetchType="lazy(延迟加 载)|eager(立即加载)"

    延迟加载设置:

  

 

 

     手动控制延迟加载在相应的mapper文件中的association标签中的fetchType属性进行控制

<resultMap id="ResultEmpAndDeptOne" type="Emp">
        <id column="eid" property="eid"/>
        <result column="emp_name" property="empName"/>
        <result column="age" property="age"/>
        <result column="sex" property="sex"/>
        <result column="email" property="email"/>
        <result column="did" property="dept.did"/>
        <!--
            fetchType:是当我们开启了延迟加载时,可以通过这个属性手动控制延迟加载的效果
            fetchType="lazy|eager":
                lazy:表示延迟加载
                eager:表示立即加载
        -->
        <association property="dept"
                    select="com.hrf.dao.DeptMapper.SelectDept"
                     column="did"
                    fetchType="eager"></association>
    </resultMap>
    <select id="SelectEmpAngDept" resultMap="ResultEmpAndDeptOne">
        select * from t_emp  where eid = #{eid}
    </select>

   一对多映射关系:

    1.通过collection解决一对多映射关系:

    mapper文件:

<!--接口方法: Dept getDeptEmpByDid(@Param("did")int did);   -->
    <resultMap id="deptEmpMap" type="dept">
        <id column="did" property="did"/>
        <result column="dept_name" property="deptName"/>
        <!--
            collection:处理一对多的映射关系
            ofType:用来表示该属性所对应的集合中存储的数据类型
        -->
        <collection property="emps" ofType="Emp">
            <id column="eid" property="eid"/>
            <result column="emp_name" property="empName"/>
            <result column="age" property="age"/>
            <result column="sex" property="sex"/>
            <result column="email" property="email"></result>
        </collection>
    </resultMap>
    <select id="getDeptEmpByDid" resultType="dept">
        select * from t_dept left join t_emp on t_dept.did = t_emp.did where t_dept.did=#{did}
    </select>

    测试类:

@Test
    public void getDeptEmpByDid(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
        List<Dept> deptEmpByDid = mapper.getDeptEmpByDid(1);
        System.out.println(deptEmpByDid);
    }

    查看结果: 

    2.分布查询

  1.先通过员工接口中的方法根据部门号获得员工信息

  2.在通过部门接口中的方法根据部门号获取部门和所在部门的员工信息

    1.设置第一步先查询根据部门id员工信息:

<!-- 分布查询第一步:接口方法List<Emp> getEmpListByDid(@Param("eid")int eid);-->
    <select id="getEmpListByDid" resultType="emp">
        select * from t_emp where did=#{did}
    </select>

    2.设置第二部根据部门id查询部门信息和所在部门的员工信息

<!--分布查询
        Dept getDpetEmpById(@Param("did") int did);
    -->
    <resultMap id="deptEmpStep" type="dept">
        <id column="did" property="did"/>
        <result column="dept_name" property="deptName"/>
        <!--
            select:设置分布查询的sql的唯一标识(namespace.SQLId或者mapper接口的全类名.方法名)
            column:用来设置分布查询的条件
             fetchType:是当我们开启了延迟加载时,可以通过这个属性手动控制延迟加载的效果
            fetchType="lazy|eager":
                lazy:表示延迟加载
                eager:表示立即加载
        -->
        <collection property="emps" fetchType="eager"
                    select="com.hrf.dao.EmpMapper.getEmpListByDid"   column="did">
        </collection>
    </resultMap>
    <select id="getDpetEmpById" resultMap="deptEmpStep">
        select * from t_dept where did =#{did} ;
    </select>

    测试类方法:

@Test
    public void getDpetEmpById(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
        Dept dpetEmpById = mapper.getDpetEmpById(2);
        System.out.println(dpetEmpById);

    }

  查看结果:

  

 

posted @ 2022-03-02 19:03  Soleili  阅读(32)  评论(0编辑  收藏  举报