实体类:

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.

(1)关联查询 级联属性封装结果集

(2)使用association定义单个对象的封装规则

<!-- 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(女生),

则结果为:有部门信息

Employee [eId=101, ename=张三, gender=0, email=2714763867@qq.com]
Department [id=1, departName=销售部]

(2)Employee emp= mapper.getEmpByIdDis(103);

       若员工103的gender=1(男生),

则结果为:无部门信息,并且email的为ename的值杨张

Employee [eId=103, ename=杨张, gender=1, email=杨张]
null

</mapper>

代码:https://github.com/shuaishuaihand/mybatis.git

mybatis映射文件_select_resultMap的更多相关文章

  1. Mybatis映射文件完整模板参照

    Mybatis映射文件完整模板参照 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE map ...

  2. Mybatis映射文件中#取值时指定参数相关规则

    Mybatis映射文件中#取值时指定参数相关规则 在#{}中,除了需要的数值外,还可以规定参数的一些其他规则. 例如:javaType,jdbcType,mode(存储过程),numericScale ...

  3. SSM实战——秒杀系统之DAO层实体定义、接口设计、mybatis映射文件编写、整合Spring与Mybatis

    一:DAO实体编码 1:首先,在src目录下,新建org.myseckill.entity包,用于存放实体类: 2:实体类设计 根据前面创建的数据库表以及映射关系,创建实体类. 表一:秒杀商品表 对应 ...

  4. MyBatis 映射文件详解

    1. MyBatis 映射文件之<select>标签 <select>用来定义查询操作; "id": 唯一标识符,需要和接口中的方法名一致; paramet ...

  5. MyBatis映射文件中用#和$传递参数的特点

    在MyBatis映射文件中用#和$传递参数的特点, #是以占位符的形式来传递对应变量的参数值的,框架会对传入的参数做预编译的动作, 用$时会将传入的变量的参数值原样的传递过去,并且用$传递传递参数的时 ...

  6. MyBatis映射文件 相关操作

    一.MyBatis映射文件 1.简介 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行 ...

  7. Mybatis映射文件标签(关于sql)

    Mybatis映射文件 1.接口的全限定名和映射文件的namespace一致 <mapper namespace="com.offcn.dao.UserDao"> 2. ...

  8. MyBatis 映射文件

    Mybatis映射文件简介 1) MyBatis 的真正强大在于它的映射语句.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立即发现省掉 ...

  9. Mybatis映射文件

    Mapper XML 文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会 ...

随机推荐

  1. rrdtool ubuntu python snmpwalk

    rrdtool install: apt-get install libpango1.0-dev libxml2-dev wget https://packages.debian.org/wheezy ...

  2. C++开源库集合

    | Main | Site Index | Download | mimetic A free/GPL C++ MIME Library mimetic is a free/GPL Email lib ...

  3. Python下的正则表达式原理和优化笔记

    摘要: 本文旨在总结一些编写表达式的技巧和原理.鉴于介绍python中re模块的使用方法的文章太多.所以本文在基础方面都是略过,而在回溯原理和一些技巧方面记录一点点学习总结. 目录:[ - ] 基础规 ...

  4. 10张Gif动图让你弄懂递归等概念

    图像(包括动图)是传递信息的一种高效方式,往往能增强表象.记忆与思维等方面的反应强度.所谓一图胜千言,说的就是这个道理. 今天为大家整理了十张动图GIFS,有助于认识循环.递归.二分检索等概念的具体运 ...

  5. 0701-spring cloud config-简介、Config Server开发、Config Client开发

    一.概述 参看地址: https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_spring_ ...

  6. Linux系统——shell脚本应用示例

    传入一个网段地址,自动找出本网段内存活的IP地址.2,将存活的IP地址当作密码来创建Linux用户,用户名格式为:你的名字_数字 3,有几个存活IP地址,就自动创建几个用户   4,最后将创建的用户名 ...

  7. [笔记] Python实现全排列算法

    所谓全排列,就是给定数组,将所有的可能排列组合都枚举出来,n个元素共有n!种排列组合. 举例,对于['1', '2', '3'],全排列结果为:123,132,213,231,312,321,共有3! ...

  8. 19. Remove Nth Node From End of List(移除倒数第N的结点, 快慢指针)

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  9. webpack打包静态资源和动态资源

    1.对于静态引用的资源: <img src = "static/modelname/imgname.png"> // 修改为下面的写法 <img src = &q ...

  10. spark关于join后有重复列的问题(org.apache.spark.sql.AnalysisException: Reference '*' is ambiguous)

    问题 datafrme提供了强大的JOIN操作,但是在操作的时候,经常发现会碰到重复列的问题.在你不注意的时候,去用相关列做其他操作的时候,就会出现问题! 假如这两个字段同时存在,那么就会报错,如下: ...