本节我们将学习用sql语句分步查询数据库表信息,顾名思义。分步骤,多步查询。
目录
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>