技术员联盟提供win764位系统下载,win10,win7,xp,装机纯净版,64位旗舰版,绿色软件,免费软件下载基地!

当前位置:主页 > 教程 > 服务器类 >

MyBatis查询缓存实例教程

来源:技术员联盟┆发布时间:2017-07-15 00:04┆点击:

查询缓存的使用,主要是为了提高查询访问速度。将用户对同一数据的重复查询过程简化,不再每次均从数据库查询获取结果数据,从而提高访问速度。

MyBatis的查询缓存机制,根据缓存区的作用域(生命周期)可划分为两种:一级缓存与二级缓存

一、一级查询缓存

MyBatis一级缓存是基于org.apache.ibatis.cache.impl.PerpetualCache类的HashMap本地缓存,其作用域是Sqlsession。在同一个Sqlsession中两次执行相同的sql语句,第一次执行完毕后,会将查询结果写入到缓存中,第二次会从缓存中直接获取数据,而不再到数据库中进行查询,从而提高查询效率。

当一个Sqlsession结束后,该Sqlsession中的一级缓存也就不存在了。MyBatis默认一级缓存是开启状态,且不能关闭。

MyBatis查询缓存实例教程 三联

1.一级缓存的存在性证明

测试类:

//证明一级缓存的存在 @Test public void test01(){ //第一次查询 Student student = dao.selectStudentById(2); System.out.println(student); //第二次查询 Student student2 = dao.selectStudentById(2); System.out.println(student2); }

mapper:

<mapper namespace="com.hcx.dao.IStudentDao"> <select id=selectStudentById resultType="com.hcx.beans.Student"> select * from student where id=#{id} </select> </mapper>

控制台:

执行完后,发现只执行了一次从DB中的查询,第二次的结果是直接输出的。说明,第二次是从Sqlsession缓存中读取的。

MyBatis查询缓存实例教程

2.从缓存读取数据的依据是sql的id

一级缓存缓存的是相同sql映射id的查询结果,而非相同sql语句的查询结果。因为MyBatis内部对于查询缓存,无论是一级查询还是二级查询,其底层均使用一个hashmap实现:key为sql的id相关内容,value为从数据库中查询出的结果。

mapper:

<mapper namespace="com.hcx.dao.IStudentDao"> <select id=selectStudentById resultType="com.hcx.beans.Student"> select * from student where id=#{id} </select> <select id="selectStudnetById2" resultType="com.hcx.beans.Student"> select id,name,age,score,birthday from student where id=#{id} </select> </mapper>

dao接口:

public interface IStudentDao { Student selectStudentById(int id); Student selectStudentById2(int id); }

测试类:

//证明从一级缓存中读取数据的依据: //MyBatis:sql的id+sql语句 //hibernate:查询结果对象的id @Test public void test02(){ Student student = dao.selectStudentById(2); System.out.println(student); Student student2 = dao.selectStudentById2(2); System.out.println(student2); }

控制台:

查看控制台,发现第二次查询结果与第一次的完全相同,但第二次查询并没有从缓存中读取数据,而是直接从DB中进行的查询。这是因为从缓存读取数据的依据是查询sql的映射id,而非查询结果。

MyBatis查询缓存实例教程

3.增删改对一级查询缓存的影响

增删改操作,无论是否进行提交Sqlsession.commit(),均会清空一级查询缓存,使查询再次从DB中select。

测试类:

@Test public void test03(){ Student student = dao.selectStudentById(2); System.out.println(student); //增删改操作都会清空一级缓存,无论是否提交 dao.insertStudent(new Student("赵六",26,96.6)); Student student2 = dao.selectStudentById(2); System.out.println(student2); }

控制台:

MyBatis查询缓存实例教程

二、内置二级查询缓存

MyBatis查询缓存的作用域是根据映射文件mapper的namespace划分的,相同namespace的mapper查询数据存放在同一个缓存区域。不同namespace下的数据互不干扰。

无论是一级缓存还是二级缓存,都是按照namespace进行分别存放的。但一、二级缓存的不同之处在于,Sqlsession一旦关闭,则Sqlsession中的数据将不存在,即一级缓存就不复存在。而二级缓存的生命周期会与整个应用同步,与Sqlsession是否关闭无关。

使用二级缓存的目的,不是共享数据,因为MyBatis从缓存中读取数据的依据是sql的id,而非查询出的对象。所以,二级缓存中的数据不是为了在多个查询之间共享(所有查询中只要查询结果中存在该对象的,就直接从缓存中读取,这是对数据的共享,hibernate中的缓存就是为了共享,但MyBatis不是),而是为了延长该查询结果的保存时间,提高系统性能。

1.二级缓存用法

二级缓存的使用只需要完成两步:

序列化实体

在mapper映射文件中添加<cache/>标签

1.实体序列化

要求查询结果所涉及到的实体类要实现java.io.Serializable接口。若该实体类存在父类,或其具有域属性,则父类与域属性类也要实现序列化接口。

public class Student implements Serializable{ private Integer id; private String name; private int age; private double score; }

2.mapper映射文件中添加<cache/>标签

在mapper映射文件中的<mapper/>标签中添加<cache/>子标签