[mybatis]缓存_二级缓存使用&细节
Posted 唐火
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[mybatis]缓存_二级缓存使用&细节相关的知识,希望对你有一定的参考价值。
二级缓存
开启全局二级缓存
<setting name="cacheEnabled" value="true"/>
去mapper.xml中配置使用二级缓存
<cache></cache>
<cache eviction="FIFO" flushInterval="60000" readOnly="false" size="1024" ></cache>
- cache中的配置
- type = “”:指定自定义缓存的全类名;实现Cache接口即可;
我们的POJO需要实现序列化接口
测试
@Test
public void testSecondLevelCache() throws IOException
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
SqlSession sqlSession1 = sqlSessionFactory.openSession();
try
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
EmployeeMapper mapper1 = sqlSession1.getMapper(EmployeeMapper.class);
Employee emp01 = mapper.getEmpById(1);
System.out.println(emp01);
sqlSession.close();
Employee emp02 = mapper1.getEmpById(1);
System.out.println(emp02);
sqlSession1.close();
finally
sqlSession.close();
- 二级缓存失效
只有会话提交或者关闭以后,一级缓存中的数据才会转移到二级缓存中
@Test
public void testSecondLevelCache() throws IOException
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
SqlSession sqlSession1 = sqlSessionFactory.openSession();
try
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
EmployeeMapper mapper1 = sqlSession1.getMapper(EmployeeMapper.class);
Employee emp01 = mapper.getEmpById(1);
System.out.println(emp01);
Employee emp02 = mapper1.getEmpById(1);
System.out.println(emp02);
sqlSession.close();
sqlSession1.close();
finally
以上是关于[mybatis]缓存_二级缓存使用&细节的主要内容,如果未能解决你的问题,请参考以下文章
mybatis_二级缓存深入_使用第三方ehcache配置二级缓存
Mybatis--二级缓存(namespace级别)& 序列化