網站首頁 編程語言 正文
目錄
一、什么是一級緩存和二級緩存?
二、一級緩存的失效情況
?三、二級緩存存在的問題
四、一級緩存和二級緩存的區別
一、什么是一級緩存和二級緩存?
一級緩存:默認開啟,一級緩存只是相于同一個SqlSession對象而言的;
二級緩存:需要手動設置,二級緩存是對于同一個SQLSessionFactory而言的。
二、一級緩存的失效情況
1.不同的SQLSession對應不同的一級緩存
工具類DaoUtil
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class DaoUtil {
private static SqlSessionFactory ssf;
static {
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
ssf = new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession() {
return ssf.openSession();
}
public static void closeResource(SqlSession sqlSession) {
sqlSession.close();
}
}
public class Demo {
public static void main(String[] args) {
SqlSession sqlSession1 = DaoUtil.getSqlSession();
StuMapper stumapper1 = sqlSession1.getMapper(StuMapper.class);
Student s1 = stumapper1.findStudentBysid(5);
System.out.println(s1);
System.out.println("————————————————————————————————————————————————————————");
SqlSession sqlSession2 = DaoUtil.getSqlSession();
StuMapper stumapper2 = sqlSession2.getMapper(StuMapper.class);
Student s2 = stumapper2.findStudentBysid(5);
System.out.println(s2);
DaoUtil.closeResource(sqlSession);
}
}
2.同一個SqlSession單查詢條件不同
public class Demo {
public static void main(String[] args) {
SqlSession sqlSession = DaoUtil.getSqlSession();
StuMapper stumapper = sqlSession.getMapper(StuMapper.class);
Student s1 = stumapper.findStudentBysid(5);
System.out.println(s1);
System.out.println("————————————————————————————————————————————————————");
Student s2 = stumapper.findStudentBysname("鄭竹");
System.out.println(s2);
DaoUtil.closeResource(sqlSession);
}
}
?3.同一個SqlSession兩次查詢期間做了增刪改操作
以新增為例
public class Demo {
public static void main(String[] args) {
SqlSession sqlSession = DaoUtil.getSqlSession();
StuMapper stumapper = sqlSession.getMapper(StuMapper.class);
Student s1 = stumapper.findStudentBysid(5);
System.out.println(s1);
System.out.println("————————————————————————————————————————————————————");
Student s2 = new Student();
s2.setBirthday(new Date());
s2.setClassid(3);
s2.setSsex("女");
s2.setSname("蘇小小");
// 增刪改
int ret = stumapper.addStudent(s2);
if(ret > 0) {
sqlSession.commit();
System.out.println("新增成功");
}else {
sqlSession.rollback();
System.out.println("新增失敗");
}
Student s3 = stumapper.findStudentBysid(5);
System.out.println(s3);
DaoUtil.closeResource(sqlSession);
}
}
4.同一個SqlSession兩次查詢期間手動清空了緩存
public class Demo {
public static void main(String[] args) {
SqlSession sqlSession = DaoUtil.getSqlSession();
StuMapper stumapper = sqlSession.getMapper(StuMapper.class);
Student s1 = stumapper.findStudentBysid(5);
System.out.println(s1);
System.out.println("————————————————————————————————————————————————————");
// 清空緩存
sqlSession.clearCache();
Student s2 = stumapper.findStudentBysid(5);
System.out.println(s2);
System.out.println(s1 == s2);
DaoUtil.closeResource(sqlSession);
}
}
?三、二級緩存存在的問題
1.極大可能會出現錯誤數據,有設計上的缺陷,安全使用的條件比較苛刻;
2.分布式環境下,必然會出現錯誤數據,不推薦使用。
四、一級緩存和二級緩存的區別
1.mybatis的二級緩存相對于一級緩存來說,實現了數據的共享,可控性也更強;
2.一級緩存是對SqlSession對象而言的,二級緩存是對于SqlSessionFactory而言的;
3.一級緩存存儲在內存上,二級緩存存儲在磁盤上;
4.一級緩存是默認開啟的,二級緩存需要手動開啟。
原文鏈接:https://blog.csdn.net/weixin_56373368/article/details/126898594
相關推薦
- 2023-03-21 通俗易懂的C語言快速排序和歸并排序的時間復雜度分析_C 語言
- 2022-08-04 Python練習之讀取XML節點和屬性值的方法_python
- 2022-10-18 使用shell腳本快速登錄容器的實現步驟_linux shell
- 2023-07-04 spring之BeanDefinition
- 2022-11-14 C#中對集合排序的三種方式_C#教程
- 2023-04-07 C#快速實現IList非泛型類接口的自定義類作為數據源_C#教程
- 2023-04-08 C語言字符串左旋的兩種實現方法_C 語言
- 2022-07-22 Redis主從復制關系實現(Linux系統)
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支