之前说了由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. DP 魔族密码 LIS

    题目描述 风之子刚走进他的考场,就…… 花花:当当当当~~偶是魅力女皇——花花!!^^(华丽出场,礼炮,鲜花) 风之子:我呕……(杀死人的眼神)快说题目!否则……-_-### 花花:……咦好冷我们现在 ...

  2. 在Linux上搭建VisualSVN Server(svn服务端)

    一.检查是否安装了低版本的SVN #  rpm -qa | grep subversion 如果已安装SVN,则会返回版本信息.这时需要卸载旧版本的SVN. 卸载旧版本SVN # yum remove ...

  3. (2)Python索引和切片

  4. swift Class的内存布局

    class Human { //8 type or isa //retainCount var age: Int?//16 var name: String?//16 var nicknames: [ ...

  5. Spark排序与去重遇见的问题

    答案: Spark的distinct是通过聚集去重的,可以简单理解为group by去重: 代码1:是先去重之后再排序取limit20是正确的, 代码2:是先排序之后再到各个节点进行去重之后再limi ...

  6. ClickHouse最简单的安装方法

    安装包地址: https://packagecloud.io/Altinity/clickhouse 无需下载安装包,更新yum源即可!! 最后: yum install -y clickhouse- ...

  7. (二 -3-3) 天猫精灵接入Home Assistant-自动发现Mqtt设备-自动生成配置信息

    http://www.hassmart.com/products/switches/#tab=config switch: - platform: mqtt name: keting state_to ...

  8. windows解决访问github慢问题

    ·1.更改host文件 文件地址: C:\Windows\System32\Drivers\etc ​ 如果不能直接修改,可拷贝到桌面修改后再复制回去 2.在host文件追加 ​ #github 19 ...

  9. Spring Security(二十):6.2.3 Form and Basic Login Options

    You might be wondering where the login form came from when you were prompted to log in, since we mad ...

  10. jquery()后续版本中,live()取消后使用on()实现功能写法

    今天做项目想用live()功能,写完打开浏览器发现报错 然后查了查发现自己用的是jquery是jquery-2.1.1.min.js,而jquery早就取消了live()方法,在后续版本里都已经没有使 ...