MyBatis --- 映射关系【一对一、一对多、多对多】,懒加载机制
映射(多、一)对一的关联关系
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>
<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());
}
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);
}
}
MyBatis --- 映射关系【一对一、一对多、多对多】,懒加载机制的更多相关文章
- Python进阶----表与表之间的关系(一对一,一对多,多对多),增删改查操作
Python进阶----表与表之间的关系(一对一,一对多,多对多),增删改查操作,单表查询,多表查询 一丶表与表之间的关系 背景: 由于如果只使用一张表存储所有的数据,就会操作数 ...
- mybatis入门截图四(订单商品数据模型-懒加载-缓存)
<!-- 延迟加载的resultMap --> <resultMap type="cn.itcast.mybatis.po.Orders" id="Or ...
- hibernate 多对多 懒加载问题
报错:org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: net. ...
- Nhibernate 映射关系,一对多 多对一与多对手在映射文件中的体现。
今天做了第一个Nhibernate项目,摸着石头过河,学到了一些东西,在这里将自己总结体会到的一些映射关系写出来,与大家分享,由于是初学者,如果有不对的地方希望大家能够指出来. 首先要说明要建立的几张 ...
- MyBatis的关联关系 一对一 一对多 多对多
一对一示例 一个妻子对应一个丈夫 数据库表设计时 在妻子表中添加一个丈夫主键的作为外键 1 对应的JavaBean代码虽然在数据库里只有一方配置的外键,但是这个一对一是双向的关系. Husband实体 ...
- JPA 一对一 一对多 多对一 多对多配置
1 JPA概述 1.1 JPA是什么 JPA (Java Persistence API) Java持久化API.是一套Sun公司 Java官方制定的ORM 方案,是规范,是标准 ,sun公司自己并没 ...
- 一步步学习NHibernate(4)——多对一,一对多,懒加载(1)
请注明转载地址:http://www.cnblogs.com/arhat 通过上一章的学习,我们学会如何使用NHibernate对数据的简单查询,删除,更新和插入,那么如果说仅仅是这样的话,那么NHi ...
- MyBatis 懒加载
懒加载的概念 MyBatis中的延迟加载,也称为懒加载,是指进行关联查询时,按需执行子查询. 当程序需要获取|使用关联对象时,mybatis再执行子查询,这样可以减轻数据库的压力,在一定程度上可以降低 ...
- MyBatis加强(1)~myBatis对象关系映射(多对一关系、一对多关系)、延迟/懒加载
一.myBatis对象关系映射(多对一关系.一对多关系) 1.多对一关系: ---例子:多个员工同属于一个部门. (1)myBatis发送 额外SQL: ■ 案例:员工表通过 dept_id 关联 部 ...
随机推荐
- linux 防火墙基本使用
写在最前面 由于工作后,使用的Linux就是centos7 所以,本文记录是是centos7的防火墙使用. 从 centos7 开始,系统使用 firewall 进行防火墙的默认管理工具. 基本使用 ...
- HikariCP重要参数配置
概述 HikariCP是Spring Framework 5.0的默认数据库连接池,这得益于他的高性能.但是如果配置不当,数据库连接池也可能因影响到系统性能. 重要参数 maximum-pool-si ...
- PTA 7-3 编辑距离问题 (30 分)
一.实践题目 设A和B是2个字符串.要用最少的字符操作将字符串A转换为字符串B.这里所说的字符操作包括: ()删除一个字符: ()插入一个字符: ()将一个字符改为另一个字符. 将字符串A变换为字符串 ...
- redis安装及启动
Redis 的安装及启动停止 下载 https://redis.io/download 软件copy至虚拟机中,常用的路径为/root/software 开始安装 安装gcc 目的是为了编译软件 yu ...
- Java零基础手把手系列:HashMap排序方法一网打尽
HashMap的排序在一开始学习Java的时候,比较容易晕,今天总结了一些常见的方法,一网打尽.HashMap的排序入门,看这篇文章就够了. 1. 概述 本文排序HashMap的键(key)和值(va ...
- Python3程序设计指南:02 数据类型
目录 1.标识符与关键字 1.1 规则 1.2 约定 2.Integral类型 2.1 整数 2.1.1 数值型操作符与函数 2.1.2 使用数据类型创建对象 2.1.3 整数位逻辑操作符 2.2 布 ...
- Meterpreter后渗透之信息收集
在获得目标的Meterpreter shell后 进行信息收集是后渗透工作的基础 记录一下关于meterpreter 信息收集的使用 环境: kali linux 192.168.190.141 xp ...
- 非阻塞IO模型
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h> # ...
- 玩转 RTC时钟库 DS1302
1.前言 最近博主在弄8266编程的时候,偶然发现两个全新时钟模块压仓货: DS1302 DS3231 为了避免资源浪费以及重复编写代码,博主还是抱着尝试的心态去寻找能够同时兼容 DS ...
- 百万年薪python之路 -- 字典(dict)
1.字典(dict)-- dict关键字 字典(dict)是python中唯⼀的⼀个映射类型.他是以{ }括起来的键值对组成. 字典中逗号分隔叫作一个元素 字典是无序的 key必须是不可变 ...