MyBatis入门(七)

181 阅读3分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第8天,点击查看活动详情

本篇主要是对多对一映射处理一对多映射处理

多对一映射处理

查询员工信息以及员工所对应的部门信息

a>级联方式处理映射关系

//多对一  创建一的那个对象
    private Dept dept;
生成get和set方法以及toString 
    
<resultMap id="AndDeptResultMap" type="Emp">
        <id property="eid" column="eid"></id>
        <result property="empName" column="emp_name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="email" column="email"></result>
    这边的dept就是我们在emp中创建的对象
        <result column="did" property="dept.did"></result>
        <result column="dname" property="dept.dname"></result>
​
​
    </resultMap>
<!--     Emp geAndDept(@Param("eid") Integer eid);-->
<select id="geAndDept" resultMap="AndDeptResultMap">
    select   * from t_emp left join t_dept on t_emp.did = t_dept.did where t_emp.eid = #{eid}
</select>

b>使用association处理映射关系

  • association:处理多对一的映射关系
  • property:需要处理多对的映射关系的属性名
  • javaType:该属性的类型
<resultMap id="AndDeptResultMapTwo" type="Emp">
        <id property="eid" column="eid"></id>
        <result property="empName" column="emp_name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="email" column="email"></result>
        <!--
          association:处理多对一的映射关系
          property:需要处理多对的映射关系的属性名
          javaType:改属性的类型
        -->
        <association property="dept" javaType="Dept">
            <id property="did" column="did"></id>
            <result property="deptName" column="dept_name"></result>
        </association>
    </resultMap>
<!--     Emp geAndDept(@Param("eid") Integer eid);-->
<select id="geAndDept" resultMap="AndDeptResultMapTwo">
    select   * from t_emp left join t_dept on t_emp.did = t_dept.did where t_emp.eid = #{eid}

c>分步查询

先查员工表

  • select:设置分布查询的sql的唯一标识(namespace.SQLId或mapper接口的全类名.方法名)
  • column:设置分步查询的条件
/**
 * 查询分步查询所有员工以及员工对应的部门信息
 * 第一步,查询员工信息
 */
Emp geAndDeptByStepOne(@Param("eid") Integer eid);
<resultMap id="empAndDeptByStepResultMap" type="Emp">
        <id property="eid" column="eid"></id>
        <result property="empName" column="emp_name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="email" column="email"></result>
        <association property="dept"
                      select设置分步查询的的sql的唯一标识
                     column设置分步查询的条件
                     select="com.aitiguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                     column="did">
​
        </association>
    </resultMap>
<!--    Emp geAndDeptByStepOne(@Param("eid") Integer eid);-->
    <select id="geAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
        select  * from t_emp where eid = #{eid}
    </select>

查询员工所对应的部门

/**
 * 查询分步
 * 第一步查询所有员工以及员工对应的部门信息
 * 第二步,通过did查询员工所对应的部门信息
 */
 Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);
<select id="getEmpAndDeptByStepTwo" resultType="Dept">
    select  * from t_dept where  did = #{did}
</select>

延迟加载

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

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

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

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

延迟加载的概念:对于实体类关联的属性到需要使用时才查询。也叫懒加载。

<settings> <!--开启延迟加载--> <setting name="lazyLoadingEnabled" value="true"/> </settings>
@Test
public void getEmpAndDeptByStepOne() {
	SqlSession sqlSession = SqlSessionUtils.getSqlSession();
	EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
	Emp emp = mapper.getEmpAndDeptByStepOne(1);
	System.out.println(emp.getEmpName());
}

关闭延迟加载,两条SQL语句都运行了

image.png

开启延迟加载,只运行获取emp的SQL语句

image.png

一对多映射处理

collection

  • collection:用来处理一对多的映射关系
  • ofType:表示该属性的集合中存储的数据的类型
/**
* 根据部门id查新部门以及部门中的员工信息
* @param did
* @return
*/
Dept getDeptEmpByDid(@Param("did") int did);
<resultMap id="deptEmpMap" type="Dept">
<id property="did" column="did"></id>
<result property="dname" column="dname"></result>
<!--
ofType:设置collection标签所处理的集合属性中存储数据的类型
-->
    <!--        collection:处理一对多的关系
            ofType:表示该属性对应的集合中存储数据的类型
            -->
<collection property="emps" ofType="Emp">
<id property="eid" column="eid"></id>
<result property="ename" column="ename"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
</collection>
</resultMap>
<!--Dept getDeptEmpByDid(@Param("did") int did);-->
<select id="getDeptEmpByDid" resultMap="deptEmpMap">
select dept.*,emp.* from t_dept dept left join t_emp emp on dept.did =
emp.did where dept.did = #{did}
</select>

分步查询

/**
 * 通过分步查询查询部门以及部门中所有的成员
 *
 */
Dept getDeptAndEmpByStepOne(@Param("did") Integer did);
<resultMap id="DeptAndEmpByStepResultMap" type="Dept">
        <id property="did" column="did" ></id>
        <result property="deptName" column="dept_name"></result>
        <collection property="emps"
                    select="com.aitiguigu.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"
                    column="did"
                  fetchType="lazy" 这个代表延迟加载调用getset方法
                    @Test
    public  void  testDeptGeEmp(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
        Dept deptAndEmpByStepOne = mapper.getDeptAndEmpByStepOne(3);
        System.out.println(deptAndEmpByStepOne.getDeptName());
    }
                   输出结果DEBUG 03-18 21:28:45,046 ==>  Preparing: select * from t_dept where did = ? (BaseJdbcLogger.java:137) 
DEBUG 03-18 21:28:45,086 ==> Parameters: 3(Integer) (BaseJdbcLogger.java:137) 
DEBUG 03-18 21:28:45,159 <==      Total: 1 (BaseJdbcLogger.java:137) 
C    只执行了一句话
                    
                    ></collection>
    </resultMap>
<!--     Dept getDeptAndEmpByStepOne(@Param("did") Integer did);-->
    <select id="getDeptAndEmpByStepOne" resultMap="DeptAndEmpByStepResultMap">
        select  * from t_dept where did = #{did}
    </select>
/**
 * 通过分步查询查询部门以及部门中所有的成员
 * 第二步 ,根据did查询员工信息
 *
 */
List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);
<!--     List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);-->
    <select id="getDeptAndEmpByStepTwo" resultType="emp" >
        select * from t_emp where did = #{did}

    </select>