MyBatis缓存

在Mybatis里面,所谓的缓存就是将已经查询过的记录放在内存的缓冲区或文件上,这样如果再次查询,可以通过配置的策略,命中已经查询过的记录.从而提高查询的效率.
缓存作用
提高查询的效率.

当我们访问京东网站等时,我们看到数据都是从数据库查出来,不断的从数据库中查的话,会出现一个问题,因为mysql数据库的数据是放在磁盘中的,从磁盘里查出来要做IO操作,IO操作是非常耗性能的。

缓存图

在这里插入图片描述

1、 数据放在数据库里,数据库的数据是放在磁盘里,磁盘里面查出来是要做IO操作, IO操作是非常耗性能的。
2、 缓存数据存储在内存。
3、 从内存中读取数据是非常快的。
4、 当数据发生改变时,数据库会将数据同步更新到缓存区域中

mybatis缓存分类

Mybatis的缓存分为一级缓存\ 二级缓存

一级缓存

一级缓存:所谓的一级缓存就是会话(SqlSesion对象)级别的缓存,就是同一个会话,如果已经查询过的数据会保存一份在内存中,如果会话没有关闭,再次调用同样的方法查询,不会再查询数据库,而是直接从缓存中取出之前查询的数据.

一级缓存默认是打开的,而且是关闭不了的.

如何清空一级缓存.
1.关闭会话.close()
2.进行了操作(增删改),提交了commit();
3.手工清除缓存clearCache()

测试代码一

	@Test
	public void testSelectByPrimaryKey() {
		// 1.创建SqlSession操作对象
		SqlSession session = MyBatisUtil.openSession();

		// 2.创建UserMapper接口的代理对象
		UserMapper userMapper = session.getMapper(UserMapper.class);

		User user = userMapper.selectByPrimaryKey(3);
		System.out.println(user);
		// 4.关闭session
		session.close();

		SqlSession openSession = MyBatisUtil.openSession();
		UserMapper mapper = openSession.getMapper(UserMapper.class);
		User selectByPrimaryKey = mapper.selectByPrimaryKey(3);
		System.out.println(selectByPrimaryKey);
	}

结果图一:

在这里插入图片描述

因为一级缓存是指同一个sqlSeesion会话中,查询同一条数据时,只会去访问一次数据库。当不同sqlSession时,就出现如上图所示,两个sqlSession访问同一条数据时,访问了两次数据库。

测试代码二

	@Test
	public void testSelectByPrimaryKey() {
		// 1.创建SqlSession操作对象
		SqlSession session = MyBatisUtil.openSession();
		// 2.创建UserMapper接口的代理对象
		UserMapper userMapper = session.getMapper(UserMapper.class);
		for (int i = 0; i < 100; i++) {
			User user = userMapper.selectByPrimaryKey(3);
			//清除缓存
			//session.clearCache();
			System.out.println(user);
		}
		// 4.关闭session
		session.close();
	}

结果图2:

在这里插入图片描述

测试代码三

@Test
	public void testSelectByPrimaryKey() {
		// 1.创建SqlSession操作对象
		SqlSession session = MyBatisUtil.openSession();
		// 2.创建UserMapper接口的代理对象
		UserMapper userMapper = session.getMapper(UserMapper.class);
		for (int i = 0; i < 100; i++) {
			User user = userMapper.selectByPrimaryKey(3);
			//清除缓存
			session.clearCache();
			System.out.println(user);
		}
		// 4.关闭session
		session.close();
	}

结果图三

在这里插入图片描述
我们会发现清除缓存,会比使用缓存慢,并且查询了多次

二级缓存

二级缓存又叫SqlSessionFactory 缓存。 有效范围:同一个 factory 内哪个 SqlSession 都可以获取。

什么时候使用二级缓存?

当数据频繁被使用,很少被修改

如何使用二级缓存

在 mapper.xml 中添加<cache readOnly="true"></cache>
如果不写 readOnly=”true”,写<cache></cache>时,需要把实体类序列化
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.mapper.UserMapper">
	<cache readOnly="true" />
	<select id="selectByPrimaryKey" parameterType="int"
		resultType="com.mybatis.pojo.User">
		select * from user where id=#{id}
	</select>

	<select id="selectAll" resultType="com.mybatis.pojo.User">
		select * from user
	</select>

	<select id="selectCount" resultType="int">
		select count(*) from user
	</select>
</mapper>


当 SqlSession 对象 close()时或 commit()时会把 SqlSession 缓存的数据刷(flush)到 SqlSessionFactory 缓存区中

测试代码

	@Test
	public void testSelectByPrimaryKey() {
		for (int i = 0; i < 1000; i++) {
			// 1.创建SqlSession操作对象
			SqlSession openSession = MyBatisUtil.openSession();
			// 2.创建UserMapper接口的代理对象
			UserMapper mapper = openSession.getMapper(UserMapper.class);
			User user2 = mapper.selectByPrimaryKey(3);
			System.out.println("用户:"+user2);
			// 4.关闭session
			openSession.close();
		}
	}

不使用二级缓存时

在这里插入图片描述

使用二级缓存

在这里插入图片描述

我们对比可以看出使用二级缓存查询不同sqlSession级别的查询会比不使用快。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值