一、準備工做 jar包2. 下載mybatis相關包與ehcache相關包 ehcache-core-2.4.4.jar mybatis-ehcache-1.0.0.jar slf4j-api-1.6.1.jar slf4j-log4j12-1.6.2.jar 二、在src目錄下新建ehcache.xml配置文件 <ehcache xmlns:xsi="" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" monitoring="autodetect" dynamicConfig="true"> <diskStore path="d:\other"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="1200" overflowToDisk="true" /> <!-- securityCache --> <cache name="securityCache" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120000" timeToLiveSeconds="120000" overflowToDisk="true"/>
</ehcache> 三、在src目錄下 新建ehcache.xsd文件
四、在src目錄下新建applicationContext-ehcache.xml配置文件 也能夠加入到其餘配置文件中 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="" xmlns:xsi="" xmlns:tx="" xsi:schemaLocation=" "> <!-- 緩存的配置文件 ehcache.xml 的位置 --> <bean id="defaultCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml" /> </bean>
<!-- 定義ehCache的工廠,並設置所使用的Cache name --> <bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean"> <property name="cacheManager" ref="defaultCacheManager" /> <property name="cacheName" value="securityCache"> </property> </bean> </beans> 五、user-mapper.xml配置文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" ""> <mapper namespace="com.ap.mapper.UserMapper"> <cache type="org.mybatis.caches.ehcache.LoggingEhcache" /> <!-- select article by limit --> <select id="getCount" resultType="int" useCache="true"> <![CDATA[ select count(*) from user ]]> </select> </mapper> 默認狀況下 mybatis sql配置文件的sql是開啓緩存的,若是不想被掃描到的話就將useCache設置爲false 6junit測試 import javax.inject.Inject;
import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.andson.ap.mapper.UserMapper;
@ContextConfiguration("classpath*:applicationContext-*.xml") @RunWith(SpringJUnit4ClassRunner.class) public class Test { @Autowired private UserMapper userMapper; @org.junit.Test public void test(){ System.out.println("------------------------------"); int a = userMapper.getCount(); System.out.println(a); System.out.println("______________________"); int b = userMapper.getCount(); System.out.println(b); } } debug測試 你將會發現打印b的時候沒有執行sql查詢 2014-04-10 20:59:19Creating new JDBC DriverManager Connection to [jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8] 2014-04-10 20:59:19JDBC Connection [com.mysql.jdbc.JDBC4Connection@71cf662f] will not be managed by Spring 2014-04-10 20:59:19ooo Using Connection [com.mysql.jdbc.JDBC4Connection@71cf662f] 2014-04-10 20:59:19==> Preparing: select count(*) from user 2014-04-10 20:59:19==> Parameters: 2014-04-10 20:59:19Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3fb6772a] 2014-04-10 20:59:19Returning JDBC Connection to DataSource 2 ______________________ 2014-04-10 20:59:19Creating a new SqlSession 2014-04-10 20:59:19SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@65d9e279] was not registered for synchronization because synchronization is not active 2014-04-10 20:59:19Cache Hit Ratio [com.ap.mapper.UserMapper]: 0.5 2014-04-10 20:59:19Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@65d9e279] 2 2014-04-10 20:59:19After test method: context [[TestContext@8f0c85e testClass = Test, locations = array<String>['classpath*:applicationContext-*.xml'], testInstance = , testMethod = , testException = [null]]], class dirties context [false], class mode [null], method dirties context [false]. 2014-04-10 20:59:19After test class: context [[TestContext@8f0c85e testClass = Test, locations = array<String>['classpath*:applicationContext-*.xml'], testInstance = [null], testMethod = [null], testException = [null]]], dirtiesContext [false]. 2014-04-10 20:59:19Closing : startup date [Thu Apr 10 20:59:19 CST 2014]; root of context hierarchy 2014-04-10 20:59:19Returning cached instance of singleton bean 'sqlSessionFactory' 2014-04-10 20:59:19Returning cached instance of singleton bean 'lifecycleProcessor' 2014-04-10 20:59:19Destroying singletons in : defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,propertyConfigurer,dataSource,sqlSessionFactory,sqlSessionTemplate,org.mybatis.spring.mapper.MapperScannerConfigurer#0,org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#0,defaultCacheManager,ehCache,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0,userMapper]; root of factory hierarchy 2014-04-10 20:59:19Retrieved dependent beans for bean 'userMapper': [com.andson.ap.test.Test] 2014-04-10 20:59:19Invoking destroy() on bean with name 'defaultCacheManager' 2014-04-10 20:59:19Shutting down EHCache CacheManager 2014-04-10 20:59:19Deleted directory ehcache_auto_created_1397134759678