日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Mybaits一級緩存和二級緩存分別是什么,區別是什么?

作者:柒畫503 更新時間: 2022-09-22 編程語言

目錄

一、什么是一級緩存和二級緩存?

二、一級緩存的失效情況

?三、二級緩存存在的問題

四、一級緩存和二級緩存的區別


一、什么是一級緩存和二級緩存?

一級緩存:默認開啟,一級緩存只是相于同一個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

欄目分類
最近更新