SpringBoot + Mybatis-plus实战之Mybatis-plus的一级缓存、二级缓存

 更新时间:2020年12月01日 14:58:29   作者:爱吃早餐的程序员  
这篇文章主要介绍了SpringBoot + Mybatis-plus实战之Mybatis-plus的一级缓存、二级缓存,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

前言

现在的JAVA行业,貌似已经是SpringBoot + SpringCloud 的天下了,早期的SSH,SSM框架已经老去,与SpringBoot相结合的JPA框架虽然省去了很多的增删改查sql,但是比较笨拙,在面对一些复杂多变的逻辑时常常力不从心,而相对应的Mybatis由于其高度的灵活性受到广大JAVA攻城狮的欢迎。之前整合过了springboot+mybatis,前几天看到一个面试的问一个问题,Mybatis的一级缓存,二级缓存。我想这个应该也是一个重点吧,所以今天决定来详细解读一下神秘的一二级缓存。

  • 一级缓存是SqlSession级别的缓存。在操作数据库时需要构造sqlSession对象,在对象中有一个数据结构(HashMap)用于存储缓存数据。不同的sqlSession之间的缓存数据区域(HashMap)是互相不影响的。 一级缓存是默认开启的不用配置。
  • 二级缓存是mapper级别的缓存,多个SqlSession去操作同一个Mapper的sql语句,多个SqlSession可以共用二级缓存,二级缓存是跨SqlSession的。二级缓存的开启(实体类必须序列化),然后在配置文件里面配置。

MyBatis-plus 配置要点
核心要点1

mybatis-plus 在springboot 中的核心配置如下

