[mybatis]缓存_二级缓存使用细节
生活随笔
收集整理的這篇文章主要介紹了
[mybatis]缓存_二级缓存使用细节
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
二級緩存
開啟全局二級緩存
<setting name="cacheEnabled" value="true"/>去mapper.xml中配置使用二級緩存
<cache></cache> <cache eviction="FIFO" flushInterval="60000" readOnly="false" size="1024" ></cache>- cache中的配置
- type = “”:指定自定義緩存的全類名;實現(xiàn)Cache接口即可;
我們的POJO需要實現(xiàn)序列化接口
測試
@Testpublic 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();}}- 二級緩存失效
只有會話提交或者關(guān)閉以后,一級緩存中的數(shù)據(jù)才會轉(zhuǎn)移到二級緩存中
@Testpublic 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 {}}總結(jié)
以上是生活随笔為你收集整理的[mybatis]缓存_二级缓存使用细节的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [mybatis]映射文件_select
- 下一篇: 华硕天选Air拆机评测华硕电脑如何拆机