MyBatis 缓存技

为了提升查询效率,提高用户体验,MyBatis提供了数据缓存支持,依据数据缓存的有效范围默认定义了一级缓存和二级缓存。

一级缓存

              1、该级缓存默认开启,不能关闭;

              2、该级缓存为SqlSession级别的缓存,也称为本地缓存;

              3、以下4种情况将会导致该级缓存失效:

                             a、在不同SqlSession中查询数据;

public class Test {
	public static void main(String[] args) {
		try {
			InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
			SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream);
			
			SqlSession sqlSession = sqlSessionFactory.openSession();
			IUserInfoDao userInfoDao = sqlSession.getMapper(IUserInfoDao.class);
			List<UserInfo> list = userInfoDao.select();
			System.out.println(list.size());
			list = userInfoDao.select();
			System.out.println(list.size());
			sqlSession.close();
			//第二个sqlSession
			sqlSession = sqlSessionFactory.openSession();
			userInfoDao = sqlSession.getMapper(IUserInfoDao.class);
			list = userInfoDao.select();
			System.out.println(list.size());
			sqlSession.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 

       b、相同SqlSession中查询数据,但查询条件不同

public class Test {
	public static void main(String[] args) {
		try {
			InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
			SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream);
			SqlSession sqlSession = sqlSessionFactory.openSession();
			IUserInfoDao userInfoDao = sqlSession.getMapper(IUserInfoDao.class);
			List<UserInfo> list = userInfoDao.select("%m%");//一种条件
			System.out.println(list.size());
			list = userInfoDao.select("%i%");//一种条件
			System.out.println(list.size());
			sqlSession.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

c、相同SqlSession中查询数据,但两次查询之间执行了增删改操作

public class Test {
	public static void main(String[] args) {
		try {
			InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
			SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream);
			SqlSession sqlSession = sqlSessionFactory.openSession();
			IUserInfoDao userInfoDao = sqlSession.getMapper(IUserInfoDao.class);
			List<UserInfo> list = userInfoDao.select();
			System.out.println(list.size());
			
			boolean flag = userInfoDao.delete(7);
			System.out.println(flag);
			
			list = userInfoDao.select();
			System.out.println(list.size());
			sqlSession.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

       d、相同SqlSession中查询数据,但第二次查询前,程序调用SqlSession对象clearCache()方法手动清除了一级缓存

public class Test {
	public static void main(String[] args) {
		try {
			InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
			SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream);
			SqlSession sqlSession = sqlSessionFactory.openSession();
			IUserInfoDao userInfoDao = sqlSession.getMapper(IUserInfoDao.class);
			List<UserInfo> list = userInfoDao.select();
			System.out.println(list.size());
			
			sqlSession.clearCache();
			
			list = userInfoDao.select();
			System.out.println(list.size());
			sqlSession.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

二级缓存

              1、该级缓存默认不开启,但如果使用二级缓存需要在每个XML映射文件中添加<cache></cache>以配置该级缓存。二级缓存可以通过在全局配置文件配置setting标签来关闭该级缓存。

                     cache标签属性:

                            eviction:缓存回收策略:LRU – 最近最少使用的:移除最长时间不被使用的对象,默认值;FIFO – 先进先出:按对象进入缓存的顺序来移除它们;SOFT – 软引用:移除基于垃圾回收器状态和软引用规则的对象;WEAK – 弱引用:更积极地移除基于垃圾收集器状态和弱引用规则的对象;

                            flushInterval:刷新间隔,单位毫秒,默认情况是不设置,也就是没有刷新间隔,缓存仅仅调用语句时刷新

                            size:引用数目,正整数,代表缓存最多可以存储多少个对象,太大容易导致内存溢出

                            readOnly:只读,默认为false。true:只读缓存;会给所有调用者返回缓存对象的相同实例,速度快;false:读写缓存;会返回缓存对象的拷贝(通过序列化),速度慢但安全。

              2、该级缓存为namespace级别的缓存

              3、工作机制:通过SqlSession查询数据,这些数据将会放到当前会话的一级缓存中;如果当前会话关闭,则一级缓存中的数据会被保存到二级缓存中,此后新的SqlSession将从二级缓存中查找数据;

public class Test {
	public static void main(String[] args) {
		try {
			InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
			SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream);
			SqlSession sqlSession = sqlSessionFactory.openSession();
			IUserInfoDao userInfoDao = sqlSession.getMapper(IUserInfoDao.class);
			List<UserInfo> list = userInfoDao.select();
			System.out.println(list.size());
			
			sqlSession.close();
			
			sqlSession = sqlSessionFactory.openSession();
			userInfoDao = sqlSession.getMapper(IUserInfoDao.class);
			list = userInfoDao.select();
			System.out.println(list.size());
			sqlSession.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

4、select标签的useCache属性用于设置是否使用二级缓存;insert、update、delete或select标签均有flushCache属性,其中增删改默认true,即sql执行以后,会同时清空一级和二级缓存,查询默认false。缺点:会把所有的已经查询的数据全部删除,所以用redis缓存器。Redis只删除想删除的

public class Test {
	public static void main(String[] args) {
		try {
			InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
			SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream);
			SqlSession sqlSession = sqlSessionFactory.openSession();
			IUserInfoDao userInfoDao = sqlSession.getMapper(IUserInfoDao.class);
			List<UserInfo> list = userInfoDao.select();
			System.out.println(list.size());
			
			sqlSession.close();
			
			sqlSession = sqlSessionFactory.openSession();
			userInfoDao = sqlSession.getMapper(IUserInfoDao.class);
			boolean flag = userInfoDao.delete(5);//清空二级缓存,全部清除
			System.out.println(flag);
			
			list = userInfoDao.select();
			System.out.println(list.size());
			sqlSession.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

       5、为了提高扩展性,MyBatis定义了Cache缓存接口,可以通过实现该缓存接口自定义二级缓存,jar包下载:https://github.com/mybatis

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值