Ehcache和mybatis整合

首先在maven中添加

        <!-- ehcache -->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.10.1</version>
        </dependency>
        <!-- ehcache-mybatis整合 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-ehcache</artifactId>
            <version>1.0.0</version>
        </dependency>       

创建ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
    <diskStore path="D:\ehcache" />
    <!--  上面的diskStor path 你可以指定某一个路径下,java.io.tmpdir 指的是你系统的缓存目录  效率会好一点 -->
    <defaultCache 
        maxElementsInMemory="1000"
        maxElementsOnDisk="10000000" 
        eternal="false" 
        overflowToDisk="false"
        timeToIdleSeconds="120" 
        timeToLiveSeconds="120"
        diskExpiryThreadIntervalSeconds="120" 
        memoryStoreEvictionPolicy="LRU">
    </defaultCache> 
    <!-- 
    属性说明:
     diskStore:指定数据在磁盘中的存储位置。
     defaultCache:当借助CacheManager.add("demoCache")创建Cache时,EhCache便会采用<defalutCache/>指定的的管理策略
    以下属性是必须的:
     maxElementsInMemory - 在内存中缓存的element的最大数目 
     maxElementsOnDisk - 在磁盘上缓存的element的最大数目,若是0表示无穷大
     eternal - 设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断
     overflowToDisk - 设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上
    以下属性是可选的:
     timeToIdleSeconds - 当缓存在EhCache中的数据前后两次访问的时间超过timeToIdleSeconds的属性取值时,这些数据便会删除,默认值是0,也就是可闲置时间无穷大
     timeToLiveSeconds - 缓存element的有效生命期,默认是0.,也就是element存活时间无穷大
       diskSpoolBufferSizeMB 这个参数设置DiskStore(磁盘缓存)的缓存区大小.默认是30MB.每个Cache都应该有自己的一个缓冲区.
     diskPersistent - 在VM重启的时候是否启用磁盘保存EhCache中的数据,默认是false。
     diskExpiryThreadIntervalSeconds - 磁盘缓存的清理线程运行间隔,默认是120秒。每个120s,相应的线程会进行一次EhCache中数据的清理工作
     memoryStoreEvictionPolicy - 当内存缓存达到最大,有新的element加入的时候, 移除缓存中element的策略。默认是LRU(最近最少使用),可选的有LFU(最不常使用)和FIFO(先进先出)
     -->
</ehcache> 

在自己的mapper里添加cache ,然后自己的pojo要序列化,要不然内存满了要写到磁盘的时候进行io要报错
这里调用的是ehcache中的设置的默认的cache

<?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.is.dao.UserMapper" >
    <!-- ehcache 下面两个选一个就好,一个有日志,一个没有
    <cache type="org.mybatis.caches.ehcache.EhcacheCache" />  -->         
    <cache type="org.mybatis.caches.ehcache.LoggingEhcache" />  


  <resultMap id="BaseResultMap" type="com.is.model.User" >
    <id column="user_id" property="userId" jdbcType="BIGINT" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="real_name" property="realName" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
    <result column="role_id" property="roleId" jdbcType="BIGINT" />
    <result column="status" property="status" jdbcType="CHAR" />
    <result column="belong" property="belong" jdbcType="CHAR" />
    <result column="operator_id" property="operatorId" jdbcType="BIGINT" />
    <result column="insert_time" property="insertTime" jdbcType="TIMESTAMP" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
  </resultMap>
</mapper>

弄完上面的就可以在mybitas中使用ehcache了,查询速度有明显的提高

至于要为不同的mapper配置不同的cache,或者再整合spring使用注解,试了网上所说的各种方法配置,都没有啥效果,不知道是不是我用的版本不对还是别的什么的原因。。暂时就先这样玩玩吧。。以后有空再研究

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值