映射(多、一)对一的关联关系

1)若只想得到关联对象的id属性,不用关联数据表

2)若希望得到关联对象的其他属性,要关联其数据表

举例:

员工与部门的映射关系为:多对一

1.创建表

员工表

确定其外键是部门表的 id

DROP TABLE IF EXISTS emp;

CREATE TABLE emp(
id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
emp_name VARCHAR(255) DEFAULT NULL,
gender CHAR(1) DEFAULT NULL,
email VARCHAR(255) DEFAULT NULL,
dept_id INT(22) DEFAULT NULL,
FOREIGN KEY (dept_id) REFERENCES dept(id)
)

部门表

CREATE TABLE dept (
id INT(11) PRIMARY KEY AUTO_INCREMENT,
dept_name VARCHAR(255)
)

2.创建相应的实体类和Mapper接口

查询的方法有三!

方法一:

- 写关联的 sql 语句

将两个表通过部门表的id关联,查找员工表id为1的员工信息

SELECT e.id,e.emp_name,e.gender,e.email,e.dept_id,d.id,d.dept_name
FROM emp e,dept d
WHERE e.dept_id = d.id AND e.id = 1

- 在sql映射文件中写映射sql语句【联合查询:级联属性封装结果集】

使用resultMap映射,对于【对一】关系可以不用 association

<resultMap type="com.neuedu.mybatis.entity.TblEmployee" id="getEmployeeByIdMap">
<!-- Employee -->
<id column="id" property="id"/>
<result column="emp_name" property="empName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<!-- Department -->
<result column="id" property="dept.id"/>
<result column="dept_name" property="dept.deptName"/>
</resultMap>
<select id="getEmployeeById" resultMap="getEmployeeByIdMap">
SELECT e.id,e.emp_name,e.gender,e.email,e.dept_id,d.id,d.dept_name
FROM emp e,dept d
WHERE e.dept_id = d.id AND e.id = #{id}
</select>

- 编写测试用例

private ApplicationContext ioc = new ClassPathXmlApplicationContext("bean.xml");
@Test
public void test() {
TblEmployeeMapper bean = ioc.getBean(TblEmployeeMapper.class);
TblEmployee employee = bean.getEmployeeById(1);
System.out.println(employee);
}

方法二:使用association来定义关联对象的规则,【比较正规的,推荐的方式】

<resultMap type="com.neuedu.mybatis.entity.TblEmployee" id="getEmployeeByIdMap">
<!-- Employee -->
<id column="id" property="id"/>
<result column="emp_name" property="empName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<!--
association可以指定联合的javaBean对象
property="dept":指定哪个属性是联合的对象
javaType:指定这个属性对象的类型【不能省略】
-->
<association property="dept" javaType="com.neuedu.mybatis.entity.TblDepartment">
<id column="id" property="id"/>
<result column="dept_name" property="deptName"/>
</association>
</resultMap> <select id="getEmployeeById" resultMap="getEmployeeByIdMap">
SELECT e.id,e.emp_name,e.gender,e.email,e.dept_id,d.id,d.dept_name
FROM emp e,dept d
WHERE e.dept_id = d.id AND e.id = #{id}
</select>

方法三:上述结果相当于使用嵌套结果集的形式,还可以使用 association 进行分步查询:

使用association进行分步查询:
1.先按照员工id查询员工信息
2.根据查询员工信息中d_id值取部门表查出部门信息
3.部门设置到员工中:
<!-- 通过id与resultMap相连 -->
<select id="selectDept" resultType="com.neuedu.mybatis.entity.TblDepartment">
select d.id,d.dept_name
from dept d
where d.id = #{id}
</select>
<!-- 通过id被查找employee的select找到 -->
<resultMap type="com.neuedu.mybatis.entity.TblEmployee" id="getEmployeeByIdMap">
<id column="id" property="id"/>
<result column="emp_name" property="empName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<!-- property:是employee中的属性,
select:dept的select语句的id
column:查找到的部门的id,传到dept的select中
-->
<association property="dept" select="selectDept" column="dept_id"/>
</resultMap> <select id="getEmployeeById" resultMap="getEmployeeByIdMap">
SELECT e.id,e.emp_name,e.gender,e.email,e.dept_id
from emp e
where e.id = #{id}
</select>
修改:
由于上个方法中有两个select
一个针对employee,另一个针对department
所以另写一个DepartmentMapper,存放针对 department 的 select
在 DepartmentMapper 中:
<mapper namespace="com.neuedu.mybatis.mapper.TblDepartmentMapper">

      <select id="selectDept" resultType="com.neuedu.mybatis.entity.TblDepartment">
select d.id,d.dept_name
from dept d
where d.id = #{id}
</select> </mapper>

之前的 association 中的select是写 id,现在写 DepartmentMapper 的全类名+ id

<resultMap type="com.neuedu.mybatis.entity.TblEmployee" id="getEmployeeByIdMap">
<id column="id" property="id"/>
<result column="emp_name" property="empName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<!-- property:是employee中的属性,
select:select语句的id
column:查找到的部门的id,传到查找dept的select中
-->
<association property="dept" select="com.neuedu.mybatis.mapper.TblDepartmentMapper.selectDept" column="dept_id"/>
</resultMap> <select id="getEmployeeById" resultMap="getEmployeeByIdMap">
SELECT e.id,e.emp_name,e.gender,e.email,e.dept_id
from emp e
where e.id = #{id}
</select>