mybatis-plus.configuration.cache-enabled=true
mybatis-plus.mapper-locations=classpath*:/mapper/*.xml
mybatis-plus.type-aliases-package=com.sch.app.mybatis.entity
logging.level.com.sch.app.mybatis.mapper= debug

所需依赖 除了基本的springboot依赖外,还有

核心要点2

<dependency>
		  <groupId>com.baomidou</groupId>
		  <artifactId>mybatis-plus-boot-starter</artifactId>
		  <version>3.3.2</version>
		</dependency>		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-cache</artifactId>
		</dependency>
			<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>

核心要点3

mybatis 语句生成 generatorConfig.xml 用它一步生成需要的基本实体类和接口以及mapper文件(resouses目录下)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
  <!-- <properties resource="mybatis.properties" />
     -->
  <classPathEntry location="D:\AJava\mysql-connector-java-8.0.16.jar" />
  <context id="msqlTables" targetRuntime="MyBatis3">
    <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
    <jdbcConnection connectionURL="jdbc:mysql://localhost:3306/alexshi?serverTimezone=GMT%2B8"
            driverClass="com.mysql.cj.jdbc.Driver" password="1234" userId="root" >

      <property name="nullCatalogMeansCurrent" value="true"/>
    </jdbcConnection>
    <javaTypeResolver>
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>
    <javaModelGenerator targetPackage="com.sch.app.mybatis.entity" targetProject="SpringbootMybatis\src\main\java">
      <property name="enableSubPackages" value="true"/>
      <!-- 从数据库返回的值被清理前后的空格 -->
      <property name="trimStrings" value="true" />
    </javaModelGenerator>
    <sqlMapGenerator targetPackage="mapper" targetProject="SpringbootMybatis\src\main\resources">
      <property name="enableSubPackages" value="true"/>
    </sqlMapGenerator>
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.sch.app.mybatis.mapper" targetProject="SpringbootMybatis\src\main\java">
      <property name="enableSubPackages" value="true"/>
    </javaClientGenerator>

    <!--数据库表-->
    <table schema="" tableName="d_dictionary"></table>
    <table schema="" tableName="d_dictionary_type"></table>
    <table schema="" tableName="c_resource"></table>
    <table schema="" tableName="c_role"></table>
    <table schema="" tableName="c_role_resource"></table>
    <table schema="" tableName="c_user_online"></table>
    <table schema="" tableName="c_user"></table>
    <table schema="" tableName="c_user_role"></table>
    <table schema="" tableName="test"></table>
  </context>
</generatorConfiguration>

在这里插入图片描述

这个 Run Mybatis Generator 可以在eclipse 的插件市场下的

点击执行后生成以下内容

在这里插入图片描述

在这里插入图片描述

Mybatis-plus 一级缓存的测试

首先一定要开启日志 方便查看效果

logging.level.com.sch.app.mybatis.mapper= debug

com.sch.app.mybatis.mapper 也就是 mapper接口的目录

在这里插入图片描述

测试代码1

@Autowired
private SqlSessionFactory sqlSessionFactory;

 @RequestMapping(value = "/testMybatis")
 @ResponseBody
 public void testMybatis(){
	 SqlSession sqlSession = sqlSessionFactory.openSession();
	 TestMapper testMapper = sqlSession.getMapper(TestMapper.class);
	  for (int i = 0; i < 3; i++) {
	    Test selectByPrimaryKey = testMapper.selectByPrimaryKey(5);
	    log.info("结果:"+ selectByPrimaryKey.getUsername());
 }

在这里插入图片描述

结果是

在这里插入图片描述

可以看出,只搜索了一次,第二三次都没有sql打印

测试代码2

@RequestMapping(value = "/testMybatis")
	 @ResponseBody
	 public void testMybatis(){
		 SqlSession sqlSession = sqlSessionFactory.openSession();
		 TestMapper testMapper = sqlSession.getMapper(TestMapper.class);
		  for (int i = 0; i < 3; i++) {
		    Test selectByPrimaryKey = testMapper.selectByPrimaryKey(5);
		    log.info("结果:"+ selectByPrimaryKey.getUsername());
		    if (i == 2) {
		    	selectByPrimaryKey.setUsername("刘惜君的妹妹");
		    	testMapper.updateByPrimaryKey(selectByPrimaryKey);
		    	Test selectByPrimaryKey2 = testMapper.selectByPrimaryKey(5);
		    	log.info("更新后的用户名:"+ selectByPrimaryKey2.getUsername());
				}
	 }

打印结果:

在这里插入图片描述

可见,第一次我加入了更新的代码后再次查询的时候,就又执行了sql语句,说明当执行插入、更新、删除,会清空SqlSession中的一级缓存。只有查询的操作,一级缓存才不会被清除。

Mybatis-plus二级缓存测试

二级缓存的开启除了在配置文件中打开开关 还要在mapper对应开启

在这里插入图片描述

测试代码1

@RequestMapping(value = "/testMybatis2")
		  @ResponseBody
		  public void testMybatis2(){
		  	SqlSession openSession1 = sqlSessionFactory.openSession();
		  	SqlSession openSession2 = sqlSessionFactory.openSession();
		  	TestMapper mapper1 = openSession1.getMapper(TestMapper.class);
		  	TestMapper mapper2 = openSession2.getMapper(TestMapper.class);
		  	Test selectByPrimaryKey = mapper1.selectByPrimaryKey(5);
		  	System.out.println(selectByPrimaryKey.getUsername());
		  	openSession1.close();
		  	Test selectByPrimaryKey2 = mapper2.selectByPrimaryKey(5);
		  	System.out.println(selectByPrimaryKey2.getUsername());
		  	openSession2.close();
		  }

测试结果

在这里插入图片描述

由测试结果可知,上述代码第一次查 mapper1.selectByPrimaryKey(5) 的时候执行了sql,然后关闭了第一个session 第二次 用别的sqlseeison 去查没有调用sql,说明了二级换粗和sqlseesion 无关,之和mapper有关。

测试代码2

	@RequestMapping(value = "/testMybatis3")
		  @ResponseBody
		  public void testMybatis3(){
		  	SqlSession openSession1 = sqlSessionFactory.openSession();
		  	SqlSession openSession2 = sqlSessionFactory.openSession();
		  	SqlSession openSession3 = sqlSessionFactory.openSession();
		  	TestMapper mapper1 = openSession1.getMapper(TestMapper.class);
		  	TestMapper mapper2 = openSession2.getMapper(TestMapper.class);
		  	TestMapper mapper3 = openSession3.getMapper(TestMapper.class);
		  	Test selectByPrimaryKey = mapper1.selectByPrimaryKey(5);
		  	System.out.println(selectByPrimaryKey.getUsername());
		  	openSession1.close();
		  	selectByPrimaryKey.setUsername("刘惜君的姐姐");
		  	mapper2.updateByPrimaryKey(selectByPrimaryKey);
		  	openSession2.commit();
		  	
		  	Test selectByPrimaryKey3 = mapper3.selectByPrimaryKey(5);
		  	System.out.println(selectByPrimaryKey3.getUsername());
		  	openSession3.close();
		  }

打印结果

在这里插入图片描述

由此可知,做了更新mapper2.updateByPrimaryKey(selectByPrimaryKey); 之后, 二级缓存才被清空。特性和一级缓存很类似。

初次之外,我们可以通过userCache是来设置具体的语句是否禁用二级缓存

在这里插入图片描述

重新执行 http://localhost:8080/testMybatis2 后的打印结果

在这里插入图片描述

可见 selectByPrimaryKey 这个查询禁止二级缓存后,两次都从数据库里面查了。

 小结

  • 一级缓存是默认开始的,属于会话级别,一个会话做多次做相同查询会开启,如果对查询的数据进行更新,删除等操作时,再次查询会从数据库里查而不用一级缓存。
  • 二级缓存开启最重要,请记住三点,1.配置文件开启mybatis-plus.configuration.cache-enabled=true,2.对应mapper文件开启 3.对应实体类实现Serializable 接口。如果要对某一个sql语句禁用二级缓存,则需要在具体的xml 的sql语句定义处加上 useCache=“false” 。另外记住它和会话无关,和 xml 的 namespace 即具体的mapper 有关。
  • 在mapper的同一个namespace中,如果有其它insert、update、delete操作数据后需要刷新缓存,如果不执行刷新缓存会出现脏读。
  • 设置statement配置中的flushCache=“true” 属性,可以实现二级缓存的刷新,false则可能出现脏读。openSession.clearCache() 可以实现对一级缓存的刷新。

到此这篇关于SpringBoot + Mybatis-plus实战之Mybatis-plus的一级缓存、二级缓存的文章就介绍到这了,更多相关Mybatis-plus一级缓存、二级缓存内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Javassist如何操作Java 字节码

    Javassist如何操作Java 字节码

    这篇文章主要介绍了Javassist如何操作Java 字节码,帮助大家更好的理解和学习Java,感兴趣的朋友可以了解下
    2020-09-09
  • java8中的默认垃圾回收器(GC)

    java8中的默认垃圾回收器(GC)

    这篇文章主要介绍了java8中的默认垃圾回收器(GC),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-05-05
  • Java之HashMap案例详解

    Java之HashMap案例详解

    这篇文章主要介绍了Java之HashMap案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • 一次因HashSet引起的并发问题详解

    一次因HashSet引起的并发问题详解

    这篇文章主要给大家介绍了一次因HashSet引起的并发问题的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-11-11
  • Spring5使用JSR 330标准注解的方法

    Spring5使用JSR 330标准注解的方法

    从Spring3.0之后,除了Spring自带的注解,我们也可以使用JSR330的标准注解,本文主要介绍了Spring5使用JSR 330标准注解,感兴趣的可以了解一下
    2021-09-09
  • SpringBoot项目使用@Scheduled注解实现定时任务的方法

    SpringBoot项目使用@Scheduled注解实现定时任务的方法

    文章介绍了在SpringBoot项目中使用@Scheduled注解实现定时任务的三种方式:基于注解、基于接口和基于注解设定多线程定时任务,详细讲解了@Scheduled注解的使用方法、各个参数以及如何配置动态定时任务和多线程定时任务,感兴趣的朋友一起看看吧
    2025-03-03
  • java基于JDBC连接Oracle 11g Release2实例分析

    java基于JDBC连接Oracle 11g Release2实例分析

    这篇文章主要介绍了java基于JDBC连接Oracle 11g Release2的方法,实例分析了JDBC连接Oracle 11g Release2容易出现的异常与解决方法,需要的朋友可以参考下
    2015-06-06
  • MyBatis中的XML实现和动态SQL实现示例详解

    MyBatis中的XML实现和动态SQL实现示例详解

    这篇文章主要介绍了MyBatis中的XML实现和动态SQL实现,我们可以将XML中重复出现的内容提取出来放到sql标签中,当需要用到sql标签中的内容时,用include标签将sql标签中的内容引进来即可,感兴趣的朋友跟随小编一起看看吧
    2024-02-02
  • java操作mongodb实现CURD功能实例

    java操作mongodb实现CURD功能实例

    mongodb支持多种语言,并且提供了多种语言的驱动,本文使用java操作mongodb实现CURD功能,大家参考使用吧
    2013-12-12
  • JAVA实现空间索引编码——GeoHash的示例

    JAVA实现空间索引编码——GeoHash的示例

    本篇文章主要介绍了JAVA实现空间索引编码——GeoHash的示例,如何从众多的位置信息中查找到离自己最近的位置,有兴趣的朋友可以了解一下
    2016-10-10

最新评论