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整合包,如果不知道怎么引入,可以参考官方文档,如下图:

官方地址: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缓存框架的更多相关文章

  1. [原创]mybatis中整合ehcache缓存框架的使用

    mybatis整合ehcache缓存框架的使用 mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓 ...

  2. mybatis中整合ehcache缓存框架的使用

    mybatis整合ehcache缓存框架的使用 mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓 ...

  3. Spring MVC学习总结(7)——Spring MVC整合Ehcache缓存框架

    Ehcache算是当前比较流行的缓存框架,使用缓存可以极大的缓解服务器和数据库的压力,提高访问效率,提高服务器的并发能力.接下来我们看怎么把缓存使用起来. SpringMVC集成Ehcache所需的j ...

  4. mybatis0210 mybatis和ehcache缓存框架整合

    .1mybatis和ehcache缓存框架整合 一般不用mybatis来管理缓存而是用其他缓存框架在管理缓存,因为其他缓存框架管理缓存会更加高效,因为别人专业做缓存的而mybatis专业做sql语句的 ...

  5. (十一)mybatis之整合ehcache缓存

    一.二级缓存 大家都知道使用mybatis就要先获取sqlsessionfactory,继而使用sqlsession来和数据库交互,每次只需要使用sqlsession对象提供的方法就好,当我们需要第一 ...

  6. 项目一:第十四天 1.在realm中动态授权 2.Shiro整合ehcache 缓存realm中授权信息 3.动态展示菜单数据 4.Quartz定时任务调度框架—Spring整合javamail发送邮件 5.基于poi实现分区导出

    1 Shiro整合ehCache缓存授权信息 当需要进行权限校验时候:四种方式url拦截.注解.页面标签.代码级别,当需要验证权限会调用realm中的授权方法   Shiro框架内部整合好缓存管理器, ...

  7. C#开发微信门户及应用(48) - 在微信框架中整合CacheManager 缓存框架

    在我们的很多框架或者项目应用中,缓存在一定程度上可以提高程序的响应速度,以及减轻服务器的承载压力,因此在一些地方我们都考虑引入缓存模块,这篇随笔介绍使用开源缓存框架CacheManager来实现数据的 ...

  8. 项目一:第十三天 1、菜单数据管理 2、权限数据管理 3、角色数据管理 4、用户数据管理 5、在realm中动态查询用户权限,角色 6、Shiro中整合ehcache缓存权限数据

    1 课程计划 菜单数据管理 权限数据管理 角色数据管理 用户数据管理 在realm中动态查询用户权限,角色 Shiro中整合ehcache缓存权限数据         2 菜单数据添加 2.1 使用c ...

  9. ehcache缓存框架简介(一)

    EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. 我们使用EhCache缓存框架主要是为了判断重复Url,每次爬取一个网 ...

随机推荐

  1. 5F - Coin Change

    Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make c ...

  2. 从1~N中任选出三个数,最小公倍数最大

    已知一个正整数N,问从1~N中任选出三个数,它们的最小公倍数最大可以为多少. 当n为奇数:n.n-1.n-2这是三个最大数,并且它们两两互质.因为连续的奇.偶.奇,互质.连续的两个数互质是因为它们的公 ...

  3. Codeforces 799D. String Game 二分

    D. String Game time limit per test:2 seconds memory limit per test:512 megabytes input:standard inpu ...

  4. 查看CPU核数和内存

    查看CPU核数 top 然后按数字键1 通过虚拟文件系统proc,直接获取CPU总数量 cat /proc/cpuinfo | grep processor 查看内存 free命令主要用于显示内存数量 ...

  5. 【jdbcTemplate】baseDao书写规范

    今天加班,为了下个月的北京之行,希望父亲身体安康,一切顺利: 老大今天发出来同事的代码,并标记了jdbcTemplate的书写规范,此处查询数据库之前声明对象时,不用new出来,因为在底层源码中已经给 ...

  6. python代码检索小引擎

    https://www.programcreek.com/python/

  7. 【算法专题】工欲善其事必先利其器—— 常用函数和STL

    一.    常用函数 #include <stdio.h> int getchar( void );               //读取一个字符, 一般用来去掉无用字符 char *ge ...

  8. @Valid报错 No validator could be found for constraint

    使用hibernate validator出现上面的错误, 需要 注意 @NotNull 和 @NotEmpty  和@NotBlank 区别 @NotEmpty 用在集合类上面@NotBlank 用 ...

  9. 【Java】使用Apache POI生成和解析Excel文件

    概述 Excel是我们平时工作中比较常用的用于存储二维表数据的,JAVA也可以直接对Excel进行操作,分别有jxl和poi,2种方式. HSSF is the POI Project's pure ...

  10. DOM-查找和修改

    1. 查找: 按HTML查找: 问题: 每次只能按一个条件查找,如果查找条件复杂,则步骤很繁琐 解决: 选择器: 按选择器查找: 2个API 1. 只查找一个元素: var elem=parent.q ...