懒加载机制【按需加载】:

我们只需要在分步查询的基础之上,在mybatis的全局配置文件中加入两个属性:

<!-- 开启懒加载机制 ,默认值为false-->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 开启的话,每个属性都会直接全部加载出来;禁用的话,只会按需加载出来 -->
<setting name="aggressiveLazyLoading" value="false"/>
private ApplicationContext ioc = new ClassPathXmlApplicationContext("bean.xml");
@Test
public void test() {
TblEmployeeMapper bean = ioc.getBean(TblEmployeeMapper.class);
TblEmployee employee = bean.getEmployeeById(1);
System.out.println(employee.getEmpName());
}
此时:可以看到这里只发送了一条SQL语句。
 
懒加载机制,是针对第三种映射的,因为只有第三种映射有两个select语句
 
注意:如果关联的是一个对象,可以使用association标签
 
映射对多的关联关系
查询部门的时候,将部门对应的所有员工信息也查询出来,注释在DepartmentMapper.xml中
 
第一种:
1.修改Department实体类【添加Employee集合,并为该集合提供getter/setter方法】
private List<TblEmployee> list;
public List<TblEmployee> getList() {
return list;
}
public void setList(List<TblEmployee> list) {
this.list = list;
}

建立DepartmentMapper接口文件,并添加如下方法:

public TblDepartment selectDeptAndEmp(Integer id);

2.sql映射文件中的内容为:【collection:嵌套结果集的方式:使用collection标签定义关联的集合类型元素的封装规则】

<resultMap type="com.neuedu.mybatis.entity.TblDepartment" id="selectDeptAndEmpMap">
<id column="id" property="id"/>
<result column="dept_name" property="deptName"/>
<!-- property:Department中的属性名,ofType集合中存放的类型 -->
<collection property="list" ofType="com.neuedu.mybatis.entity.TblEmployee">
<id column="eid" property="id"/>
<result column="emp_name" property="empName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
</collection>
</resultMap>
<select id="selectDeptAndEmp" resultMap="selectDeptAndEmpMap">
select d.id,d.dept_name,e.id eid,e.emp_name,e.gender,e.email,e.dept_id
from dept d
LEFT JOIN emp e
ON d.id = e.dept_id
WHERE d.id = #{id}
</select>

3.测试方法为:

@Test
public void TestselectDeptAndEmp(){
TblDepartmentMapper bean = ioc.getBean(TblDepartmentMapper.class);
TblDepartment dept = bean.selectDeptAndEmp(1);
List<TblEmployee> list = dept.getList();
for (TblEmployee tblEmployee : list) {
System.out.println(tblEmployee);
}
}

第二种:使用分步查询结果集

1.在DepartmentMapper接口文件中添加方法,如下所示:

public TblDepartment selectDeptAndEmp(Integer id);

2.再从EmployeeMapper接口中添加一个方法,如下所示:

public List<TblEmployee> getEmps(Integer deptId);

3.并在响应的sql映射文件中添加相应的sql语句

<select id="getEmps" resultType="com.neuedu.mybatis.entity.TblEmployee">
SELECT e.id,e.emp_name,e.gender,e.email,e.dept_id
FROM emp e
WHERE e.dept_id = #{id}
</select>

4.在DepartmentMapper映射文件中:

<resultMap type="com.neuedu.mybatis.entity.TblDepartment" id="selectDeptAndEmpMap">
<id column="id" property="id"/>
<result column="dept_name" property="deptName"/>
<!-- property:Department中的属性名,ofType集合中存放的类型 -->
<collection property="list" select="com.neuedu.mybatis.mapper.TblEmployeeMapper.getEmps" column="id"/>
</resultMap> <select id="selectDeptAndEmp" resultMap="selectDeptAndEmpMap">
SELECT d.id,d.dept_name
FROM dept d
WHERE d.id = #{id}
</select>

5.测试类:

@Test
public void TestselectDeptAndEmp(){
TblDepartmentMapper bean = ioc.getBean(TblDepartmentMapper.class);
TblDepartment dept = bean.selectDeptAndEmp(1);
List<TblEmployee> list = dept.getList();
for (TblEmployee tblEmployee : list) {
System.out.println(tblEmployee);
}
}
映射(一)对多、(多)对多的关联关系=======》【映射"对多"的关联关系】
1.必须使用collection节点进行映射
2.基本示例:
注意:
1). ofType指定集合中的元素类型
2). collection标签:映射多的一端的关联关系,使用ofType指定集合中的元素类型
     column:传要查找的项,如按 id 查找就传 id
     prefix:指定列的前缀
     使用情境:若关联的数据表和之前的数据表有相同的列名,此时就需要给关联的列其"别名".
     若有多个列需要起别名,可以为所有关联的数据表的列都加上相同的前缀,然后再映射时指定前缀。
 

