多对一映射处理
多对一映射关系,必须要在多的方添加一的对象属性
**案例:**员工对应部门信息,就是典型的多对一映射关系,需要在员工
类中添加部门
的对象属性【并且添加getter
方法和setter
方法】
// 处理多对一映射关系,必须要多的一方添加一的方的对象属性
private Dept dept;
并添加:setter方法和getter方法
方式一:级联属性赋值
Mapper
接口
/**
* 根据ID查询员工信息及对应部门信息
*/
Emp getEmpAndDeptById(@Param("eid") Integer eid);
Mapper.xml
文件
<!--处理多对一映射关系方式一:级联属性赋值-->
<resultMap id="empAndDeptResultMap" type="Emp">
<id property="eid" column="eid"/>
<result property="empName" column="emp_name"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
<result property="email" column="email"/>
<result property="dept.did" column="did"/>
<result property="dept.deptName" column="dept_name"/>
</resultMap>
<!--getEmpAndDeptById(@Param("eid") Integer eid);根据ID查询员工信息及对应部门信息-->
<select id="getEmpAndDeptById" resultMap="empAndDeptResultMap">
select e.eid,e.emp_name,e.age,e.sex,e.email,d.did,d.dept_name
from emp as e inner join dept as d on e.did=d.did
where e.eid=#{eid};
</select>
test
类
// 查询指定ID员工的信息及对应部门信息
@Test
public void testGetEmpAndDeptById(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
Emp emp = mapper.getEmpAndDeptById(1);
System.out.println(emp);
//使用级联属性赋值:
// Emp{eid=1, empName='明天', age=21, sex='女', email='123@qq.com', dept=Dept{did=1, deptName='A'}}
}
方式二:使用association
处理映射关系【重点】
例如:
<association property="dept" javaType="Dept">
<id property="did" column="did"></id>
<result property="deptName" column="dept_name" ></result>
</association>
标签解释:
association
父级标签:
property
: 设置是resultMap
中type
属性值中的javabean
类中dept
赋值javaType
表示是另一个Javabean
类
子标签:
id
表示为javaType
属性中的Dept
的属性id
值赋值
property
表示为javaType
属性中的Dept
的属性赋值
意思是:先设置javaTyp
属性是将查询到的字段值为javaType
的javabean
对象赋值,赋值完成后再将javaType
赋值的对象给propery
的属性赋值
Mapper.xml
文件
<!--处理多对一映射关系方式二:association属性解决-->
<resultMap id="empAndDeptResultMapTwo" type="Emp">
<id property="eid" column="eid"/>
<result property="empName" column="emp_name"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
<result property="email" column="email"/>
<association property="dept" javaType="Dept">
<id property="did" column="did"/>
<result property="deptName" column="dept_name"/>
</association>
</resultMap>
<!--getEmpAndDeptById(@Param("eid") Integer eid);根据ID查询员工信息及对应部门信息-->
<select id="getEmpAndDeptById" resultMap="empAndDeptResultMapTwo">
select e.eid,e.emp_name,e.age,e.sex,e.email,d.did,d.dept_name
from emp as e inner join dept as d on e.did=d.did
where e.eid=#{eid};
</select>
方式三:分步查询【最重要推荐使用】
分步查询就是将一条sql
语句,分成多步查询获取最终结果
同样需要使用association
标签,标签需要这两个属性select
和column
标签属性说明:
select:设置分步查询的sql的唯一标识(namespace.SQLId或mapper接口的全类名.方法名)
column:设置分布查询的条件
fetchType:当开启了全局的延迟加载之后,可通过此属性手动控制延迟加载的效果
步骤的解释:
- 先是第一步查询到的
did
【部门id
】给第二步查询的条件赋值,使用的属性是column
- 第二步查询到的结果给
Dept
对象赋值 - 第三步是将第二的查询到的
Dept
对象值赋值给指定的属性 如:<association property="dept"
注意是:select
是指第二条sql
的位置【位置是mapper接口的全类名.方法名
】
例如:
第一步:
<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>
<!--
select:设置分步查询的sql的唯一标识(namespace.SQLId或mapper接口的全类名.方法名)
column:设置分布查询的条件
fetchType:当开启了全局的延迟加载之后,可通过此属性手动控制延迟加载的效果
fetchType="lazy|eager":lazy表示延迟加载[默认值],eager表示立即加载
-->
<association property="dept"
select="com.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
column="did"
fetchType="eager"></association>
</resultMap>
第二步:【查询到的结果赋值给Dept对象】
<select id="getEmpAndDeptByStepTwo" resultType="Dept">
select * from dept where did = #{did};
</select>
案例:
Mapper
接口
第一步
/**
* 分步查询员工信息及对应的部门信息
* 由于每一员工只能对应一个部门,所以需要查询员工信息,
* 再根据员工对应的部门ID,查询部门信息
*/
Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);
第二步
// 分步查询第二步:根据部门ID查询部门信息
Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);
Mapper.xml
文件
第一步:
<!--处理多对一映射关系方式二:association属性解决-->
<resultMap id="empAndDeptResultMapTwo" type="Emp">
<id property="eid" column="eid"/>
<result property="empName" column="emp_name"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
<result property="email" column="email"/>
<association property="dept" javaType="Dept">
<id property="did" column="did"/>
<result property="deptName" column="dept_name"/>
</association>
</resultMap>
<!--
getEmpAndDeptByStepOne(@Param("eid") Integer eid);
分步查询员工及部门信息
-->
<select id="getEmpAndDeptByStepOne" resultMap="getEmpAndDeptByStepOneResultMap">
select * from emp where eid = #{eid};
</select>
第二步:
<select id="getEmpAndDeptByStepTwo" resultType="Dept">
select * from dept where did = #{did};
</select>
test
类
// 分步查询员工及对应部门信息
@Test
public void testGetEmpAndDeptByStep() {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
Emp emp = mapper.getEmpAndDeptByStepOne(1);
System.out.println(emp);
}