MyBatis高级篇之整合ehcache缓存框架
MyBatis高级篇之整合ehcache缓存框架
一、前言
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整合包,如果不知道怎么引入,可以参考官方文档,如下图:

官方地址: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文件,添加如下配置

引入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放置路径

如上,就是我们整合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缓存,但可以看一下缓存路径下是否有数据就知道了

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

四、总结
MyBatis与Ehcache整合步骤总结如下:
- 引入第三方缓存包
- 导入与第三方缓存整合的适配包
- 在mapper.xml映射文件中引入<cache type=”org.mybatis.caches.ehcache.EhcacheCache”/>即可
- 引入ehcache.xml文件,设置相关属性配置
至此,我们关于MyBatis高级篇之整合ehcache缓存框架介绍完毕。
博客地址:http://www.marsitman.com/mybatis/mybatis-ehcache.html
版权声明:本文为博主原创文章,允许转载,但转载必须标明出处。
MyBatis高级篇之整合ehcache缓存框架的更多相关文章
- [原创]mybatis中整合ehcache缓存框架的使用
mybatis整合ehcache缓存框架的使用 mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓 ...
- mybatis中整合ehcache缓存框架的使用
mybatis整合ehcache缓存框架的使用 mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓 ...
- Spring MVC学习总结(7)——Spring MVC整合Ehcache缓存框架
Ehcache算是当前比较流行的缓存框架,使用缓存可以极大的缓解服务器和数据库的压力,提高访问效率,提高服务器的并发能力.接下来我们看怎么把缓存使用起来. SpringMVC集成Ehcache所需的j ...
- mybatis0210 mybatis和ehcache缓存框架整合
.1mybatis和ehcache缓存框架整合 一般不用mybatis来管理缓存而是用其他缓存框架在管理缓存,因为其他缓存框架管理缓存会更加高效,因为别人专业做缓存的而mybatis专业做sql语句的 ...
- (十一)mybatis之整合ehcache缓存
一.二级缓存 大家都知道使用mybatis就要先获取sqlsessionfactory,继而使用sqlsession来和数据库交互,每次只需要使用sqlsession对象提供的方法就好,当我们需要第一 ...
- 项目一:第十四天 1.在realm中动态授权 2.Shiro整合ehcache 缓存realm中授权信息 3.动态展示菜单数据 4.Quartz定时任务调度框架—Spring整合javamail发送邮件 5.基于poi实现分区导出
1 Shiro整合ehCache缓存授权信息 当需要进行权限校验时候:四种方式url拦截.注解.页面标签.代码级别,当需要验证权限会调用realm中的授权方法 Shiro框架内部整合好缓存管理器, ...
- C#开发微信门户及应用(48) - 在微信框架中整合CacheManager 缓存框架
在我们的很多框架或者项目应用中,缓存在一定程度上可以提高程序的响应速度,以及减轻服务器的承载压力,因此在一些地方我们都考虑引入缓存模块,这篇随笔介绍使用开源缓存框架CacheManager来实现数据的 ...
- 项目一:第十三天 1、菜单数据管理 2、权限数据管理 3、角色数据管理 4、用户数据管理 5、在realm中动态查询用户权限,角色 6、Shiro中整合ehcache缓存权限数据
1 课程计划 菜单数据管理 权限数据管理 角色数据管理 用户数据管理 在realm中动态查询用户权限,角色 Shiro中整合ehcache缓存权限数据 2 菜单数据添加 2.1 使用c ...
- ehcache缓存框架简介(一)
EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. 我们使用EhCache缓存框架主要是为了判断重复Url,每次爬取一个网 ...
随机推荐
- spring boot (二):使用fastJson解析json数据
如果我们想在spring boot中使用第三方的json解析框架: 1)我们需要在pom.xml文件中引入第三方包的依赖; 2)实现方法: 方法1 需要在启动类中继承WebMvcConfigurerA ...
- 解决Address is in use:Windows和Linux通过杀死进程
在开发无卡支付系统的过程中,因为用了端口来监听服务,在调试程序的时候,忘了关,再次运行的时候会出现Address is in use的问题,即端口已经被绑定,无法再次使用,最直观的方法就是杀死之前的进 ...
- [Robot Framework] Jenkins上调用Rebot命令时执行报错不往下执行其他命令
在配置jenkins job时,添加构建步骤Execute Windows batch command,输入执行rebot命令 报错信息: Call C:\Python27\Scripts\rebot ...
- Continuous Subarray Sum LT523
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- 重新学pytorch
安装: conda install pytorch -c pytorch pip3 install torchvision pip install torchtext 这3个命令就够了
- Python 使用for...in...和 while 循环 实现8种格式的 九九乘法表
#九九乘法表 for...in .. #左下角 for i in range(1,10): for j in range(1,i+1): print(' %d×%d=%2d'%(j,i,i*j), e ...
- GC垃圾收集器分类
参考https://blog.csdn.net/tjiyu/article/details/53983650 Java垃圾收集器组合: 新生代收集器:Serial.ParNew.Parallel Sc ...
- 【WebService】WebService之WSDL文档深入分析(三)
WSDL概念 WSDL(网络服务描述语言,Web Services Description Language)是一门基于 XML 的语言,用于描述 Web Services 以及如何对它们进行访问. ...
- centos 7 禁止root登录及更改ssh端口号
vim /etc/ssh/sshd_config PermitRootLogin yes => PermitRootLogin no systemctl restart sshd.service ...
- Codeforces gym 102062 简要题解
文章目录 A. Bob and BoB B. Vibranium Gift C. The Blood Moon D. Palindrome and Chocolate E. Jumpy Robot F ...