mybatis返回结果ResultMap
<?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="com.atguigu.mybatis.mapper.EmployeeMapper2">
<!--
需求:在查询员工信息的同时将部门信息也查询出来
-->
<select id="getEmployeeByIdContainsDept" resultMap="cutomMap">
SELECT e.id e_id,e.last_name lastName,e.email,e.salary,d.id d_id,d.name d_name
FROM employees e
LEFT JOIN departments d
ON e.dept_id = d.id
WHERE e.id = #{id};
</select>
<!-- 自定义结果集 -->
<!--
type:指定要映射的对象所对应的类的全类名
id:起一个唯一的标识,以便被引用
-->
<resultMap type="com.atguigu.mybatis.entities.Employee2" id="cutomMap">
<!-- 映射主键
column:指定数据库中的列名
property:指定要映射的对象的属性名
-->
<id column="e_id" property="id"/>
<!-- 映射其他列 -->
<result column="lastName" property="lastName"/>
<result column="email" property="email"/>
<result column="salary" property="salary"/>
<!-- 通过级联属性进行映射 -->
<result column="d_id" property="dept.id"/>
<result column="d_name" property="dept.deptName"/>
</resultMap>
</mapper>
<!--
需求:在查询员工信息的同时将部门信息也查询出来
-->
<select id="getEmployeeByIdContainsDept" resultMap="cutomMap">
SELECT e.id e_id,e.last_name lastName,e.email,e.salary,d.id d_id,d.name d_name
FROM employees e
LEFT JOIN departments d
ON e.dept_id = d.id
WHERE e.id = #{id};
</select>
<!-- 自定义结果集 -->
<!--
type:指定要映射的对象所对应的类的全类名
id:起一个唯一的标识,以便被引用
-->
<resultMap type="com.atguigu.mybatis.entities.Employee" id="cutomMap">
<!-- 映射主键
column:指定数据库中的列名
property:指定要映射的对象的属性名
-->
<id column="e_id" property="id"/>
<!-- 映射其他列 -->
<result column="lastName" property="lastName"/>
<result column="email" property="email"/>
<result column="salary" property="salary"/>
<!-- 通过级联属性进行映射 -->
<result column="d_id" property="dept.id"/>
<result column="d_name" property="dept.deptName"/>
</resultMap>
使用<association >联合查询
<select id="getEmployeeByWithAssociation" resultMap="customMap2">
SELECT e.*,d.id d_id,d.name d_name
FROM employees e
LEFT JOIN departments d
ON e.dept_id = d.id
WHERE e.id = #{id};
</select>
<!-- 自定义结果集 -->
<resultMap type="com.atguigu.mybatis.entities.Employee" id="customMap2">
<!-- 映射主键 -->
<id column="id" property="id"/>
<!-- 映射其他列 -->
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="salary" property="salary"/>
<!-- 通过association进行联合查询
property:设置要映射属性的属性名
javaType:设置要映射的属性的类型
-->
<association property="dept" javaType="com.atguigu.mybatis.entities.Department">
<!-- 映射主键 -->
<id column="d_id" property="id"/>
<!-- 映射其他列 -->
<result column="d_name" property="deptName"/>
</association>
</resultMap>
分步骤查询
public Employee getDepartmentById(Integer id);
<mapper namespace="com.atgui.spring.mybatis.mapper.DepartmentMapper">
<select id="getDepartmentById" resultType="com.atgui.spring.mybatis.entities.Department">
select id,name from departments where id=#{id}
</select>
public Employee getEmployeeAndDepartmentByAssociationFenbu(Integer id);
<!--
通过association进行分步查询:
1.根据员工的id将Employee查询出来
2.根据Employee中部门的id将部门查询出来
3.将部门设置到Employee中
-->
<select id="getEmployeeAndDepartmentByAssociationFenbu" resultMap="customMap3">
select * from employees where id=#{id}
</select>
<resultMap type="com.atgui.spring.mybatis.entities.Employee" id="customMap3">
<id column="e_id" property="id"></id>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="salary" property="salary"/>
<result column="dept_id" property="deptId"/>
<association property="dept" select="com.atgui.spring.mybatis.mapper.DepartmentMapper.getDepartmentById"
column="dept_id">
</association>
</resultMap>
<!-- 自定义 结果集 -->
<resultMap type="com.atguigu.mybatis.entities.Employee" id="customMap3">
<!-- 映射主键 -->
<id column="id" property="id"/>
<!-- 映射其他列 -->
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="salary" property="salary"/>
<!-- 通过association进行分步查询
select:设置调用那个接口的那个方法查询该属性
column:设置将Employe中那一列的值传入到select属性中的方法中
-->
多个参数查询
<!--
扩展:分步查询传入多个值得情况:
使用{key1=column1,key2=column2…}的形式来传入多个值
-->
<resultMap type="com.atgui.spring.mybatis.entities.Employee" id="customMap3">
<id column="e_id" property="id"></id>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="salary" property="salary"/>
<result column="dept_id" property="deptId"/>
<association property="dept" select="com.atgui.spring.mybatis.mapper.DepartmentMapper.getDepartmentById"
column="{deptId=dept_id}" fetchType="eager">
</association>
</resultMap>