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

阅读: 评论:0

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

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 = ResourceAsStream(&#l");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 = SqlSession();StuMapper stumapper1 = Mapper(StuMapper.class);Student s1 = stumapper1.findStudentBysid(5);System.out.println(s1);System.out.println("————————————————————————————————————————————————————————");SqlSession sqlSession2 = SqlSession();StuMapper stumapper2 = Mapper(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 = SqlSession();StuMapper stumapper = Mapper(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 = SqlSession();StuMapper stumapper = Mapper(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) {sqlSessionmit();System.out.println("新增成功");}else {llback();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 = SqlSession();StuMapper stumapper = Mapper(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.分布式环境下,必然会出现错误数据,不推荐使用。

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

2.一级缓存是对SqlSession对象而言的,二级缓存是对于SqlSessionFactory而言的;

3.一级缓存存储在内存上,二级缓存存储在磁盘上;

4.一级缓存是默认开启的,二级缓存需要手动开启。

本文发布于:2024-01-29 14:11:13,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170650867615838.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:缓存   二级缓存   区别   Mybaits
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23