发布于 2017-09-08 00:23:25 | 170 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的MyBatis教程,程序狗速度看过来!

MyBatis 基于Java的持久层框架

MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。


查询缓存的使用,主要是为了提高查询访问速度。这篇文章主要介绍了MyBatis查询缓存,需要的朋友可以参考下

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

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

一、一级查询缓存

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

当一个Sqlsession结束后,该Sqlsession中的一级缓存也就不存在了。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缓存中读取的。

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,而非查询结果。

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查询缓存的作用域是根据映射文件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/>子标签


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

3.二级缓存的配置

为<cache/>标签添加一些相关属性设置,可以对二级缓存的运行性能进行控制。若不指定设置,则均保持默认值。


<cache eviction="IFIO" flushInterval="10800000"
  readOnly="true" size="512"/>

eviction:逐出策略。当二级缓存中的对象达到最大值时,就需要通过逐出策略将缓存中的对象移出缓存。默认为LRU。常用的策略有FIFO和LRU

flushInterval:刷新缓存的时间间隔,单位毫秒。这里的刷新缓存即清空缓存。一般不指定,即当执行增删改时刷新缓存。

readOnly:设置缓存中数据是否只读。只读的缓存会给所有调用者返回缓存对象的相同实例,因此这些对象不能被修改,这提供了很重要的性能优势。但读写的缓存会返回缓存对象的拷贝。这会慢一些,但是安全,因此默认是false。
size:二级缓存中可以存放的最多对象个数。默认为1024个。

2.二级缓存的存在性证明

对于映射文件中的同一个查询,肯定是同一个namespace中的查询。在一次查询后,将Sqlsession关闭,再进行一次相同查询,发现并没有到DB中进行select查询,说明二级缓存是存在的。


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

查看控制台:

Cache Hit Ratio表示缓存命中率。开启二级缓存后,每执行一次查询,系统都会计算一次二级缓存的命中率。第一次查询也是先从缓存中查询,只不过缓存中一定是没有的。所以会再从DB中查询。由于二级缓存中不存在该数据,所以命中率为0.但第二次查询是从二级缓存中读取的,所以这一次的命中率为1/2=0.5。当然,若有第三次查询,则命中率为1/3=0.66

3.增删改对二级缓存的影响

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

测试类:


@Testpublic void test02(){
 //第一次查询
 Student student = dao.selectStudentById(2);
 System.out.println(student);
 sqlSession.close();
 sqlSession = MyBatisUtils.getSqlSession();
 dao = sqlSession.getMapper(IStudentDao.class);
 //插入
 dao.insertStudent(new Student("",0,0));
 //第二次查询
 Student student2 = dao.selectStudentById(2);
 System.out.println(student2);  
}

控制台:

注意,在第二次查询时的缓存命中率为0.5,但还是从DB中查询了。说明在缓存中与该查询相对应的key是存在的,但其value被清空。而value被清空的原因是前面执行了对DB的增删改操作,所以不会从缓存中直接将null值返回,而是从DB中进行查询。

说明:

二级缓存的清空,实质上是对所查找key对应的value置为null,而非将<key,value>对,即entry对象删除。

从DB中进行select查询的条件是:缓存中根本不存在这个key或者缓存中存在该key所对应的entry对象,但value为null。

设置增删改操作不刷新二级缓存:

若要使某个增、删或改操作不清空二级缓存,则需要在其<insert/>或<delete/>或<update/>中添加属性flushCache="false",默认为true。


<insert id="insertStudent" flushCache="false">
  insert into student(name,age,score) values(#{name},#{age},#{score})
 </insert>

4.二级缓存的关闭

二级缓存默认为开启状态。若要将其关闭,则需要进行相关设置。
根据关闭的范围大小,可以分为全局关闭和局部关闭

1.全局关闭(在配置文件中设置)

全局关闭是指整个应用的二级缓存全部关闭,所有查询均不使用二级缓存。全局开关设置在主配置文件的全局设置<settings/>中,该属性为cacheEnabled,设置为false,则关闭;设置为true,则开启,默认值为true。即二级缓存默认是开启的。


<!-- 关闭二级缓存 -->
<settings>
 <setting name="cacheEnabled" value="false"/>
</settings>

2.局部关闭(在映射文件的每个select中设置)

局部关闭指整个应用的二级缓存是开启的,但只是针对某个<select/>查询,不使用二级缓存。此时可以单独只关闭该<select/>标签的二级缓存。

在该要关闭二级缓存的<select/>标签中,将其属性useCache设置为false,即可关闭该查询的二级缓存。该属性默认为true,即每个<select/>查询的二级缓存默认是开启的。


<!--useCache="false"对当前sql的二级缓存的局部关闭 -->
 <select id=selectStudentById useCache="false" resultType="com.hcx.beans.Student">
  select * from student where id=#{id}
 </select>

5.二级缓存的使用原则

1.只能在一个命名空间下使用二级缓存

由于二级缓存中的数据是基于namespace的,即不同namespace中的数据互不干扰。在多个namespace中若均存在对同一个表的操作,那么这多个namespace中的数据可能就会出现不一致现象。

2.在单表上使用二级缓存

如果一个表与其它表有关联关系,那么久非常有可能存在多个namespace对同一数据的操作。而不同namespace中的数据互补干扰,所以就有可能出现多个namespace中的数据不一致现象。

3.查询多于修改时使用二级缓存

在查询操作远远多于增删改操作的情况下可以使用二级缓存。因为任何增删改操作都将刷新二级缓存,对二级缓存的频繁刷新将降低系统性能。

三、ehcache二级查询缓存

MyBatis允许使用第三方缓存产品。ehCache就是其中一种。

注意:使用ehcache二级缓存,实体类无需实现序列化接口。

1.导入jar包

2.添加ehcache.xml

解压ehcache的核心jar包ehcache-core-2.6.8.jar,将其中的一个配置文件ehcache-failsafe.xml直接放到项目的src目录下,并更名为ehcache.xml

(1)<diskStore/>标签

指定一个文件目录,当内存空间不够,需要将二级缓存中数据写到硬盘上时,会写到这个指定目录中。其值一般为java.io.tmpdir,表示当前系统的默认文件临时目录。

当前文件系统的默认文件临时目录,可以通过System.property()方法查看:


@Test
public void test(){
 String path = System.getProperty("java.io.tmpdir");
 System.out.println(path);
}

(2)<defaultCache/>标签

3.启用ehcache缓存机制

在映射文件的mapper中的<cache/>中通过type指定缓存机制为Ehcache缓存。默认为MyBatis内置的二级缓存org.apache.ibatis.cache.impl.PerpetualCache。


<mapper namespace="com.hcx.dao.IStudentDao">
 <cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
  <select id=selectStudentById resultType="com.hcx.beans.Student">
   select * from student where id=#{id}
  </select>
</mapper>

4.ehcache在不同mapper中的个性化设置

在ehcache.xml中设置的属性值,会对该项目中所有使用ehcache缓存机制的缓存区域起作用。一个项目中可以有多个mapper,不同的mapper有不同的缓存区域。对于不同缓存区域也可进行专门针对于当前区域的个性设置,可通过指定不同mapper的<cache>属性值来设置。

<cache>属性值的优先级高于ehcache.xml中的属性值。


<mapper namespace="com.hcx.dao.IStudentDao">
 <cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
   <property name="maxElementsInMemory" value="5000"/>
   <property name="timeToIdleSeconds" value="240"/>
 </cache>
</mapper>

以上所述是小编给大家介绍的MyBatis查询缓存实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对PHPERZ网站的支持!



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务