Mybaits一级缓存和二级缓存分别是什么,区别是什么?

目录

一、什么是一级缓存和二级缓存?

二、一级缓存的失效情况

 三、二级缓存存在的问题

四、一级缓存和二级缓存的区别


一、什么是一级缓存和二级缓存?

一级缓存:默认开启,一级缓存只是相于同一个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.一级缓存是默认开启的,二级缓存需要手动开启。

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值