mybatis 整合ehcache实现缓存

mybatis 整合ehcache实现缓存
   mybatis 一级缓存和二级缓存的区别:
1、一级缓存:基于PerpetualCache的HashMap本地缓存,其存储作用域为同一个SqlSession,当Session flush或close之后,该Session中的所有Cache就将清空。
2、二级缓存:与一级缓存其机制相同,默认也是采用PerpetualCache,HashMap存储,不同在于其存储作用域为Mapper(Namespace),并且可自定义存储源,如Ehcache。
3、对于缓存数据更新机制,当某一个作用域(一级缓存Session/二级缓存Namespaces)的进行了C/U/D操作后,默认该作用域下所有select中的缓存将被clear。
如果要实现MyBatis的二级缓存,一般来说有如下两种方式:
1. 采用MyBatis内置的Cache机制。
2. 采用三方Cache框架, 比如EhCache, OSCache等等。

  

一、导包:基于ssm---》添加ehcache-core-2.4.4.jar  mybatis-ehcache-1.0.0.jar 这两个jar包(当前这两个版本一致时可以实现,我之前两个版本用其他的报错,反正就是要匹配得上) (slf4j-api-1.6.2.jar和
   slf4j-log4j12-1.6.2.jar也要 <!--ehcache依赖slf4j--> <!--slf4j需要log4j-->

  二、文件目录 --新建ehcache.xml (目录如下)


     <?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<!-- java.io.tmpdir:Java临时目录。指定一个文件目录,当EhCache把数据写到硬盘上或者系统jvm内存时,将把数据写到这个文件目录下 -->
<diskStore path="java.io.tmpdir"/>
<!-- maxElementsInMemory:设置基于内存的缓存可存放对象的最大数目。  -->
<!-- eternal:如果为true,表示对象永远不会过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds属性,默认为false; -->
<!-- timeToIdleSeconds: 设定允许对象处于空闲状态的最长时间,以秒为单位。当对象自从最近一次被访问后,如果处于空闲状态的时间超过了 -->
<!-- timeToIdleSeconds属性值,这个对象就会过期。当对象过期,EHCache将把它从缓存中清空。只有当eternal属性为false,该属性才有效。 -->
<!-- 如果该属性值为0,则表示对象可以无限期地处于空闲状态。  -->
<!-- timeToLiveSeconds:设定对象允许存在于缓存中的最长时间,以秒为单位。当对象自从被存放到缓存中后,如果处于缓存中的时间超过了 timeToLiveSeconds属性值,这个对象就会过期。当对象过期,EHCache将把它从缓存中清除。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地存在于缓存中。timeToLiveSeconds必须大于timeToIdleSeconds属性,才有意义。  -->
<!-- overflowToDisk:如果为true,表示当基于内存的缓存中的对象数目达到了maxElementsInMemory界限后,会把益出的对象写到基于硬盘的缓存中。 -->
<!-- 设定缓存的默认数据过期策略 -->
<defaultCache
maxElementsInMemory="10000" 
eternal="false" 
overflowToDisk="true"
timeToIdleSeconds="10"
timeToLiveSeconds="20"
diskPersistent="false"  
diskExpiryThreadIntervalSeconds="120"/>
<!--  自定义缓存策略-学生信息缓存容器对应策略-->
<cache name="userCache"          
maxElementsInMemory="1000"  
eternal="false"             
overflowToDisk="true"       
timeToIdleSeconds="10"      
timeToLiveSeconds="20"/> 
   <!--  自定义缓存策略-教师信息缓存容器对应策略-->  
<!--      <cache name="techerCache"          -->
<!--         maxElementsInMemory="1000"  -->
<!--         eternal="false"                 -->
<!--         overflowToDisk="true"       -->
<!--         timeToIdleSeconds="10"      -->
<!--         timeToLiveSeconds="20"/>        -->
</ehcache>
   三、 IUserMapper.xml 中添加下面(*mapper.xml想要哪些sql进行缓存就在相应的xml里面加)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.user.dao.IUserMapper">
<resultMap type="com.user.entity.User" id="userMap">
   <id  property="id" column="id"/>
   <result property="name" column="name"/>
   <result property="psd" column="psd"/>
</resultMap>   
   <!--在mapper文件中的头部引入缓存策略-->
   <!-- 以下两个<cache>标签二选一,第一个可以输出日志,第二个不输出日志 -->
   <!--   <cache type="org.mybatis.caches.ehcache.EhcacheCache" /> -->
<cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> 

四、测试类:
        @RequestMapping("findUserList")
public String findUserList(HttpServletRequest request, Model model) {
User user1 = (User) request.getSession().getAttribute("user");
List<User> user = userService.findUserList(user1.getName());//第一次执行正常查询
System.out.println("----------------------------start");
List<User> user22 = userService.findUserList(user1.getName()); //第二次执行从缓存中获取数据(打印sql如下)
System.out.println("----------------------------end");
model.addAttribute("userList", user);
return "user/userInformation";
}
五、执行该方法之后:第一次正常查询,第二次获取缓存内数据

    如上就成功。

六、有个问题,

用spring管理 ehcache 和 直接在 mapper.xml 文件里面使用ehcache有什么区别么

整合玩ssm框架以后 加上 必要的ehcaceh的jar包,然后直接在整个usermapper.xml 文件中增加 一行

<cache type="org.mybatis.caches.ehcache.LoggingEhcache" />

 就可以直接使用ehcache了

可是   还可以在 applicationContext.xml文件中 增加 ehcaceh 管理

然后 使用@cacheable,spring 的粒度更细     这 两者  有那些不一样的地方。 有没有人能说一下。



猜你喜欢

转载自blog.csdn.net/sinat_34338162/article/details/80697868