知识点:查询一个实体类,并查出这个类下面的集合

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.java实体类

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 + "]";
    }
   
}

DepartmentMapper.java接口

package com.hand.mybatis.dao;
import com.hand.mybatis.bean.Department;

public interface DepartmentMapper {

Department getDeptById(Integer did);
    
    //根据部门id,查询部门信息,同时将部门下的所有员工信息查询出来
     Department getDeptByIdPlus(Integer did);
    
     //分步查询部门信息
     Department getDeptByIdStep(Integer did);
}

DepartmentMapper.xm l映射文件

<?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.DepartmentMapper">

<!-- Department getDeptById(Integer did); -->
   <select id="getDeptById"  resultType="com.hand.mybatis.bean.Department">
       SELECT did id,deptname departName FROM dept WHERE did=#{did}
   </select>
   
    <!--1.查询部门的时候将部门对应的所有员工信息也查询出来
    -->
   
    <resultMap type="com.hand.mybatis.bean.Department" id="departmentMap">
        <result property="id" column="did"/>
        <result property="departName" column="deptname"/>
        <!--collection定义关联集合类型的属性的封装规则
            ofType:指定集合里面元素的类型  -->
        <collection property="empList" ofType="com.hand.mybatis.bean.Employee">
         <!-- 定义这个集合元素的封装规则 -->
          <id column="eid" property="eId"/>
          <result column="ename" property="eName"/>
          <result column="email" property="email"/>
          <result column="gender" property="gender"/>
        </collection>
    </resultMap>
      
    <!-- Department getDeptByIdStep(Integer did); -->   接口一:根据部门did查询部门信息,员工信息   
    <select id="getDeptByIdPlus" resultMap="departmentMap">
        SELECT d.did,d.deptname,e.eid,e.ename,e.email,e.gender
        FROM dept d
        LEFT JOIN emp e
        ON d.did=e.did
        WHERE d.did=#{did}
    </select>
    
    
    <!--2. select_resultMap关联查询collection分步查询 -->
    
    <resultMap type="com.hand.mybatis.bean.Department" id="departmentMapPlus">
      <id column="did" property="id"/>
      <result column="deptname" property="departName"/>
      <collection property="empList"
          select="com.hand.mybatis.dao.EmployeeMapper.getEmpByDeptId"
          column="{dId=did}" fetchType="lazy">//可以多列传值
      </collection>
    </resultMap>

<!--Department getDeptByIdStep(Integer did); -->接口二:根据部门id分步查询员工信息
   <select id="getDeptByIdStep" resultMap="departmentMapPlus">
      SELECT did,deptname
      from dept
      WHERE did=#{did}
   </select>
 
  <!--  扩展,多列的值传递过去,将多列的值封装map传递
       column="{key1=column1,key2=column2}"
       fetchType="lazy":表示使用延迟加载
       - lazy:延迟
       - eager:立即-->
 
</mapper>

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

mybatis映射文件select_resultMap_关联查询_collection定义关联集合的更多相关文章

  1. Mybatis3.1-[tp_34-35]-_映射文件_select_resultMap关联查询_collection定义关联集合封装规则_collection分步查询_延迟加载

    笔记要点出错分析与总结工程组织 1.定义接口 interface DepartmentMapper package com.dao; import com.bean.Department; publi ...

  2. MyBatis映射文件的resultMap如何做表关联

    MyBatis的核心是其映射文件,SqlMap文件,里面配置了项目中用到了什么SQL语句,和数据库相关的逻辑都在这个映射文件里.顾名思义,映射文件就是对Java对象和SQL的映射.这里简单介绍一下映射 ...

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

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

  4. MyBatis 映射文件详解

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

  5. MyBatis映射文件 相关操作

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

  6. MyBatis 映射文件

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

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

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

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

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

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

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

随机推荐

  1. ArcEngine之Provide your license server administrator with the following information.error code =-42

    今天打开VS,不一会就出现了下面的对话框,感到非常疑惑,仔细一想,原来是昨天不小心把权限弄错了! 解决办法:在控价中找到AxLicenseControl,右键属性,把权限改为ArcGIS Engine ...

  2. 大文本 mysql es

    大文本 mysql  es mysql  id longText  ---> es  longText mysqlId 大文本先入mysql,再同步至es: 文本查询逻辑交由es实现: mysq ...

  3. QQ空间发表日志的图片上传功能实现

    w间接促使了用户注意图片的顺序,进一步优化的方向的是手指触动或鼠标点击来同时进行图片的增删和调序,避免精确的数字输入. 有效code <form action="wcon/wact&q ...

  4. Tr A--hdu1575(矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575 算是模板吧 #include <iostream> #include <std ...

  5. ubuntu开机自动加载iptables配置(转)

    原文:http://www.xuebuyuan.com/730127.html iptables的使用参见http://wiki.ubuntu.org.cn/IptablesHowTo iptable ...

  6. 【我的Android进阶之旅】解决Center OS 64位系统编译Android APP报错error=2和finished with non-zero exit value 127

    一.错误描述 1.问题 java.io.IOException: error=2, 没有那个文件或目录 今天在刚重新搭建好的64位的Center OS上安装好了Android SDK,Jenkins, ...

  7. foo ?

    我们经常看到一些基础教程,面试题中经经常使用foo来命名,甚至有时候我们也会用过,可是你是否又知道foo是什么意思?(实际上,知道不知道又不会对你编码有不论什么影响~) 从编程黑马的王轶男的话来解释, ...

  8. UVA10026:Shoemaker's Problem(贪心)

    题目链接:  http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68990#problem/K 题目需求:鞋匠有n个任务,第i个任务要花费ti ...

  9. phpstudy2016 redis扩展 windows

    第一步,查看环境的信息. 第二步,根据线程是否安全.架构32位或64位下载redis扩展. http://pecl.php.net/package-stats.php 第三步,php_redis.dl ...

  10. 国内NLP的那些人那些会

    统计学和语言学专家都列在一起了,没有区分.1,黄昌宁,1937年生于广东,1955年考入清华大学电机系,1961年毕业并留校任教至博士生导师, 1983-1984年赴美国耶鲁大学进修,1986-198 ...