mybaits二十二:一级缓存失效的几种情况
生活随笔
收集整理的這篇文章主要介紹了
mybaits二十二:一级缓存失效的几种情况
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*
?? ? *緩存
?? ? * ?一級緩存(本地緩存),sqlSession級別的緩存,一級緩存是一直開啟的.
?? ? * ? ? ?與數據庫同一次會話期間查詢到的數據會放在本地緩存中。
?? ? * ? ? ?以后如果需要獲取相同的數據,直接從緩存中取,沒必要再去查詢數據庫
?? ? * ? ? ??
?? ? * ? ? ?一級緩存的幾種失效情況(沒有使用到當前一級緩存的情況,效果就是,還需要再向數據庫發出查詢)
?? ? * ? ? ? ? ?1. sqlSession不同,一個sqlSession是一個一級緩存.不同的sqlSession是不同的一級緩存
?? ? * ? ? ? ? ?2. sqlSession相同,查詢條件不同(當前一級緩存中還沒有這個數據)
?? ? * ? ? ? ? ?3. sqlSession相同,但兩次查詢之間執行了增刪改操作(這次增刪改可能對原來數據有影響)
?? ? * ? ? ? ? ?4. sqlSession相同,手動清除了一級緩存(緩存清空)
?? ? * ?二級緩存:(全局緩存)
?? ? */
?
失效情況一:不同的sqlSession
@Testpublic void testFirstLevelCache() throws IOException{String resource = "mybatis-config.xml";InputStream ins = Resources.getResourceAsStream(resource);SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(ins);SqlSession openSession = sf.openSession();try{EmployeePlusMapper em = openSession.getMapper(EmployeePlusMapper.class);Employee e1 = em.getEmpByDepnos(7369);// 1. sqlSession不同,一個sqlSession是一個一級緩存.不同的sqlSession是不同的一級緩存// 新建的sqlSessionSqlSession openSession2 = sf.openSession();EmployeePlusMapper em2 = openSession2.getMapper(EmployeePlusMapper.class);Employee e2 = em2.getEmpByDepnos(7369);System.out.println(e1 == e2);}finally{openSession.close();}}失效情況二:sqlSession相同,查詢條件不同
@Testpublic void testFirstLevelCache() throws IOException{String resource = "mybatis-config.xml";InputStream ins = Resources.getResourceAsStream(resource);SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(ins);SqlSession openSession = sf.openSession();try{EmployeePlusMapper em = openSession.getMapper(EmployeePlusMapper.class);Employee e1 = em.getEmpByDepnos(7369);// sqlSession相同, 查詢條件不一樣Employee e2 = em.getEmpByDepnos(7499);System.out.println(e1 == e2);}finally{openSession.close();}}失效情況三:sqlSession相同,但兩次查詢之間執行了增刪改操作
@Testpublic void testFirstLevelCache() throws IOException, ParseException{String resource = "mybatis-config.xml";InputStream ins = Resources.getResourceAsStream(resource);SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(ins);SqlSession openSession = sf.openSession();try{EmployeePlusMapper em = openSession.getMapper(EmployeePlusMapper.class);Employee e1 = em.getEmpByDepnos(7369);// 執行增加操作SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); Date dd = formatter.parse("2019-04-03");Employee employee = new Employee("1","tom","grammer",7902,dd,800.0);em.addEmp(employee);// sqlSession相同, 查詢條件也一樣Employee e2 = em.getEmpByDepnos(7369);System.out.println(e1 == e2);}finally{openSession.close();}}失效情況四:sqlSession相同,手動清除了一級緩存
@Testpublic void testFirstLevelCache() throws IOException, ParseException{String resource = "mybatis-config.xml";InputStream ins = Resources.getResourceAsStream(resource);SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(ins);SqlSession openSession = sf.openSession();try{EmployeePlusMapper em = openSession.getMapper(EmployeePlusMapper.class);Employee e1 = em.getEmpByDepnos(7369);// 手動清除一級緩存openSession.clearCache();// sqlSession相同, 查詢條件也一樣Employee e2 = em.getEmpByDepnos(7369);System.out.println(e1 == e2);}finally{openSession.close();}}?
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的mybaits二十二:一级缓存失效的几种情况的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux查看tomcat 控制台输出命
- 下一篇: ORACLE 11g 使用ROWNUM完