Mybatis 整合 Ehcache

一、Mybatis整合ehcache

  ehcache 是一个纯 Java 的进程内缓存框架,是一种广泛使用的开源 Java 分布式缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。还可使用 Redis 等。在 Mybatis 中提供了一个 cache 接口(org.apache.ibatis.cache.Cache.class),如果要实现自己的缓存逻辑,实现 cache 接口开发即可。Mybatis 的默认 cache 实现类是 PerpetualCache(org.apache.ibatis.cache.impl.PerpetualCache.class)。
  Mybatis 和 ehcache 整合,Mybatis 和 ehcache 整合包中提供了一个cache接口的实现类org.mybatis.caches.ehcache.EhcacheCache.class

(一)加入ehcache的jar包

  1. ehcache-core-2.6.5.jar
  2. mybatis-ehcache-1.0.2.jar
    Mybatis 整合 Ehcache_第1张图片

(二)整合ehcache

  在 Mapper 文件中为 cache 配置参数org.mybatis.caches.ehcache.EhcacheCache即可。


	
	
	

(三)加入ehcache配置文件

  /mybatis01/src/config/ehcache.xml


	 
        
        
		
        
        
    


  整合完成,还是使用二级缓存时的测试代码进行测试,测试日志:

DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
 WARN [main] - No configuration found. Configuring ehcache from ehcache-failsafe.xml  found in the classpath: jar:file:/D:/01JavaEclipse/mybatis01/lib/ehcache-core-2.6.5.jar!/ehcache-failsafe.xml
DEBUG [main] - Configuring ehcache from URL: jar:file:/D:/01JavaEclipse/mybatis01/lib/ehcache-core-2.6.5.jar!/ehcache-failsafe.xml
DEBUG [main] - Configuring ehcache from InputStream
DEBUG [main] - Ignoring ehcache attribute xmlns:xsi
DEBUG [main] - Ignoring ehcache attribute xsi:noNamespaceSchemaLocation
DEBUG [main] - Disk Store Path: C:\Users\14345\AppData\Local\Temp\
DEBUG [main] - Creating new CacheManager with default config
DEBUG [main] - propertiesString is null.
DEBUG [main] - No CacheManagerEventListenerFactory class specified. Skipping...
DEBUG [main] - No BootstrapCacheLoaderFactory class specified. Skipping...
DEBUG [main] - CacheWriter factory not configured. Skipping...
DEBUG [main] - No CacheExceptionHandlerFactory class specified. Skipping...
DEBUG [main] - Initialized net.sf.ehcache.store.MemoryStore for com.mapper.UserMapper
DEBUG [main] - Using diskstore path C:\Users\14345\AppData\Local\Temp
DEBUG [main] - Holding exclusive lock on C:\Users\14345\AppData\Local\Temp\.ehcache-diskstore.lock
DEBUG [main] - Failed to delete file com%002emapper%002e%0055ser%004dapper.index
DEBUG [main] - Matching data file missing (or empty) for index file. Deleting index file C:\Users\14345\AppData\Local\Temp\com%002emapper%002e%0055ser%004dapper.index
DEBUG [main] - Failed to delete file com%002emapper%002e%0055ser%004dapper.index
DEBUG [main] - Initialised cache: com.mapper.UserMapper
DEBUG [main] - CacheDecoratorFactory not configured for defaultCache. Skipping for 'com.mapper.UserMapper'.
DEBUG [main] - Cache Hit Ratio [com.mapper.UserMapper]: 0.0
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 690521419.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@2928854b]
DEBUG [main] - ==>  Preparing: SELECT * FROM USER WHERE ID = ? 
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <==      Total: 1
User [id=1, username=测试, sex=null, birthday=null, address=null]
DEBUG [main] - put added 0 on heap
DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@2928854b]
DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@2928854b]
DEBUG [main] - Returned connection 690521419 to pool.
DEBUG [main] - Cache Hit Ratio [com.mapper.UserMapper]: 0.5
User [id=1, username=测试, sex=null, birthday=null, address=null]

你可能感兴趣的:(#,Mybatis,➤,框架技术)