01

    xiaoxiao2023-07-31  129

    

    1 mybatis集成时需要的jar

    ehcache-core-2.6.5.jar

    mybatis-ehcache-1.0.2.jar

    Mybatis、日志、EHCache所需要的jar包如下:

    2 EHCachemybatis集成

    EHCache是一种广泛使用java分布式缓存通用缓存,JavaEE中的一个轻量级的容器。

    EHCache集成是基于ehcache-core,没有任何其它第三方应用程序。

    想使用EHCache到她们的应用程序的用户,必须下载EHCachezip bundle,解压它添加这些jarsclasspath路径下。使用Apache Maven的用户只需要在pom.xml文件中添加如下内容:

    <dependencies>   ...   <dependency>     <groupId>org.mybatis.caches</groupId>     <artifactId>mybatis-ehcache</artifactId>     <version>1.0.2</version>   </dependency>   ... </dependencies>

    接着需要在mapperxml文件中配置如下内容:

    <mapper namespace="org.acme.FooMapper">

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

    </mapper>

    你也可以提供可以再运行动态变更的如下参数:

    <mapper namespace="org.acme.FooMapper">

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

        <property name="timeToIdleSeconds" value="3600"/><!--1hour-->

        <property name="timeToLiveSeconds" value="3600"/><!--1hour-->

        <property name="maxEntriesLocalHeap" value="1000"/>

        <property name="maxEntriesLocalDisk" value="10000000"/>

        <property name="memoryStoreEvictionPolicy" value="LRU"/>

      </cache>

      ...

    </mapper>

    如果用户需要日志缓存操作,可以插入Cachelogging version:

    <mapper namespace="org.acme.FooMapper">

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

      ...

    </mapper>

        需要通xml配置文件配置EHCache的用户,必须要在classpath路径下放置/ehcache.xml资源。

    /ehcache.xml资源没有发现或者在加载/ehcache.xml的时候发生错误,将会使用默认的配置。

    3 /ehcache.xml配置文件参数配置

    <?xml version="1.0" encoding="UTF-8"?>

    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:noNamespaceSchemaLocation="ehcache.xsd"

       updateCheck="true" monitoring="autodetect" dynamicConfig="true">

     

       <diskStore path="java.io.tmpdir" />

      

       <!-- 

        nameCache的唯一标识

        maxElementsInMemory:内存中最大缓存对象数

        maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大

        eternalElement是否永久有效,一但设置了,timeout将不起作用

        overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中

        timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大

        timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间

            和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大 

        diskPersistent:是否缓存虚拟机重启期数据 

        diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120

        diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区

        memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用) 

        -->

       <defaultCache maxElementsInMemory="10000"

          eternal="false"

          timeToIdleSeconds="120"

          timeToLiveSeconds="120"

          overflowToDisk="true"

          maxElementsOnDisk="10000000"

          diskPersistent="false"

          diskExpiryThreadIntervalSeconds="120"

          memoryStoreEvictionPolicy="LRU" />

    </ehcache>

    sqlMapConfig.xml中的<settings>设置如下:

    <!-- 开启延迟加载 -->

    <settings>

       <!-- 全局的延迟加载的开关必须要开启 -->

       <setting name="lazyLoadingEnabled" value="true"/>

       <!-- 积极加载设置成false -->

       <setting name="aggressiveLazyLoading" value="false"/>

       <!-- 开启二级缓存, 缓存中只要是需要配置的针对的都是二级缓存 -->

       <setting name="cacheEnabled" value="true"/>

    </settings>

        mapper文件中的内容如下:

    相关资源:mybatis教程之查询缓存(一级缓存二级缓存和整合ehcache)
    最新回复(0)