五、mybatis缓存

一、什么是缓存?

缓存是互联网系统经常用到的,其特点是在计算机内存上保存的数据,在读取的时候无需再从磁盘读入,因此具备快速读取和使用的特点,如果缓存命中率高,那么可以极大的提高系统性能,如果缓存命中率低,那么久不存在使用的意义了,所以使用缓存关键在于存储内容访问的命中率

二、mybatis缓存

mybatis分为:一级缓存、二级缓存两种;在没有配置的默认情况下,它只是开启一级缓存;
一级缓存:只是相对于同一个sqlsession
二级缓存:相对于同一个sqlSessionFactory



一级缓存测试:

下面就来测试一下一级缓存效果,它是相对于同一个sqlsession才生效的

先查看不同sqlsession产生的效果,可以看到默认情况下不同的sqlsession对同一条数据会发送多条sql

		SqlSessionFactory sqlSessionFactory = MySqlSessionFacoty.getSqlSessionFactory();
		SqlSession openSession = sqlSessionFactory.openSession();
		SqlSession openSession2 = sqlSessionFactory.openSession();
		PersonMapper mapper = openSession.getMapper(PersonMapper.class);
		PersonMapper mapper2 = openSession2.getMapper(PersonMapper.class);
		System.out.println("---------------1");
		Person p1 = mapper.queryPersonById(10);
		System.out.println("---------------2");
		Person p2 = mapper2.queryPersonById(10);
---------------1
2019-07-29 23:17:57 DEBUG mybatis.sql.com.cjy.mybatis.dao.PersonMapper.queryPersonById - ==>  Preparing: select id,user_name,marker from person where id = ? 
2019-07-29 23:17:57 DEBUG mybatis.sql.com.cjy.mybatis.dao.PersonMapper.queryPersonById - ==> Parameters: 10(Integer)
2019-07-29 23:17:57 DEBUG mybatis.sql.com.cjy.mybatis.dao.PersonMapper.queryPersonById - <==      Total: 1
---------------2
2019-07-29 23:18:08 DEBUG mybatis.sql.com.cjy.mybatis.dao.PersonMapper.queryPersonById - ==>  Preparing: select id,user_name,marker from person where id = ? 
2019-07-29 23:18:08 DEBUG mybatis.sql.com.cjy.mybatis.dao.PersonMapper.queryPersonById - ==> Parameters: 10(Integer)
2019-07-29 23:18:08 DEBUG mybatis.sql.com.cjy.mybatis.dao.PersonMapper.queryPersonById - <==      Total: 1


下面测试同一个sqlsession的效果,可以看出只发送了一条sql
	@Test
	public void cache1(){
	 SqlSessionFactory sqlSessionFactory = MySqlSessionFacoty.getSqlSessionFactory();
   	 SqlSession openSession = sqlSessionFactory.openSession();
   	 PersonMapper mapper = openSession.getMapper(PersonMapper.class);
   	 System.out.println("---------------");
   	 Person p1 = mapper.queryPersonById(10);
   	 System.out.println("---------------");
   	 Person p2 = mapper.queryPersonById(10);
/**
 * 可以看到中间只发送了一次sql
---------------
2019-07-29 21:29:21 DEBUG mybatis.sql.com.cjy.mybatis.dao.PersonMapper.queryPersonById - ==>  Preparing: select id,user_name,marker from person where id = ? 
2019-07-29 21:29:21 DEBUG mybatis.sql.com.cjy.mybatis.dao.PersonMapper.queryPersonById - ==> Parameters: 10(Integer)
2019-07-29 21:29:21 DEBUG mybatis.sql.com.cjy.mybatis.dao.PersonMapper.queryPersonById - <==      Total: 1
---------------
 */
	}



二级缓存测试:
二级缓存默认是不开启的,需要手动去开启,同时有一些要求:
1.mybatis要求返回的pojo必须是可序列化的,也就是实现serializable接口。
2.配置二级缓存开启,只需要在映射器xml文件配置

<cache/>

3.第一个sqlsession查询后的结果要commit。

代码测试:


//-------------------------由于第一次执行后未commit,所以缓存使用失效
	@Test
	public void cache2(){
		SqlSessionFactory sqlSessionFactory = MySqlSessionFacoty.getSqlSessionFactory();
		SqlSession openSession = sqlSessionFactory.openSession();
		SqlSession openSession2 = sqlSessionFactory.openSession();
		PersonMapper mapper = openSession.getMapper(PersonMapper.class);
		PersonMapper mapper2 = openSession2.getMapper(PersonMapper.class);
		System.out.println("---------------1");
		Person p1 = mapper.queryPersonById(10);
		System.out.println("---------------2");
		Person p2 = mapper2.queryPersonById(10);
		/**
---------------1
2019-07-29 21:30:57 DEBUG mybatis.sql.com.cjy.mybatis.dao.PersonMapper.queryPersonById - ==>  Preparing: select id,user_name,marker from person where id = ? 
2019-07-29 21:30:57 DEBUG mybatis.sql.com.cjy.mybatis.dao.PersonMapper.queryPersonById - ==> Parameters: 10(Integer)
2019-07-29 21:30:57 DEBUG mybatis.sql.com.cjy.mybatis.dao.PersonMapper.queryPersonById - <==      Total: 1
---------------2
2019-07-29 21:31:07 DEBUG mybatis.sql.com.cjy.mybatis.dao.PersonMapper.queryPersonById - ==>  Preparing: select id,user_name,marker from person where id = ? 
2019-07-29 21:31:07 DEBUG mybatis.sql.com.cjy.mybatis.dao.PersonMapper.queryPersonById - ==> Parameters: 10(Integer)
2019-07-29 21:31:07 DEBUG mybatis.sql.com.cjy.mybatis.dao.PersonMapper.queryPersonById - <==      Total: 1
		 */
	}

//-------------------------commit
	@Test
	public void cache3(){
		SqlSessionFactory sqlSessionFactory = MySqlSessionFacoty.getSqlSessionFactory();
		SqlSession openSession = sqlSessionFactory.openSession();
		SqlSession openSession2 = sqlSessionFactory.openSession();
		PersonMapper mapper = openSession.getMapper(PersonMapper.class);
		PersonMapper mapper2 = openSession2.getMapper(PersonMapper.class);
		System.out.println("---------------1");
		Person p1 = mapper.queryPersonById(10);
		
		openSession.commit(); //第一个sqlSession查询完commit才行
		
		System.out.println("---------------2");
		Person p2 = mapper2.queryPersonById(10);
/**
---------------1
2019-07-29 21:39:53 DEBUG mybatis.sql.com.cjy.mybatis.dao.PersonMapper.queryPersonById - ==>  Preparing: select id,user_name,marker from person where id = ? 
2019-07-29 21:39:53 DEBUG mybatis.sql.com.cjy.mybatis.dao.PersonMapper.queryPersonById - ==> Parameters: 10(Integer)
2019-07-29 21:39:53 DEBUG mybatis.sql.com.cjy.mybatis.dao.PersonMapper.queryPersonById - <==      Total: 1
---------------2	 
*/
	}


缓存元素介绍:
五、mybatis缓存_第1张图片



自定义缓存:
系统缓存是mybatis应用机器上的本地缓存,但是在大型服务器上,会使用各类不同的缓存服务器,这个时候我们可以定制缓存,比如现在十分流行的redis缓存。这里先不做这种介绍,可以查看另一篇SpringBoot与缓存使用与原理并debug源码讲解
https://blog.csdn.net/qq_35241080/article/details/88593932
使用的也是mybatis。

你可能感兴趣的:(mybatis)