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

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)

    数据结构1.什么是数据结构?数据结构有哪些? 数据结构是指数据在内存中存放的机制. 不同的数据结构在数据的查询,增删该的情况下性能是不一样的. 数据结构是可以模拟业务场景. 常见的数据结构有:栈,队列 ...

  2. Maven下载速度过慢问题已解决

    因为Maven 默认仓库的服务器在国外所以我们国内的使用效果极差,我们可以修改成为国内镜像地址加速下载. 两种方法 修改全局文件 C:\Users\您电脑帐号\ .m2\settings.xml没有文 ...

  3. netty源码解解析(4.0)-25 ByteBuf内存池:PoolArena-PoolChunk

    PoolArena实现了用于高效分配和释放内存,并尽可能减少内存碎片的内存池,这个内存管理实现使用PageRun/PoolSubpage算法.分析代码之前,先熟悉一些重要的概念: page: 页,一个 ...

  4. [Luogu2422]良好的感觉

    题目描述 kkk做了一个人体感觉分析器.每一天,人都有一个感受值Ai,Ai越大,表示人感觉越舒适.在一段时间[i, j]内,人的舒适程度定义为[i, j]中最不舒服的那一天的感受值 * [i, j]中 ...

  5. Flink整合oozie shell Action 提交任务 带kerberos认证

    最近这段时间一直在忙新集群迁移,上了最新的cdh6.3.0 于是Flink 提交遇到了许多的问题 还好有cloudera License 有了原厂的帮助和社区的伙伴,问题解决起来快了不少,手动滑稽 集 ...

  6. SpringBoot中神奇的@Enable*注解?

    在SpringBoot开发过程,我们经常会遇到@Enable开始的好多注解,比如@EnableEurekaServer.@EnableAsync.@EnableScheduling等,今天我们就来分析 ...

  7. 怎样快速找到某一行代码的git提交记录

    利用notepad++提高问题分析效率,以及快速找到某一行代码的git提交记录 1. 全目录搜索/替换 Notepad++是一款强大的文本编辑工具,当知道大概的关键词但不知道在哪个日志时可以使用not ...

  8. 微信小程序——获取openGid

    小编使用的Node版本的解密方式(其他方式自行替换写法也是 一样的) 1.到微信小程序官网下载解密demo包 2.获取用户[openid] 3.调用 [wx.showShareMenu] 并且设置 w ...

  9. Redis真集群安装

    Redis真集群安装 命令文档:http://redisdoc.com/index.html 下载:https://code.google.com/archive/p/redis/downloads ...

  10. js数据交互——fetch

    什么是fetch? Fetch被称为下一代Ajax技术,采用Promise方式来处理数据. 是一种简洁明了的API,比XMLHttpRequest更加简单易用.fetch是原生的(无需引入任何库和框架 ...