目录
一、什么是一级缓存和二级缓存?
二、一级缓存的失效情况
三、二级缓存存在的问题
四、一级缓存和二级缓存的区别
一、什么是一级缓存和二级缓存?
一级缓存:默认开启,一级缓存只是相于同一个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.一级缓存是默认开启的,二级缓存需要手动开启。