zoukankan      html  css  js  c++  java
  • mybatis的二级缓存的使用

    1.引入ehcache的jar包和mybatis整合ehcache的jar包;

            <!-- ehchache -->
            <dependency>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache</artifactId>
                <version>2.8.3</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-ehcache</artifactId>
                <version>1.0.0</version>
            </dependency>

    2.配置ehcache.xml文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache updateCheck="false" name="txswx-ehcache">
        <diskStore path="java.io.tmpdir"/>
        <!-- DefaultCache setting. -->
        <defaultCache maxEntriesLocalHeap="10000" eternal="true" timeToIdleSeconds="300" timeToLiveSeconds="600"
                      overflowToDisk="true" maxEntriesLocalDisk="100000"/>
    </ehcache>

    3.mybatis-config.xml中开启二级缓存:<setting name="cacheEnabled" value="true"/>;

    4.applicationContext.xml中配置ehcache的管理器;

    <!-- Cache配置 -->
        <cache:annotation-driven cache-manager="cacheManager"/>
        <bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
              p:configLocation="/WEB-INF/conf/ehcache.xml"/>
        <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
              p:cacheManager-ref="ehCacheManagerFactory"/>

    5.*mapper.xml文件中配置缓存:

    <cache type="org.mybatis.caches.ehcache.LoggingEhcache" > 
        <property name="timeToIdleSeconds" value="3600"/><!--1 hour-->
        <property name="timeToLiveSeconds" value="3600"/><!--1 hour-->
        <property name="maxEntriesLocalHeap" value="1000"/>
        <property name="maxEntriesLocalDisk" value="10000000"/>
        <property name="memoryStoreEvictionPolicy" value="LRU"/>
    </cache>

    这样二级缓存就起作用了。
    不启用缓存:

    修改statementuseCachefalse,表示该statement不再进行二级缓存

    <select id="getCountByName" parameterType="java.util.Map" resultType="INTEGER" statementType="CALLABLE" useCache="false">

     执行更新后,不刷新二级缓存:

    <update id="updateUser" parameterType="cn.itcast.mybatis.po.User" flushCache="false">   
        update user set username = #{username},birthday = #{birthday},sex = #{sex} where id = #{id} 
      </update>
     
  • 相关阅读:
    函数(一.return)
    if、for、while的详解及实例(一)
    Silverlight 中 TreeView 的数据绑定
    跟着微软玩 WCF RIA Services (1) – 安装AdventureWorks OLTP数据库
    MEF学习(一) MEF介绍
    WCF学习笔记(1)面向服务
    基础知识应用程序配置文件(将section转换成对象一)
    Silverlight编译出错: 未给任务“CreateRiaClientFilesTask”的必需参数“ClientFrameworkPath”赋值
    悟道MVVM 一 各施其职
    Lync 2010 二次开发(一) 开发环境的部署
  • 原文地址:https://www.cnblogs.com/super-chao/p/9809606.html
Copyright © 2011-2022 走看看