之前说了由Employee找Department,这一节讲一讲由Department找Employee,显然前者是多对一的关系,而后者是一对多的关系。

Department的JavaBean:

private Integer id;
private String departmentName;
private List<Employee> employeeList;

接口中的方法:

Department getDepByIdPlus(Integer id);

查询的SQL语句:

<select id="getDepByIdPlus" resultMap="Dep">
SELECT d.`id` did,d.`department_name` dep_name,e.`id` eid,e.`last_name` last_name,e.`email` email,e.`gender` gender
FROM tb_department d LEFT JOIN tb_employee e
ON d.`id`=e.`d_id`
WHERE d.`id`=2;
</select>

接下来编写resultMap,

collection:定义关联集合类型的属性的封装规则

ofType:指定集合里面的元素类型

<resultMap id="Dep" type="com.figsprite.bean.Department">
<id property="id" column="did"/>
<result property="departmentName" column="dep_name"/>
<collection property="employeeList" ofType="com.figsprite.bean.Employee">
<id property="id" column="eid"/>
<result property="lastName" column="last_name"/>
<result property="gender" column="gender"/>
<result property="email" column="email"/>
</collection>
</resultMap>

其实就是一个resultMap套着另外一个resultMap格式的collection

@Test
public void test2() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession sqlOpenSession = sqlSessionFactory.openSession();
);
for (Employee e : department.getEmployeeList()) {
System.out.println(e);
}
} finally {
sqlOpenSession.close();
}
}

collection标签的分步查询

与之前的association基本一致

  1. <resultMap id="DepStep" type="com.figsprite.bean.Department">
  2. <id property="id" column="id"/>
  3. <result property="departmentName" column="department_name"/>
  4. <collection property="employeeList"
  5. select="com.figsprite.dao.EmployeeMapperPlus.getDepByEmp"
  6. column="id">
  7. </collection>
  8. </resultMap>

11. <select id="getDepByIdStep" resultMap="DepStep">

12.     select id,department_name from tb_department where id=#{id}

13. </select>

  1. @Test
  2. public void test1() throws IOException {
  3. SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
  4. SqlSession sqlOpenSession = sqlSessionFactory.openSession();
  5. try {
  6. DepartmentMapper departmentMapper = sqlOpenSession.getMapper(DepartmentMapper.class);
  7. Department department = departmentMapper.getDepByIdStep(1);
  8. System.out.println(department.getEmployeeList().get(0));
  9. } finally {
  10. sqlOpenSession.close();
  11. }

13. }

DEBUG [main] - ==>  Preparing: select id,department_name from tb_department where id=?

DEBUG [main] - ==> Parameters: 1(Integer)

DEBUG [main] - <==      Total: 1

DEBUG [main] - ==>  Preparing: select * from tb_employee where d_id=?

DEBUG [main] - ==> Parameters: 1(Integer)

DEBUG [main] - <==      Total: 2

日志打印出的是两条SQL语句

多值传递的分步查询

上面的例子中,无论是association还是collection在第一步SQL语句中传的都是单一值给第二条SQL语句当条件,接下来介绍第一步SQL语句传多值给SQL语句。

只要将这些多列值封装成map传递即可,

column={key1=column1,key2=column2}

  1. <resultMap id="DepStep" type="com.figsprite.bean.Department">
  2. <id property="id" column="id"/>
  3. <result property="departmentName" column="department_name"/>
  4. <collection property="employeeList"
  5. select="com.figsprite.dao.EmployeeMapperPlus.getDepByEmp"
  6. column="{did=id}">
  7. </collection>
  8. </resultMap>

注意这里column的写法。

在分步查询的时候还有一个属性fetchType,在默认情况下它的值是lazy,表示使用延迟,eager立即查询,这样即使全局设置了分步查询也不会有影响。

鉴别器discriminator

Mybatis可以使用鉴别器判断某列值,然后根据这个值做不同的封装行为。

MyBatis映射文件6的更多相关文章

  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 代码进行对比,你会 ...

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

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

随机推荐

  1. Linux系统安装和网络配置

    系统下载 CentOS 6.x 50% 6.9    ---- 常用 CentOS 7.x 50% 7.2   ----常用 官网-国外 https://wiki.centos.org/Downloa ...

  2. cocos2d-x 绘制图形

    转载请注明出处:http://blog.csdn.net/oyangyufu/article/details/25841727 绘制图形例如以下:   程序代码: 须要又一次定义父类虚函数draw() ...

  3. idea在maven打包时运行Test测试, 导致打包失败, 乱七八糟的错误

    在maven打包时运行Test测试, 导致打包失败, 乱七八糟的错误 在maven projects中图标toggle'skip Tests' Mode //宏杰帮助 网上案例:https://blo ...

  4. centos7下kubernetes(7.kubernetesScale Up/Down)

    伸缩(Scale up/down)是指在线增加或减少pod副本数量 通过yml文件创建两个nginx的pod 先查看一下nginx的yml文件: 通过kubectl apply -f创建 通过kube ...

  5. 【转】理解WebKit和Chromium: JavaScript引擎简介

    转载请注明原文地址:http://blog.csdn.net/milado_nju1. 什么是JavaScript引擎什么是JavaScript引擎?简单来讲,就是能够提供执行JavaScript代码 ...

  6. java 生成txt文件

    FileWriter fileWriter = new FileWriter("C:/Users/li/Desktop/a.txt"); fileWriter.write(“aaa ...

  7. 010_动态语言与鸭子类型及python2和3的区别

    一. 动态语言中经常提到鸭子类型,所谓鸭子类型就是:如果走起路来像鸭子,叫起来也像鸭子,那么它就是鸭子(If it walks like a duck and quacks like a duck, ...

  8. Spring Security(十四):5.4 Authorize Requests

    Our examples have only required users to be authenticated and have done so for every URL in our appl ...

  9. JDK动态代理(2)--------反射Method的作用

    Person类做例子 package com.spring.aop.proxy; public class Preson { Preson() { System.out.println("t ...

  10. Python排序算法——选择排序

    有趣的事,Python永远不会缺席! 如需转发,请注明出处:小婷儿的python https://www.cnblogs.com/xxtalhr/p/10787340.html 一.选择排序(Sele ...