Mybatis中关联查询和resultMap使用

1.配置myBatis-config.xml

1
2
3
4
5
6
7
8
9
10
11
12
<!-- 通过别名简化对类的使用 -->
<typeAliases>
<typeAlias type="cn.itcast.entity.Dept" alias="Dept" />
<typeAlias type="cn.itcast.entity.Emp" alias="Emp" />
</typeAliases>
…….
<!--导入SQL映射文件 -->
<mappers>
<mapper resource="cn/itcast/entity/DeptMapper.xml" />
<mapper resource="cn/itcast/entity/EmpMapper.xml" />
</mappers>

2.1 基于association查询(用于多对一或一对一)

配置DeptMapper.xml

1
2
3
4
5
6
7
8
9
10
11
<mapper namespace="cn.itcast.entity.DeptMapper">

<!--resultMap用于查询,可以把查询后字段值封装到对应类的属性, type指定的是对应的实体类 -->
<resultMap type="Dept" id="deptResultMap">
<!-- id用来配置表的主键与类的属性的映射关系 ,column指定的是表的字段名; property指定的是类的属性名-->
<id column="dept_id" property="deptId"/>
<!-- result用来配置 普通字段与类的属性的映射关系 ,column指定的是表的字段名; property指定的是类的属性名-->
<result column="dept_name" property="deptName"/>
<result column="dept_address" property="deptAddress"/>
</resultMap>
</mapper>

EmpMapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<mapper namespace="cn.itcast.entity.EmpMapper">

<!-- 表字段和对应实体属性命名一致时可以不配置 -->
<resultMap id="empResultMap" type="Emp">
<id property="empId" column="emp_id" />
<result property="empName" column="emp_name" />
<result property="empSex" column="emp_sex" />
<!-- association配置对一关联 -->
<association property="dept" column="dept_id" javaType="Dept" resultMap="cn.itcast.entity.DeptMapper.deptResultMap" />
</resultMap>

<!--根据部门名称查询员工(包括员工所在部门)信息 -->
<select id="selectEmpDeptList" parameterType="Emp" resultMap="empResultMap">
<!-- 访问emp.dept.deptName, 前面不用写emp, 直接写 dept.deptName-->
select e.*,d.* from emp e inner join dept d on
e.dept_id=d.dept_id where d.dept_name like #{dept.deptName}
</select>
</mapper>

2.2 基于collection查询(用于一对多或多对多)

配置DeptMapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<mapper namespace="cn.itcast.entity.DeptMapper">

<!-- 表字段和对应实体属性命名一致时可以不配置 -->
<resultMap id="deptResultMap" type="Dept">
<id property="deptId" column="dept_id" />
<result property="deptName" column="dept_name" />
<result property="deptAddress" column="dept_address" />
<!-- collection中resultMap引用的是其它文件的map 需要命名空间+id,例如:cn.itcast.entity.EmpMapper.empResultMap -->
<collection property="emps" ofType="Emp" resultMap="cn.itcast.entity.EmpMapper.empResultMap"/>
</resultMap>

<select id="selectDeptEmpList" parameterType="Dept" resultMap="deptResultMap">
select d.*, e.* from dept d inner join emp e on d.dept_id=e.dept_id where d.dept_name like #{deptName}
</select>
</mapper>

EmpMapper.xml

1
2
3
4
5
6
7
8
<mapper namespace="cn.itcast.entity.EmpMapper">
<!-- 表字段和对应实体属性命名一致时可以不配置 -->
<resultMap id="empResultMap" type="Emp">
<id property="empId" column="emp_id" />
<result property="empName" column="emp_name" />
<result property="empSex" column="emp_sex" />
</resultMap>
</mapper>

2.3 一对多双向关联查询示例

配置DeptMapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<mapper namespace="cn.itcast.entity.DeptMapper">

<!-- 表字段和对应实体属性命名一致时可以不配置 -->
<resultMap id="deptResultMap" type="Dept">
<id property="deptId" column="dept_id" />
<result property="deptName" column="dept_name" />
<result property="deptAddress" column="dept_address" />
</resultMap>

<!-- 一对多时,“多”的关联属性可独立配置resultMap,采用extends继承基本属性的resultMap -->
<resultMap id="deptExtResultMap" type="Dept" extends="deptResultMap">
<!-- collection中resultMap引用的是其它文件的map 需要命名空间+id,例如:cn.itcast.entity.EmpMapper.empResultMap -->
<collection property="emps" ofType="Emp"
resultMap="cn.itcast.entity.EmpMapper.empResultMap" />
</resultMap>

<!--用于部门和员工关联查询,返回部门信息(包含部门员工信息)列表,采用extends继承基本属性的resultMap -->
<select id="selectDeptEmpList" parameterType="Dept" resultMap="deptExtResultMap">
select d.*, e.* from dept d inner join emp e on d.dept_id=e.dept_id
where d.dept_name like #{deptName}
</select>

</mapper>

EmpMapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<mapper namespace="cn.itcast.entity.EmpMapper">

<!-- 表字段和对应实体属性命名一致时可以不配置 -->
<resultMap id="empResultMap" type="Emp">
<id property="empId" column="emp_id" />
<result property="empName" column="emp_name" />
<result property="empSex" column="emp_sex" />
<!--注意association元素的resultMap的值为没有配置“多”的属性映射的deptResultMap,如下 -->
<association property="dept" column="dept_id" resultMap="cn.itcast.entity.DeptMapper.deptResultMap"/>
</resultMap>

<!-- 用于员工和部门关联查询,返回员工信息(包含部门信息)列表 -->
<select id="selectEmpDeptList" parameterType="Emp" resultMap="empResultMap">
select e.*,d.* from emp e inner join dept d on
e.dept_id=d.dept_id where d.dept_name like #{dept.deptName}
</select>

</mapper>