mybatis教程5(延迟加载和缓存)
关联关系
在关系型数据库中,表与表之间很少是独立与其他表没关系的。所以在实际开发过程中我们会碰到很多复杂的关联关系。在此我们来分析下载mybatis中怎么处理这些关系
1对1关系
我们有一张员工表(T_EMP),一张部门表(T_DEPT)。员工表中的一条记录对应于部门表中有且仅有一条记录。这就是一对一的关联关系。
查询每个员工的信息及对应的部门信息
对象与对象1对1
部门对象:
// 部门编号 private int deptid; // 部门名称 private String dname; // 部门描述 private String desc;
员工对象
private int id; private String name; private int age; // 员工和部门的关联关系是1对1, // 体现在面向对象中就是一个对象中包含有另一个对象 private Department dept;
映射文件中处理
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 使用接口 代理的方式 namespace必须和接口的全路径名称一致 --> <mapper namespace="com.sxt.dao.EmpMapper"> <resultMap type="com.sxt.bean.Emp" id="baseMap"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="age" property="age"/> <association property="dept" javaType="com.sxt.bean.Department"> <id column="deptid" property="deptid"/> <result column="dname" property="dname"/> <result column="desc" property="desc"/> </association> </resultMap> <select id="query" resultMap="baseMap"> select t1.id id ,t1.name name ,t1.age age ,t2.deptid deptid ,t2.dname dname ,t2.desc from t_emp t1 left join t_dept t2 on t1.deptid = t2.deptid </select> </mapper>
1对多关系
查询出所有的部门信息及该部门下所有员工的信息
对象与对象1对多
// 部门编号 private int deptid; // 部门名称 private String dname; // 部门描述 private String desc; // 1对多 1个部门对象包含多个员工对象 private List<Emp> emps;
映射文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 使用接口 代理的方式 namespace必须和接口的全路径名称一致 --> <mapper namespace="com.sxt.dao.DeptMapper"> <resultMap type="com.sxt.bean.Department" id="baseMap"> <id column="deptid" property="deptid" /> <result column="dname" property="dname" /> <result column="desc" property="desc" /> <!-- ofType List中泛型的类型 property为变量的名称 --> <collection property="emps" ofType="emp"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="age" property="age" /> </collection> </resultMap> <select id="query" resultMap="baseMap"> select t1.deptid ,t1.dname ,t1.desc ,t2.name ,t2.age ,t2.id from t_dept t1 left join t_emp t2 on t1.deptid = t2.deptid </select> </mapper>
查询结果
多对多关系
==双向的1对多既是多对多关系==
延迟加载
延迟查询是一对一和一对多查询的延续。
在默认的一对一和一对多中,一条SQL就能够查询到所有数据,但是,有的数据有时候一时半会用不上,例如查询员工,捎带获取员工的部门数据,但是部门数据使用的频率很低,这种时候可以使用延迟查询,首先获取到所有的员工数据,然后在需要的时候再去获取部门数据。==当需要使用数据的时候才去加载==既是延迟加载
开启延迟加载
全局配置文件中配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="db.properties"></properties> <settings> <!-- 开启延迟加载 --> <setting name="lazyLoadingEnabled" value="true"/> <setting name="aggressiveLazyLoading" value="false"/> </settings> <typeAliases> <!-- <typeAlias type="com.sxt.bean.User" alias="user"/> --> <package name="com.sxt.bean"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <!-- 注册映射文件 --> <mappers> <!-- <mapper resource="com/sxt/dao/UserMapper.xml"/> --> <package name="com.sxt.dao"/> </mappers> </configuration>
1对1
查询的SQL语句就不能通过一个SQL给查询出来了,得分成多个SQL
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 使用接口 代理的方式 namespace必须和接口的全路径名称一致 --> <mapper namespace="com.sxt.dao.EmpMapper"> <resultMap type="com.sxt.bean.Emp" id="baseMap"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="age" property="age"/> <association property="dept" javaType="com.sxt.bean.Department" column="deptid" select="queryDeptById"> <id column="deptid" property="deptid"/> <result column="dname" property="dname"/> <result column="desc" property="desc"/> </association> </resultMap> <!-- 需要延迟加载的数据 --> <select id="queryDeptById" parameterType="int" resultType="department"> select * from t_dept where deptid = #{deptid} </select> <!-- 查询主表数据 --> <select id="query" resultMap="baseMap"> select * from t_emp </select> </mapper>
结果:
1对多
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 使用接口 代理的方式 namespace必须和接口的全路径名称一致 --> <mapper namespace="com.sxt.dao.DeptMapper"> <resultMap type="com.sxt.bean.Department" id="baseMap"> <id column="deptid" property="deptid" /> <result column="dname" property="dname" /> <result column="desc" property="desc" /> <!-- ofType List中泛型的类型 property为变量的名称 --> <collection property="emps" ofType="emp" column="deptid" select="queryEmpByDid"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="age" property="age" /> </collection> </resultMap> <select id="queryEmpByDid" resultType="emp" parameterType="int"> select * from t_emp where deptid=#{deptid} </select> <select id="query" resultMap="baseMap"> select t1.deptid ,t1.dname ,t1.desc from t_dept t1 </select> </mapper>
缓存
## 缓存简介:
缓存(Cache )是计算机领域非常通用的概念。它介于应用程序和永久性数据存储源(如硬盘上的文件或者数据库)之间,其作用是==降低==应用程序直接读写永久性数据存储源的==频率==,从而提高应用的运行性能。缓存中的数据是数据存储源中数据的拷贝,应用程序在运行时直接读写缓存中的数据,只在某些特定时刻按照缓存中的数据来同步更新数据存储源。
缓存的物理介质通常是内存,而永久性数据存储源的物理介质通常是硬盘或磁盘,应用程序读写内在的速度显然比读写硬盘的速度快,如果缓存中存放的数据量非常大,也会用硬盘作为缓存的物理介质。
缓存的实现不仅需要作为物理介质的硬件,同时还需要用于管理缓存的并发访问和过期等策略的软件。因此,缓存是通过软件和硬件共同实现的
作用:降低访问数据源【数据库】频率
缓存分类
类别 | 说明 |
---|---|
一级缓存 | 事务范围:缓存只能被当前事务访问。缓存的生命周期 依赖于事务的生命周期当事务结束时,缓存也就结束生命周期。 在此范围下,缓存的介质是内存。 |
二级缓存 | 进程范围:缓存被进程内的所有事务共享。这些事务有 可能是并发访问缓存,因此必须对缓存采取必要的事务隔离机制。 缓存的生命周期依赖于进程的生命周期,进程结束时, 缓存也就结束了生命周期。进程范围的缓存可能会存放大量的数据, 所以存放的介质可以是内存或硬盘。 |
三级缓存 | 集群范围:在集群环境中,缓存被一个机器或者多个机器的进程共享。 缓存中的数据被复制到集群环境中的每个进程节点, 进程间通过远程通信来保证缓存中的数据的一致性, 缓存中的数据通常采用对象的松散数据形式 |
MyBatis支持1级缓存和2级缓存,在实际开发中,实际上很少使用到MyBatis自带的缓存,大部分情况下,缓存都是使用EHCache,MemoryCache、Redis等等来实现缓存。
一级缓存
MyBatis中1级缓存是基于SqlSession的
@Test public void query2() throws IOException { SqlSession session = DbUtils.getInstace().openSession(); //通过Java动态代理自动提供了UserMapper的实现类 EmpMapper mapper = session.getMapper(EmpMapper.class); // 第一次查询,从数据库中查询的数据会保存在一级缓存中 // key: List<Emp> list = mapper.query(); for (Emp emp : list) { System.out.println(emp.getId()+","+emp.getName()); } System.out.println("----------"); // 先查看缓存中是否有该数据,如果没有就从数据库中查询 Emp emp = mapper.queryById(1); System.out.println(emp.getId()+","+emp.getName()); System.out.println("----------"); // 之前查询的有,直接从缓存中获取 emp = mapper.queryById(1); System.out.println(emp.getId()+","+emp.getName()); session.close(); }
跟踪源码发现,缓存的key是由id 属性及sql语句组成的,所以第一个查询和第二个查询不同,没有使用缓存。
二级缓存
二级缓存基于SqlSessionFactory
一级缓存的作用域只是SqlSession,范围比较下,用到的不多。
二级缓存是进程级别的缓存,用的比较普遍,二级缓存mybatis本身没有提供,常用的主键有Ehcache和Redis,本文主要讲解Ehcache
使用步骤:
导包
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>1.5.0</version> </dependency> <dependency> <groupId>org.mybatis.caches</groupId> <artifactId>mybatis-ehcache</artifactId> <version>1.1.0</version> </dependency>
开启二级缓存
<cache type="org.mybatis.caches.ehcache.EhcacheCache"></cache>
测试
- MyBatis 系列五 之 延迟加载、一级缓存、二级缓存设置
- mybatis的延迟加载,一级缓存,二级缓存
- MyBatis 缓存、延迟加载
- Mybatis(五) 延迟加载和缓存机制(一级二级缓存)
- Mybatis(3、延迟加载、查询缓存、与ehcache整合、逆向工程、与spring整合)
- mybatis的延迟加载和一、二级缓存
- MyBatis 延迟加载,一级缓存,二级缓存设置
- mybatis 配置延迟加载 和 缓存
- mybatis延迟加载和缓存
- MyBatis 延迟加载,一级缓存,二级缓存设置
- MyBatis 延迟加载,一级缓存,二级缓存设置
- MyBatis 延迟加载,一级缓存(sqlsession级别)、二级缓存(mapper级别)设置
- mybatis(八) mybatis查询缓存 延迟加载 一级缓存和二级缓存原理 mybatis整合ehcache
- MyBatis 延迟加载,一级缓存,二级缓存设置
- mybatis的一对一,一对多查询,延迟加载,缓存介绍
- Mybatis步步进阶(八)——Mybatis延迟加载和缓存机制
- MyBatis 延迟加载、一级缓存、二级缓存(详解)
- MyBatis 延迟加载,一级缓存,二级缓存设置
- 深入浅出Mybatis(九)延迟加载和缓存
- mybatis的延迟加载、一级缓存、二级缓存