一、关联查询
关联关系
-
一对一
-
一对多(多对一)
-
多对多
1.1 单向关联关系
只能由一方查询出另一方:
比如一对多和(多对一):
只能由一方查询出多方,或者只能由多方查询出一方(类似一对一)
1.2 双向关联关系
关联的双方可以互相查询
比如一对多和(多对一):
既能由一方查询出多方,也能由多方查询出一方(类似一对一)
1.3 自关联查询
自己查询出自己的上级或者下级同类的对象的信息
二、关联关系查询
场景:员工和部门符合一对多(多对一)的关联关系映射
2.1 many2one(多对一)
情况一: 查询员工并只从员工查询部门(从多方查一方)
可以使用两种方式查询
方式一:使用等值、内联机、外连接这种连接查询语句来查询关联信息
要点:在SQL语句中一次性将需要的信息全部查询出来
<!--resultMap完成手动映射-->
<resultMap id="empMapper" type="emp">
<id column="eid" property="eid"/>
<result column="ename" property="ename"/>
<result column="age" property="age"/>
<result column="gender" property="gender"/>
<!--多方查询一方,使用association标签
property属性:设置关联属性
javaType属性: 设置封装的JavaBean的类型
-->
<association property="apart" javaType="apart">
<id column="aid" property="aid"/>
<result column="aname" property="aname"/>
</association>
</resultMap>
<select id="findByEid" resultMap="empMapper">
select e.eid, e.ename, e.age, e.gender, a.aid, a.aname
from t_emp as e
inner join t_apart a
on e.aid = a.aid
and e.eid = #{eid}
</select>
方式二:使用子查询方式查询
员工映射文件
<!--resultMap完成手动映射-->
<resultMap id="empMapper" type="emp">
<id column="eid" property="eid"/>
<result column="ename" property="ename"/>
<result column="age" property="age"/>
<result column="gender" property="gender"/>
<!--使用association标签
column属性: 设置列名
property属性: 设置属性名
javaType属性:设置关联的属性的类型
select属性: 指定另一个映射器方法来查询
-->
<association column="aid" property="apart"
javaType="apart"
select="com.dyh.dao.ApartMapper.findByAid"/>
</resultMap>
<select id="findByEid" resultMap="empMapper">
select eid, ename, age, gender, aid from t_emp
where eid = #{eid}
</select>
部门映射文件
<?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">
<mapper namespace="com.dyh.dao.ApartMapper">
<resultMap id="apartMapper" type="apart">
<id column="aid" property="aid"/>
<result column="aname" property="aname"/>
</resultMap>
<select id="findByAid" resultMap="apartMapper">
select aid, aname from t_apart
where aid = #{aid}
</select>
</mapper>
2.2 One2Many(一对多)
方式一:连接查询
<!--resultMap完成手动映射-->
<resultMap id="apartMapper" type="apart">
<id column="aid" property="aid"/>
<result column="aname" property="aname"/>
<!--如果关联属性是集合,一方查询多方
使用collection标签
property属性:设置属性名称
ofType属性:设置集合中元素的类型
-->
<collection property="emps" ofType="emp">
<id column="eid" property="eid"/>
<result column="ename" property="ename"/>
<result column="age" property="age"/>
<result column="gender" property="gender"/>
</collection>
</resultMap>
<select id="findByAid" resultMap="apartMapper">
select a.aid, a.aname, e.eid, e.ename, e.age, e.gender
from t_apart as a
inner join t_emp as e
on a.aid = e.aid
and a.aid = #{aid}
</select>
方式二: 子查询
部门映射文件
<!--resultMap完成手动映射-->
<resultMap id="apartMapper" type="apart">
<id column="aid" property="aid"/>
<result column="aname" property="aname"/>
<collection property="emps" ofType="emp" column="aid"
select="com.dyh.dao.EmpMapper.findEmpsByAid"/>
</resultMap>
<select id="findByAid" resultMap="apartMapper">
select aid, aname from t_apart
where aid = #{aid}
</select>
员工的映射文件
2.3 双向关联
既可以从一方查询多方,又可以从多方查询一方
<!--resultMap完成手动映射-->
<resultMap id="empMapper" type="emp">
<id column="eid" property="eid"/>
<result column="ename" property="ename"/>
<result column="age" property="age"/>
<result column="gender" property="gender"/>
</resultMap>
<select id="findEmpsByAid" resultMap="empMapper">
select eid, ename, age, gender from t_emp
where aid = #{aid}
</select>
2.4 多对多
场景:学生和兴趣班
2.5 自关联
比如菜单(menu)、新闻标签(newsLabel)有一级、二级、三级...菜单,自关联就是,其中一个菜单能查询出其父节点,或者查询出其子节点