表和表有关系,所映射的实体类也要有关系
表对应实体类,字段对应属性
表里面一条数据对应一个实体类对象
所以:对一对应一个对象,对多对应一个集合。
员工对部门就是多对一,一个员工对应一条部门信息也就是数据,所以一个部门信息对应部门对象
一个员工对应部门对象,反过来一个部门有多个员工信息,设置成员工的集合
1. 方式一 级联方式处理
①实体类
public class Emp {
private Integer empId;
private String empName;
private Integer age;
private String gender;
// 设置部门dept属性 表示当前员工所对应的部门
private Dept dept;
.....
}
public class Dept {
private Integer deptId;
private String deptName;
....
}
②接口
public interface EmpMapper {
// 获取员工以及所对应的部门信息
Emp getEmpAndDeptByEmpId(@Param("empId") Integer empId);
}
③映射文件
<resultMap id="empAndDeptByEmpId" type="Emp">
<id column="emp_id" property="empId"></id>
<result column="emp_name" property="empName"></result>
<result column="age" property="age"></result>
<result column="gender" property="gender"></result>
<result column="dept_id" property="dept.deptId"></result>
<result column="dept_name" property="dept.deptName"></result>
</resultMap>
<!-- Emp getEmpAndDeptByEmpId(@Param("empId") Integer empId);-->
<select id="getEmpAndDeptByEmpId" resultMap="empAndDeptByEmpId">
<!--左外连接查询-->
select
t_emp.*,t_dept.*
from t_emp
left join t_dept
on t_emp.dept_id=t_dept.dept_id
WHERE t_emp.emp_id= #{empId}
</select>
④测试方法
@Test
public void testGetEmpAndDeptByEmpId(){
SqlSession session = SqlSessionUtil.getSession();
EmpMapper mapper = session.getMapper(EmpMapper.class);
Emp empById = mapper.getEmpAndDeptByEmpId(1);
System.out.println(empById);
}
过程:员工表dept_id对应部门表的dept_id,这样表和表有了对应关系
那实体类也得有对应关系,所以,多对一,对应的是对象
在实体类的员工表中设置部门对象的属性
通过resultMap把部门的字段和通过部门对象.属性的方式映射出来。
2. 使用association处理映射关系
association:处理多对一的映射关系 (处理实体类类型的属性)
映射文件
<resultMap id="empAndDeptByEmpId" type="Emp">
<id column="emp_id" property="empId"></id>
<result column="emp_name" property="empName"></result>
<result column="age" property="age"></result>
<result column="gender" property="gender"></result>
<!-- property:设置需要处理映射关系的属性的属性名 -->
<!-- javaType:设置要处理的属性的类型-->
<association property="dept" javaType="Dept">
<id column="dept_id" property="deptId"></id>
<result column="dept_name" property="deptName"></result>
</association>
</resultMap>
<!-- Emp getEmpAndDeptByEmpId(@Param("empId") Integer empId);-->
<select id="getEmpAndDeptByEmpId" resultMap="empAndDeptByEmpId">
<!--左外连接查询-->
select
t_emp.*,t_dept.*
from t_emp
left join t_dept
on t_emp.dept_id=t_dept.dept_id
WHERE t_emp.emp_id= #{empId}
</select>
3. 分步查询
先把员工信息查询出来,把员工所对应部门的id作为条件,在部门表里面查询。
①接口
EmpMapper接口
// 通过分步查询员工以及所对应的部门信息第一步
Emp getEmpAndDeptByStep(@Param("empId") Integer empId);
public interface DeptMapper {
// 通过分步查询员工以及所对应的部门信息第二步
// 通过第一步可以查询员工所对应部门的id,所以员工所对应部门id是谁参数就写谁
Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);
}
②映射文件
EmpMapper.xml
<resultMap id="empAndDeptByEmpId" type="Emp">
<id column="emp_id" property="empId"></id>
<result column="emp_name" property="empName"></result>
<result column="age" property="age"></result>
<result column="gender" property="gender"></result>
<!--
select:分步查询的sql唯一标识,就是property属性值有哪个sql查询而来
column:将查询出的某个字段作为分步查询的sql条件
-->
<association property="dept" select="com.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo" column="dept_id">
</association>
</resultMap>
<!-- Emp getEmpAndDeptByStep(@Param("empId") Integer empId);-->
<select id="getEmpAndDeptByStep" resultMap="empAndDeptByEmpId">
select * from t_emp where emp_id = #{empId}
</select>
DeptMapper.xml
<mapper namespace="com.atguigu.mybatis.mapper.DeptMapper">
<!-- Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);-->
<select id="getEmpAndDeptByStepTwo" resultType="Dept">
select * from t_dept where dept_id =#{deptId}
</select>
</mapper>
4. 延迟加载
分步查询的优点:可以实现延迟加载
但是必须在核心配置文件中设置全局配置信息:
lazyLoadingEnabled:延迟加载的全局开关。当开启时,所有关联对象都会延迟加载
aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。
否则,每个属性会按需加载。
此时就可以实现按需加载,获取的数据是什么,就只会执行相应的sql。此时可通过association和
collection中的fetchType属性设置当前的分步查询是否使用延迟加载, fetchType="lazy(延迟加
载)|eager(立即加载)"