日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

mybaits二十二:一级缓存失效的几种情况

發布時間:2025/6/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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二十二:一级缓存失效的几种情况的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。