MyBatis高级篇之整合ehcache缓存框架

时间:2023-08-27 11:48:50

MyBatis高级篇之整合ehcache缓存框架

 2017-09-01  0 Comments  1,671 Views  0 Times

一、前言

MyBatis为我们提供了Cache接口,也提供了一些实现类,进入Cache接口源码,可以看到缓存对于MyBatis来说就是一个Map,比较简陋。但是大家都知道MyBatis是一个专注于持久层框架,与数据库打交道MyBatis是很专业的,但是对于缓存它就略显不足,即便如此MyBatis却提供了Cache接口的标准规范,谁做缓存专业,谁就来实现它,提供了很好的扩展性。本节我们将介绍了一下MyBatis与Ehcache框架的整合。

二、准备

Eclipse版本:Luna Service Release 1 (4.4.1)
MyBatis版本:org.mybatis.mybatis-3.2.8
JDK版本:1.7.0_79
Ehcache版本:ehcache-core-2.6.11.jar
mybatis-ehcache整合版本:mybatis-ehcache-1.1.0.jar

三、案例

♠参照<<MyBatis基础篇之简单入门>>搭建Maven工程MyBatisIntegrateEhcache

♠搭建完工程后,需要往工程里面引入ehcache相关的jar包

引入核心包

<!-- ehcache核心jar包 -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.11</version>
</dependency>

引入mybatis-ehcache整合包,如果不知道怎么引入,可以参考官方文档,如下图:

MyBatis高级篇之整合ehcache缓存框架

官方地址:http://www.mybatis.org/ehcache-cache/ 有兴趣可以看一下。

♠完整的Pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.queen.mybatis</groupId>
<artifactId>MyBatisIntegrateEhcache</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
<!-- ehcache核心jar包 -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.11</version>
</dependency>
<!-- MyBatis与ehcache整合jar包 -->
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
</project>

♠下面添加配置,都是按照官方文档来配置的

修改EmpMapper.xml文件,添加如下配置

MyBatis高级篇之整合ehcache缓存框架

引入ehcache.xml文件,完整配置说明如下:

<?xml version="1.0" encoding="UTF-8" ?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<!--
java.io.tmpdir - Default temp file path 默认的 temp 文件目录
maxElementsInMemory:内存中最大缓存对象数.
maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大.
eternal:Element是否永久有效,一但设置了,timeout将不起作用.
overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中
timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0, 也就是可闲置时间无穷大
timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,
默认是0.也就是element存活时间无穷大.
diskPersistent:是否缓存虚拟机重启期数据。(这个虚拟机是指什么虚拟机一直没看明白是什么,有高人还希望能指点一二)
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区.
-->
<diskStore path="E:\20170819\ehcache" />
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
maxElementsOnDisk="10000000" diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />
</ehcache>

ehcache.xml放置路径

MyBatis高级篇之整合ehcache缓存框架

如上,就是我们整合ehcache所需要的步骤,下面我们来简单的测试一下

@Test
public void testEhCache() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try {
EmpMapper mapper = openSession.getMapper(EmpMapper.class);
Emp emp01 = mapper.findEmpById(1);
System.out.println(emp01.toString()); Emp emp02 = mapper.findEmpById(1);
System.out.println(emp02.toString());
System.out.println(emp01 == emp02);
} finally {
openSession.close();
}
}

测试结果如下

2017-08-19 11:27:18,090 [main] [com.queen.mybatis.mapper.EmpMapper.findEmpById]-[DEBUG] ==>  Preparing: select id,emp_name empName,emp_email empEmail, dept_id deptId from t_emp where id = ?
2017-08-19 11:27:18,144 [main] [com.queen.mybatis.mapper.EmpMapper.findEmpById]-[DEBUG] ==> Parameters: 1(Integer)
2017-08-19 11:27:18,175 [main] [com.queen.mybatis.mapper.EmpMapper.findEmpById]-[DEBUG] <== Total: 1
Emp [id=1, empName=queen3aasd21, empEmail=queen123@sina.com, deptId=1]
2017-08-19 11:27:18,177 [main] [com.queen.mybatis.mapper.EmpMapper]-[DEBUG] Cache Hit Ratio [com.queen.mybatis.mapper.EmpMapper]: 0.0
Emp [id=1, empName=queen3aasd21, empEmail=queen123@sina.com, deptId=1]
true
2017-08-19 11:27:18,178 [main] [net.sf.ehcache.store.disk.Segment]-[DEBUG] put added 0 on heap

控制台打印了一段SQL,第二次查询直接从缓存中获取。

从上面的打印结果,我们好像无法判断是否真的用到了ehcache缓存,但可以看一下缓存路径下是否有数据就知道了

MyBatis高级篇之整合ehcache缓存框架

找到该路径,可以确信使用了ehcache缓存

MyBatis高级篇之整合ehcache缓存框架

四、总结

MyBatis与Ehcache整合步骤总结如下:

  • 引入第三方缓存包
  • 导入与第三方缓存整合的适配包
  • 在mapper.xml映射文件中引入<cache type=”org.mybatis.caches.ehcache.EhcacheCache”/>即可
  • 引入ehcache.xml文件,设置相关属性配置

至此,我们关于MyBatis高级篇之整合ehcache缓存框架介绍完毕。

博客地址:http://www.marsitman.com/mybatis/mybatis-ehcache.html
版权声明:本文为博主原创文章,允许转载,但转载必须标明出处。