Spring+myBatis+ehcache的配置

4 篇文章 0 订阅
1 篇文章 0 订阅
一、在POM中添加相关的库包引用:
   <dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>${ehcache.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>


二、添加ehcache的配置:ehcache.xml
<ehcache updateCheck="true" name="application-cache">
<diskStore path="java.io.tmpdir"/>
<defaultCache maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU" />

</ehcache>


三、在spring配置文件 application-context.xml中添加配置:
beans头中添加引用:

       xmlns:p="http://www.springframework.org/schema/p"
xmlns:cache="http://www.springframework.org/schema/cache"

xsi:schemaLocation中添加
http://www.springframework.org/schema/cache           http://www.springframework.org/schema/cache/spring-cache.xsd

添加ehcache的bean:
<cache:annotation-driven/>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref="ehcache"/>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:ehcache.xml" p:shared="true"/>

四、修改mybatis-config.xml:
添加:
  <!-- 全局映射器启用缓存 -->
<setting name="cacheEnabled" value="true"/>

该步骤也可以省略,mybatis默认cacheEnable是打开的

五、在需要使用缓存的mapper中添加:
<cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>
<!--<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>-->

实测中没发现这两者有什么区别。相关资料说LogginEhcache打开了日志。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值