mybatis处理多对一映射关系3- 分步查询和延迟加载

本节我们将学习用sql语句分步查询数据库表信息,顾名思义。分步骤,多步查询。

目录

1.mapper接口

2.mapper映射文件

2.1 emp映射文件

2.2 Dept映射文件

3.测试类

4.结果

5.原理

6.总结


1.mapper接口

emp mapper

此处是emp mapper接口,第一个sql的接口设置。

    /**
     * 通过分步查询, 查询员工及员工对应的部门信息
     * 分步查询第一步, 查询员工信息
     * 不需要两表联查
     */
    Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);

dept mapper

分步查询第二步接口设置

    /**
     * 通过分步查询,查询员工及员工对应的部门
     * 分布查询第二步: 通过did查询对应的部门
     */
    Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);

2.mapper映射文件

2.1 emp映射文件

此处是第一步执行之后调用第二部,在select处调用下一步。

 <!--        Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);-->
    <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
        select * from t_emp where eid = #{eid}
    </select>
 <resultMap id="empAndDeptByStepResultMap" type="Emp">
        <id property="eid" column="eid"></id>
        <result property="age" column="age"></result>
        <result property="empName" column="emp_name"></result>
        <result property="sex" column="sex"></result>
        <result property="email" column="email"></result>
        <association property="dept" select="com.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo" column="did">
            <!--此处column是分步查询的条件-->
        </association>
    </resultMap>

2.2 Dept映射文件

<resultMap id="empAndDeptByStepResultMap" type="Dept">
        <id property="did" column="did"></id>
        <result property="deptName" column="dept_name"></result>
    </resultMap>
    <!--        Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);-->
    <select id="getEmpAndDeptByStepTwo" resultMap="empAndDeptByStepResultMap">
        select * from t_dept where did = #{did}
    </select>

3.测试类

    @Test
    public void testGetOneEmpAndDeptByStep(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp empAndDept = mapper.getEmpAndDeptByStepOne(1);
        System.out.println(empAndDept);

    }

4.结果

Emp{eid=1, empName='张三', age=32, sex='男', email='123@qq.com', dept=Dept{did=1, deptName='A部门'}}

5.原理

从此处我们可以看到sql执行了两步,有两个sql

20:02:24:113 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - ==>  Preparing: select * from t_emp where eid = ?
20:02:24:173 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - ==> Parameters: 1(Integer)
20:02:24:218 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - ====>  Preparing: select * from t_dept where did = ?
20:02:24:218 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - ====> Parameters: 1(Integer)
20:02:24:224 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - <====      Total: 1
20:02:24:228 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - <==      Total: 1

6.总结

select: 设置sql分步的唯一标识

com.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo

column:设置分步查询的条件

property:分步查询实体类的属性

7.延迟加载

当在mybatis-config文件设置延迟加载时候,我们分步查询的第二步就不会执行。

        <setting name="lazyLoadingEnabled" value="true"/>

或者 映射文件添加 fetchType="lazy"

<resultMap id="empAndDeptByStepResultMap" type="Emp">
    <id property="eid" column="eid"></id>
    <result property="age" column="age"></result>
    <result property="empName" column="emp_name"></result>
    <result property="sex" column="sex"></result>
    <result property="email" column="email"></result>
    <association property="dept"
                 select="com.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                 column="did"
                 fetchType="lazy">

        <!--此处column是分步查询的条件-->
    </association>
</resultMap>

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值