MyBatis --- 映射关系【一对一、一对多、多对多】,懒加载机制的更多相关文章

  1. Python进阶----表与表之间的关系(一对一,一对多,多对多),增删改查操作

    Python进阶----表与表之间的关系(一对一,一对多,多对多),增删改查操作,单表查询,多表查询 一丶表与表之间的关系 背景: ​ ​ ​  ​ ​ 由于如果只使用一张表存储所有的数据,就会操作数 ...

  2. mybatis入门截图四(订单商品数据模型-懒加载-缓存)

    <!-- 延迟加载的resultMap --> <resultMap type="cn.itcast.mybatis.po.Orders" id="Or ...

  3. hibernate 多对多 懒加载问题

    报错:org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: net. ...

  4. Nhibernate 映射关系,一对多 多对一与多对手在映射文件中的体现。

    今天做了第一个Nhibernate项目,摸着石头过河,学到了一些东西,在这里将自己总结体会到的一些映射关系写出来,与大家分享,由于是初学者,如果有不对的地方希望大家能够指出来. 首先要说明要建立的几张 ...

  5. MyBatis的关联关系 一对一 一对多 多对多

    一对一示例 一个妻子对应一个丈夫 数据库表设计时 在妻子表中添加一个丈夫主键的作为外键 1 对应的JavaBean代码虽然在数据库里只有一方配置的外键,但是这个一对一是双向的关系. Husband实体 ...

  6. JPA 一对一 一对多 多对一 多对多配置

    1 JPA概述 1.1 JPA是什么 JPA (Java Persistence API) Java持久化API.是一套Sun公司 Java官方制定的ORM 方案,是规范,是标准 ,sun公司自己并没 ...

  7. 一步步学习NHibernate(4)——多对一,一对多,懒加载(1)

    请注明转载地址:http://www.cnblogs.com/arhat 通过上一章的学习,我们学会如何使用NHibernate对数据的简单查询,删除,更新和插入,那么如果说仅仅是这样的话,那么NHi ...

  8. MyBatis 懒加载

    懒加载的概念 MyBatis中的延迟加载,也称为懒加载,是指进行关联查询时,按需执行子查询. 当程序需要获取|使用关联对象时,mybatis再执行子查询,这样可以减轻数据库的压力,在一定程度上可以降低 ...

  9. MyBatis加强(1)~myBatis对象关系映射(多对一关系、一对多关系)、延迟/懒加载

    一.myBatis对象关系映射(多对一关系.一对多关系) 1.多对一关系: ---例子:多个员工同属于一个部门. (1)myBatis发送 额外SQL: ■ 案例:员工表通过 dept_id 关联 部 ...

随机推荐

  1. 《Java编程思想》——初始化与清理(一)读书笔记

    第一次写这个,这一章都用word写的,结果复制过来没图片....只能上传word文档了.以后改用markdown比较好 word文档地址:<Java编程思想>--初始化与清理(一)读书笔记

  2. feof() 函数判断不准确的问题

    大家在读文件时应该碰到过这样的问题,while(!feof(fp)) 函数在读文件时会多循环一次,导致 fscanf() 函数多读了一次文件. 所以也就在输出的时候会产生一些乱码. 可以看看下面的代码 ...

  3. (未完)XSS漏洞实战靶场笔记

    记录下自己写的XSS靶场的write up,也是学习了常见xss漏洞类型的实战场景

  4. PHP array_replace

    1.函数的作用:替换数组的元素 2.函数的参数: @params array $array1  被替换的数组 @params array $array2  替换的数组 @params array $a ...

  5. Beanutils.copyProperties( )使用详情总结

    Beanutils.copyProperties( ) 一.简介: ​ BeanUtils提供对Java反射和自省API的包装.其主要目的是利用反射机制对JavaBean的属性进行处理.我们知道,一个 ...

  6. python 2.x中的中文

    先不管一大堆的中文显示的原理,在这里记录下正确显示中文的方式,便于以后的查阅和深入学习. 方法1 a = {} a["哈哈哈"] = "啦啦啦啦啦啦啦" s1 ...

  7. epoll(2) 使用及源码分析的引子

    epoll(2) 使用及源码分析的引子 本文代码取自内核版本 4.17 epoll(2) - I/O 事件通知设施. epoll 是内核在2.6版本后实现的,是对 select(2)/poll(2) ...

  8. uni-app swiper设置自定义高度

    话不多少先上图, 大家可以看到图片中红色区域是头部区域,黄色区域则是我们要滑动的区域. 大家可以在uni-app官网上看到swiper高度是默认100%,而swiper-item则是要有固定宽高的,要 ...

  9. POJO和JavaBean

    1.POJO POJO(Plain Ordinary Java Object):POJO就是一个简单的普通的Java对象,它不包含业务逻辑或持久逻辑等,但不是JavaBean.EntityBean等, ...

  10. javascript严格模式的影响

    针对js严格模式会报错的编码操作进行了整理,避免不严谨的代码习惯,面向未来编程(es6+) 变量 1.定义八进制格式的数字 2.不带var的变量定义--包括函数体内的 3.对变量的delete操作-- ...