publicinterface EmployeeMapperPlus {
public Employee getEmpById(Integer id);
public Employee getEmpAndDept(Integer id);
public Employee getEmpByIdStep(Integer id);
public List<Employee> getEmpsByDeptId(Integer deptId);
}
接口DepartmentMapper.java
publicinterface DepartmentMapper {
public Department getDeptById(Integer id);
public Department getDeptByIdPlus(Integer id);
public Department getDeptByIdStep(Integer id);
}
EmployeeMapperPlus.xml
<?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"><mappernamespace="com.mybatis.dao.EmployeeMapperPlus"><!--自定义某个javaBean的封装规则
type:自定义规则的Java类型
id:唯一id方便引用
--><resultMaptype="com.mybatis.bean.Employee"id="MySimpleEmp"><!--指定主键列的封装规则
id定义主键会底层有优化;
column:指定哪一列
property:指定对应的javaBean属性
--><idcolumn="id"property="id"/><!-- 定义普通列封装规则 --><resultcolumn="last_name"property="last_name"/><!-- 其他不指定的列会自动封装:我们只要写resultMap就把全部的映射规则都写上。 --><resultcolumn="email"property="email"/><resultcolumn="gender"property="gender"/></resultMap><!-- public Employee getEmpById(Integer id); --><!-- resultMap:自定义结果集映射规则; --><selectid="getEmpById"resultMap="MySimpleEmp">
select * from tbl_employee where id = #{id}
</select><!--
场景一:
查询Employee的同时查询员工对应的部门
Employee===Department
一个员工有与之对应的部门信息;
id last_name gender d_id did dept_name (private Department dept;)
--><!--
联合查询:级联属性封装结果集
--><resultMaptype="com.mybatis.bean.Employee"id="MyDifEmp"><idcolumn="id"property="id"/><resultcolumn="last_name"property="last_name"/><resultcolumn="gender"property="gender"/><resultcolumn="did"property="department.id"/><resultcolumn="dept_name"property="department.departmentName"/></resultMap><!--
使用association定义关联的单个对象的封装规则;
--><resultMaptype="com.mybatis.bean.Employee"id="MyDifEmp2"><idcolumn="id"property="id"/><resultcolumn="last_name"property="last_name"/><resultcolumn="gender"property="gender"/><!-- association可以指定联合的javaBean对象
property="dept":指定哪个属性是联合的对象
javaType:指定这个属性对象的类型[不能省略]
--><associationproperty="department"javaType="com.mybatis.bean.Department"><idcolumn="did"property="id"/><resultcolumn="dept_name"property="departmentName"/></association></resultMap><!--public Employee getEmpAndDept(Integer id); --><selectid="getEmpAndDept"resultMap="MyDifEmp2">
SELECT e.id id,e.last_name last_name,e.gender gender,e.d_id d_id,
d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d
WHERE e.d_id=d.id AND e.id=#{id}
</select><!-- 使用association进行分步查询:
1、先按照员工id查询员工信息
2、根据查询员工信息中的d_id值去部门表查出部门信息
3、部门设置到员工中;
--><!-- id last_name email gender d_id --><resultMaptype="com.mybatis.bean.Employee"id="MyEmpByStep"><idcolumn="id"property="id"/><resultcolumn="last_name"property="last_name"/><resultcolumn="email"property="email"/><resultcolumn="gender"property="gender"/><!-- association定义关联对象的封装规则
select:表明当前属性是调用select指定的方法查出的结果
column:指定将哪一列的值传给这个方法
流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性
--><associationproperty="department"select="com.mybatis.dao.DepartmentMapper.getDeptById"column="d_id"></association></resultMap><!-- public Employee getEmpByIdStep(Integer id);--><selectid="getEmpByIdStep"resultMap="MyEmpByStep">
select * from tbl_employee where id=#{id}
</select><!-- 可以使用延迟加载(懒加载);(按需加载)
Employee==>Dept:
我们每次查询Employee对象的时候,都将一起查询出来。
部门信息在我们使用的时候再去查询;
分段查询的基础之上加上两个配置:
--><!--
场景二:
查询部门的时候将部门对应的所有员工信息也查询出来:注释在DepartmentMapper.xml中
--><!-- public List<Employee> getEmpsByDeptId(Integer deptId); --><selectid="getEmpsByDeptId"resultType="com.mybatis.bean.Employee">
select * from tbl_employee where d_id=#{deptId}
</select><!-- =======================鉴别器============================ --><!-- <discriminator javaType=""></discriminator>
鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为
封装Employee:
如果查出的是女生:就把部门信息查询出来,否则不查询;
如果是男生,把last_name这一列的值赋值给email;
--><resultMaptype="com.mybatis.bean.Employee"id="MyEmpDis"><idcolumn="id"property="id"/><resultcolumn="last_name"property="last_name"/><resultcolumn="email"property="email"/><resultcolumn="gender"property="gender"/><!--
column:指定判定的列名
javaType:列值对应的java类型 --><discriminatorjavaType="string"column="gender"><!--女生 resultType:指定封装的结果类型;不能缺少。/resultMap--><casevalue="0"resultType="com.mybatis.bean.Employee"><associationproperty="dept"select="com.mybatis.dao.DepartmentMapper.getDeptById"column="d_id"></association></case><!--男生 ;如果是男生,把last_name这一列的值赋值给email; --><casevalue="1"resultType="com.mybatis.bean.Employee"><idcolumn="id"property="id"/><resultcolumn="last_name"property="last_name"/><resultcolumn="last_name"property="email"/><resultcolumn="gender"property="gender"/></case></discriminator></resultMap></mapper>
DepartmentMapper.xml
<?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"><mappernamespace="com.mybatis.dao.DepartmentMapper"><!-- public Department getDeptById(Integer id); --><selectid="getDeptById"resultType="com.mybatis.bean.Department">
select id,dept_name departmentName from tbl_dept where id=#{id}
</select><!--嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则 --><resultMaptype="com.mybatis.bean.Department"id="MyDept"><idcolumn="did"property="id"/><resultcolumn="dept_name"property="departmentName"/><!--
collection定义关联集合类型的属性的封装规则
ofType:指定集合里面元素的类型
--><collectionproperty="emps"ofType="com.mybatis.bean.Employee"><!-- 定义这个集合中元素的封装规则 --><idcolumn="eid"property="id"/><resultcolumn="last_name"property="last_name"/><resultcolumn="email"property="email"/><resultcolumn="gender"property="gender"/></collection></resultMap><!-- public Department getDeptByIdPlus(Integer id); --><selectid="getDeptByIdPlus"resultMap="MyDept">
SELECT d.id did,d.dept_name dept_name,
e.id eid,e.last_name last_name,e.email email,e.gender gender
FROM tbl_dept d
LEFT JOIN tbl_employee e
ON d.id=e.d_id
WHERE d.id=#{id}
</select><!-- collection:分段查询 --><resultMaptype="com.mybatis.bean.Department"id="MyDeptStep"><idcolumn="id"property="id"/><resultcolumn="dept_name"property="departmentName"/><collectionproperty="emps"select="com.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId"column="{deptId=id}"></collection></resultMap><!-- public Department getDeptByIdStep(Integer id); --><selectid="getDeptByIdStep"resultMap="MyDeptStep">
select id,dept_name from tbl_dept where id=#{id}
</select><!-- 扩展:多列的值传递过去:
将多列的值封装map传递;
column="{key1=column1,key2=column2}"
fetchType="lazy":表示使用延迟加载;
- lazy:延迟
- eager:立即
--></mapper>