MyBatis整合第三方缓存EHCache

打印 上一主题 下一主题

主题 913|帖子 913|积分 2739

EHCache缓存针对于MyBatis的二级缓存。
MyBatis默认二级缓存是SqlSessionFactory级别的。
添加依赖
  1. <dependency>
  2.         <groupId>org.mybatis.caches</groupId>
  3.         <artifactId>mybatis-ehcache</artifactId>
  4.         <version>1.2.1</version>
  5.        
  6.         <exclusions>
  7.                 <exclusion>
  8.                         <groupId>org.slf4j</groupId>
  9.                         <artifactId>slf4j-api</artifactId>
  10.                 </exclusion>
  11.         </exclusions>
  12. </dependency>
  13. <dependency>
  14.         <groupId>ch.qos.logback</groupId>
  15.         <artifactId>logback-classic</artifactId>
  16.         <version>1.4.11</version>
  17.         <scope>test</scope>
  18. </dependency>
复制代码
创建EHCache的配置文件ehcache.xml

配置文件名必须为ehcache.xml
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.          xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
  4.    
  5.     <diskStore path="D:\ehcache"/>
  6.    
  7.     <defaultCache
  8.             maxElementsInMemory="1000"
  9.             maxElementsOnDisk="10000000"
  10.             eternal="false"
  11.             overflowToDisk="true"
  12.             timeToIdleSeconds="120"
  13.             timeToLiveSeconds="120"
  14.             diskExpiryThreadIntervalSeconds="120"
  15.             memoryStoreEvictionPolicy="LRU">
  16.     </defaultCache>
  17. </ehcache>
复制代码
EHCache配置文件的配置项说明


创建logback日志文件

使用SLF4J日志时,log4j日志文件会失效,需要配置SLF4J的具体实现logback来打印日志。
创建logback的配置文件logback.xml
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.          xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
  4.    
  5.     <diskStore path="D:\ehcache"/>
  6.    
  7.     <defaultCache
  8.             maxElementsInMemory="1000"
  9.             maxElementsOnDisk="10000000"
  10.             eternal="false"
  11.             overflowToDisk="true"
  12.             timeToIdleSeconds="120"
  13.             timeToLiveSeconds="120"
  14.             diskExpiryThreadIntervalSeconds="120"
  15.             memoryStoreEvictionPolicy="LRU">
  16.     </defaultCache>
  17. </ehcache><?xml version="1.0" encoding="utf-8" ?>
  18. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.          xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
  20.    
  21.     <diskStore path="D:\ehcache"/>
  22.    
  23.     <defaultCache
  24.             maxElementsInMemory="1000"
  25.             maxElementsOnDisk="10000000"
  26.             eternal="false"
  27.             overflowToDisk="true"
  28.             timeToIdleSeconds="120"
  29.             timeToLiveSeconds="120"
  30.             diskExpiryThreadIntervalSeconds="120"
  31.             memoryStoreEvictionPolicy="LRU">
  32.     </defaultCache>
  33. </ehcache>[%d{HH:mm:ss.SSS}] [%-5level] [%thread] [%logger]        [%msg]%n<?xml version="1.0" encoding="utf-8" ?>
  34. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  35.          xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
  36.    
  37.     <diskStore path="D:\ehcache"/>
  38.    
  39.     <defaultCache
  40.             maxElementsInMemory="1000"
  41.             maxElementsOnDisk="10000000"
  42.             eternal="false"
  43.             overflowToDisk="true"
  44.             timeToIdleSeconds="120"
  45.             timeToLiveSeconds="120"
  46.             diskExpiryThreadIntervalSeconds="120"
  47.             memoryStoreEvictionPolicy="LRU">
  48.     </defaultCache>
  49. </ehcache><?xml version="1.0" encoding="utf-8" ?>
  50. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  51.          xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
  52.    
  53.     <diskStore path="D:\ehcache"/>
  54.    
  55.     <defaultCache
  56.             maxElementsInMemory="1000"
  57.             maxElementsOnDisk="10000000"
  58.             eternal="false"
  59.             overflowToDisk="true"
  60.             timeToIdleSeconds="120"
  61.             timeToLiveSeconds="120"
  62.             diskExpiryThreadIntervalSeconds="120"
  63.             memoryStoreEvictionPolicy="LRU">
  64.     </defaultCache>
  65. </ehcache>        
复制代码
mapper.xml设置二级缓存的类型
  1. [/code][size=3]测试[/size]
  2. [code]public class CacheTest {
  3.     private static final Logger LOGGER = LoggerFactory.getLogger(CacheTest.class);
  4.     @Test
  5.     public void test2() {
  6.         try {
  7.             //读取MyBatis核心配置文件中的信息
  8.             InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
  9.             //通过SqlSessionFactory创建SqlSession对象
  10.             SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
  11.             SqlSession sqlSession1 = sqlSessionFactory.openSession(true);
  12.             SqlSession sqlSession2 = sqlSessionFactory.openSession(true);
  13.             CacheMapper mapper1 = sqlSession1.getMapper(CacheMapper.class);
  14.             List<Emp> emps1 = mapper1.getEmpByDeptId(1);
  15.             emps1.forEach(System.out::println);
  16.             //关闭sqlSession的时候才会把保存在一级缓存中的数据保存到二级缓存中
  17.             sqlSession1.close();
  18.             CacheMapper mapper2 = sqlSession2.getMapper(CacheMapper.class);
  19.             List<Emp> emps2 = mapper2.getEmpByDeptId(1);
  20.             emps2.forEach(System.out::println);
  21.             //关闭sqlSession的时候才会把保存在一级缓存中的数据保存到二级缓存中
  22.             sqlSession2.close();
  23.         } catch (IOException e) {
  24.             LOGGER.error("测试方法:test2异常:{0}",e);
  25.         }
  26.     }
  27. }
复制代码
输出结果:


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

大号在练葵花宝典

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表