mybatis映射文件_select_resultMap
实体类:
Employee.java类:
package com.hand.mybatis.bean;
public class Employee {
private Integer eId;
private String eName;
private Integer gender;
private String email;
private Department dept;
public Employee() {
super();
}
public Employee(Integer eId,String eName, Integer gender, String email) {
super();
this.eId=eId;
this.eName = eName;
this.gender = gender;
this.email = email;
}
public Integer geteId() {
return eId;
}
public void seteId(Integer eId) {
this.eId = eId;
}
public String getEName() {
return eName;
}
public void setEname(String ename) {
this.eName = ename;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Employee [eId=" + eId + ", ename=" + eName + ", gender=" + gender + ", email=" + email + "]";
}
Department实体类:
package com.hand.mybatis.bean;
import java.util.List;
public class Department {
private Integer id;
private String departName;
private List<Employee> empList;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDepartName() {
return departName;
}
public void setDepartName(String departName) {
this.departName = departName;
}
public List<Employee> getEmpList() {
return empList;
}
public void setEmpList(List<Employee> empList) {
this.empList = empList;
}
@Override
public String toString() {
return "Department [id=" + id + ", departName=" + departName + "]";
}
}
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">
<mapper namespace="com.hand.mybatis.dao.EmployeeMapperPlus">
<!-- 自定义某个javaBean的封装规则
type:自定义规则的java类型
id:唯一id方便引用 -->
<resultMap type="com.hand.mybatis.bean.Employee" id="myemp">
<!-- 指定主键列的封装规则
id:定义主键底层会有优化
column:指定哪一列
property:指定对应的javabean属性
-->
<id column="eid" property="eId"/>
<result column="ename" property="eName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
</resultMap>
1.简单的 javaBean映射
<!-- Employee selectEmployee(Integer eid); -->mapper接口一
<select id="selectEmployee" resultMap="myemp">
SELECT * FROM emp WHERE eid=#{eid}
</select>
<!-- 查询Employee的同时查询员工对应的部门
Employee==Department
一个员工有与之对应的部门信息
-->
<!-- (1).关联查询 级联属性封装结果集-->
<resultMap type="com.hand.mybatis.bean.Employee" id="myemp1">
<id column="eid" property="eId"/>
<result column="ename" property="eName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<result column="did" property="dept.id"/>
<result column="deptname" property="dept.departName"/>
</resultMap>
<!-- (2).使用association定义单个对象的封装规则
-->
<resultMap type="com.hand.mybatis.bean.Employee" id="myemp2">
<id column="eid" property="eId"/>
<result column="ename" property="eName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<!-- assosiation可以指定javaBean对象
property="dept":指定哪一个是关联的对象
javaType:指定这个属性对象的类型[不能省略]-->
<association property="dept" javaType="com.hand.mybatis.bean.Department">
<id column="did" property="id"/>
<result column="deptname" property="departName"/>
</association>
</resultMap>
2.
<!-- Employee getEmpAndDept(Integer eid); -->mapper接口二
<select id="getEmpAndDept" resultMap="myemp2">//根据员工id查询出员工和部门的所有字段
select emp.eid,emp.ename,emp.gender,emp.email,dept.did,dept.deptname
from emp,dept
WHERE emp.did=dept.did AND
emp.eid=#{eid}
</select>
<!-- 3.使用association进行分步查询
1.先按照员工id查询员工信息
2.根据查询员工信息中的did值去部门查询部门信息
3.部门设置到员工中 -->
<resultMap type="com.hand.mybatis.bean.Employee" id="myempStep">//分步查询
<id column="eid" property="eId"/>
<result column="ename" property="eName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<!-- association定义关联对象的封装规则
select:表明当前属性调用select指定的方法查询结果
column:指定将那一列的值传给这个方法-->
<!-- !!!有问题查不出来结果 -->
<association property="dept"
select="com.hand.mybatis.dao.DepartmentMapper.getDeptById" //使用association中的select标签调动DepartmentMapper.getDeptById接口查询部门信息
column="did">
</association>
</resultMap>
<!--Employee getEmpByIdStep(Integer eid); -->mapper接口三
<select id="getEmpByIdStep" resultMap="myempStep">
select * from emp where eid=#{eid}
</select>
<!--3. discriminator鉴别器 -->
<resultMap type="com.hand.mybatis.bean.Employee" id="mydepDis">
<id column="eid" property="eId"/>
<result column="ename" property="eName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<!--
column:指定判断的列名
javaType:列值对应的java类型 -->
<discriminator javaType="string" column="gender">
<!-- 女生 resultType:指定封装的结果类型,不能缺少,resultMap -->
<case value="0" resultType="com.hand.mybatis.bean.Employee">//gender为0(女生),则查询部门信息
<association property="dept"
select="com.hand.mybatis.dao.DepartmentMapper.getDeptById" //根据部门id查询部门信息接口
column="did">
</association>
</case>
<!-- 男生 :如果是男生,把ename这一列的值赋值给email-->
<case value="1" resultType="com.hand.mybatis.bean.Employee">//gender为1(男生),将ename这一列的值赋值给emai
<id column="eid" property="eId"/>
<result column="ename" property="eName"/>
<result column="gender" property="gender"/>
<result column="eName" property="email"/>
</case>
</discriminator>
</resultMap>
<!--Employee getEmpByIdDis(Integer eid); -->接口四
<select id="getEmpByIdDis" resultMap="mydepDis">
select * from emp where eid=#{eid}
</select>
接口四结果:
(1) Employee emp= mapper.getEmpByIdDis(101);
若员工101的gender=0(女生),
则结果为:有部门信息
Department [id=1, departName=销售部]
(2)Employee emp= mapper.getEmpByIdDis(103);
则结果为:无部门信息,并且email的为ename的值杨张
null
</mapper>
代码:https://github.com/shuaishuaihand/mybatis.git