查询Emp的同时,查出emp对应的部门Department

方法1:联合查询,使用级联属性封装结果集

<!--
联合查询,使用级联属性封装结果集 
type:要自定义规则的javaBean类型
唯一标识,方便引用
指的是数据库表中的列名
列名对应的javaBean中的属性名称
-->
 
<resultMap type="cn.bdqn.mybatis.been.Emp" id="myDifemp">
<id column="id" property="id" />
<result column="last_name" property="last_name" />
<result column="gender" property="gender" />
<result column="did_id" property="dept.id" />
<result column="dept_name" property="dept.departmentName" />
</resultMap>
<!--public Emp getEmpAndDept(Integer id); -->
<select id="getEmpAndDept" resultMap="myDifEmp2">
SELECT e.id AS id,e.last_name AS last_name,e.gender AS gender,e.email AS
email,d.id AS d_id,d.`dept_name` AS dept_name
FROM emp e,dept d WHERE e.`d_id`=d.id and e.id=#{id}
</select>

方法2:association来实现多表查询

<!--
association来实现多表查询 
association标签的property属性来指定javaBean中的哪个属性是我们自定义的(即:哪个属性是联合的对象),
javaType是用来指定这个属性的类型(不能省略)
association标签中继续使用id和result标签来封装相应的封装规则
 
-->
<resultMap type="cn.bdqn.mybatis.been.Emp" id="myDifEmp2">
<id column="id" property="id" />
<result column="last_name" property="last_name" />
<result column="gender" property="gender" />
<result column="email" property="email"/>
<association property="dept"  javaType="cn.bdqn.mybatis.been.Department">
<id column="did_id"  property="id"/>
<result column="dept_name" property="departmentName"/>
</association>
</resultMap>

 方法3:association进行分步式多表查询

01.先按照员工id查询员工信息

02.根据查出员工信息中的d_id值去部门表查询部门信息

03.把查出的部门设置到员工中;

创建Department的javaBean,编写对应的映射文件,

DepartmentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace与接口进行绑定 -->
<mapper namespace="cn.bdqn.mybatis.dao.DeptMapper">
<!--public Department getDepartmentById(Integer id); -->
<select id= "getDepartmentById" resultType="cn.bdqn.mybatis.been.Department">
<!--我们数据表中部门名称的字段的名字是dept_name 而我们javaBean中对应的 名字是departmentName,不一致,所以我们要将查询的列一一写出,并且将名称不相同的,起别名 -->
select id,dept_name as departmentName from dept where id=#{id}
</select>
</mapper>

 

编写emp对应的映射文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace:名称空间 -->
<mapper namespace="cn.bdqn.mybatis.dao.EmpMapperPlus">
 
 
<!-- association来实现多表查询 association标签的property属性来指定javaBean中的哪个属性是我们自定义的(即:哪个属性是联合的对象), 
javaType是用来指定这个属性的类型(不能省略) association标签中继续使用id和result标签来封装相应的封装规则 -->
<resultMap type="cn.bdqn.mybatis.been.Emp" id="myDifEmp2">
<id column="id" property="id" />
<result column="last_name" property="last_name" />
<result column="gender" property="gender" />
<result column="email" property="email" />
<association property="dept" javaType="cn.bdqn.mybatis.been.Department">
<id column="did_id" property="id" />
<result column="dept_name" property="departmentName" />
</association>
</resultMap>
<!--
使用association进行分步式查询
先按照员工id查询员工信息 
根据查出员工信息中的d_id值去部门表查询部门信息 
把查出的部门设置到员工中; 
 -->
<resultMap type="cn.bdqn.mybatis.been.Emp" id="myEmpBy">
<id column="id" property="id" />
<result column="last_name" property="last_name" />
<result column="gender" property="gender" />
<result column="email" property="email" />
<!--association定义关联对象的封装规则 select:select的属性值是部门的映射文件的名称空间+对应的方法名称 表明当前属性是调用select指定的方法查出来的结果 
column:指定将哪一列的值传给这个方法 流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性 -->
<association property="dept"
select="cn.bdqn.mybatis.dao.DeptMapper.getDepartmentById" column="d_id">
</association>
</resultMap>
 
<!-- public Emp getEmpByIdStep(Integer id); -->
<select id="getEmpByIdStep" resultMap="myEmpBy">
select * from emp where
id=#{id}
</select>
</mapper>

注意:这两个映射文件写好后,必须都要在全局配置文件mybatis-config.xml中进行注册

<mappers>
<mapper resource="EmpMapperPlus.xml" />
<mapper resource="DepartmentMapper.xml"/>
</mappers>

分步查询,可以使用延迟加载(懒加载/按需加载)

emp==>Dept

使用联合查询和其他复杂查询时,emp的信息和Dept的信息都会同时查出,这样很浪费资源

而我们希望部门信息在我们使用的时候再去查询,这样就很节省数据库了

只需要在分段查询的基础之上加上两个配置:

只需要在全局配置文件中开启懒加载模式

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

和按需加载

<setting name="aggressiveLazyLoading" value="false"/>

便可以实现