之前说了由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. Contest Setting 2018 ICPC Pacific Northwest Regional Contest dp

    题目:https://vj.69fa.cn/12703be72f729288b4cced17e2501850?v=1552995458 dp这个题目网上说是dp+离散化这个题目要对这些数字先处理然后进 ...

  2. linux学习笔记整理(八)

    第九章 文件的归档和压缩本节所讲内容:9.1 tar命令进行文件的归档和压缩9.2 zip管理压缩文件9.3 了解gzip-bzip2- xz管理压缩文件-file-sort查看文件 9.1 tar命 ...

  3. Thread.interrupt()

        作者:Intopass链接:https://www.zhihu.com/question/41048032/answer/89431513来源:知乎著作权归作者所有.商业转载请联系作者获得授权 ...

  4. maven 将jar包推送到自己本机的maven库

    mvn install:install-file -DgroupId=com.wdcloud.sdk -DartifactId=front-category-signed -Dversion=1.0. ...

  5. CSS--块级元素和行内元素

    相同:设置后,对应的模块都会脱离文档流 不同点:position相应的块级元素会覆盖下面的内容(文字,),而float只会覆盖块级元素,里面的文字会脱离 出来 float是浮动定位,position是 ...

  6. foreach 引用传值&

    foreach  引用传值& php 怎么在foreach中循环数组   ,的时候添加元素跟值 foreach($arr as $key => &$vo){ //注意,由于上面遍 ...

  7. Linux 文件系统管理

    Linux 文件系统管理 课程大纲  文件系统构成及命令  硬盘分区及管理  磁盘配额  备份与恢复   文件系统构成 /usr/bin ./bin:存放所有用户可以执行的命令 /usr/s ...

  8. 【js】使用javascript 实现静态网页分页效果

    <!DOCTYPE html> <html> <head> <meta http-equiv="content-Type" content ...

  9. sqlalchemy和flask-sqlalchemy的几种分页方法

    sqlalchemy中使用query查询,而flask-sqlalchemy中使用basequery查询,他们是子类与父类的关系 假设 page_index=1,page_size=10:所有分页查询 ...

  10. AI matplotlib

    matplotlib.pyplot plot(x, y):画点 show:展示