最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot + Mybatis-plus實(shí)戰(zhàn)之Mybatis-plus的一級(jí)緩存、二級(jí)緩存

 更新時(shí)間:2020年12月01日 14:58:29   作者:愛吃早餐的程序員  
這篇文章主要介紹了SpringBoot + Mybatis-plus實(shí)戰(zhàn)之Mybatis-plus的一級(jí)緩存、二級(jí)緩存,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

前言

現(xiàn)在的JAVA行業(yè),貌似已經(jīng)是SpringBoot + SpringCloud 的天下了,早期的SSH,SSM框架已經(jīng)老去,與SpringBoot相結(jié)合的JPA框架雖然省去了很多的增刪改查sql,但是比較笨拙,在面對一些復(fù)雜多變的邏輯時(shí)常常力不從心,而相對應(yīng)的Mybatis由于其高度的靈活性受到廣大JAVA攻城獅的歡迎。之前整合過了springboot+mybatis,前幾天看到一個(gè)面試的問一個(gè)問題,Mybatis的一級(jí)緩存,二級(jí)緩存。我想這個(gè)應(yīng)該也是一個(gè)重點(diǎn)吧,所以今天決定來詳細(xì)解讀一下神秘的一二級(jí)緩存。

  • 一級(jí)緩存是SqlSession級(jí)別的緩存。在操作數(shù)據(jù)庫時(shí)需要構(gòu)造sqlSession對象,在對象中有一個(gè)數(shù)據(jù)結(jié)構(gòu)(HashMap)用于存儲(chǔ)緩存數(shù)據(jù)。不同的sqlSession之間的緩存數(shù)據(jù)區(qū)域(HashMap)是互相不影響的。 一級(jí)緩存是默認(rèn)開啟的不用配置。
  • 二級(jí)緩存是mapper級(jí)別的緩存,多個(gè)SqlSession去操作同一個(gè)Mapper的sql語句,多個(gè)SqlSession可以共用二級(jí)緩存,二級(jí)緩存是跨SqlSession的。二級(jí)緩存的開啟(實(shí)體類必須序列化),然后在配置文件里面配置。

MyBatis-plus 配置要點(diǎn)
核心要點(diǎn)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依賴外,還有

核心要點(diǎn)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>

核心要點(diǎn)3

mybatis 語句生成 generatorConfig.xml 用它一步生成需要的基本實(shí)體類和接口以及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"/>
      <!-- 從數(shù)據(jù)庫返回的值被清理前后的空格 -->
      <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>

    <!--數(shù)據(jù)庫表-->
    <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>

在這里插入圖片描述

這個(gè) Run Mybatis Generator 可以在eclipse 的插件市場下的

點(diǎn)擊執(zhí)行后生成以下內(nèi)容

在這里插入圖片描述

在這里插入圖片描述

Mybatis-plus 一級(jí)緩存的測試

首先一定要開啟日志 方便查看效果

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("結(jié)果:"+ selectByPrimaryKey.getUsername());
 }

在這里插入圖片描述

結(jié)果是

在這里插入圖片描述

可以看出,只搜索了一次,第二三次都沒有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("結(jié)果:"+ selectByPrimaryKey.getUsername());
		    if (i == 2) {
		    	selectByPrimaryKey.setUsername("劉惜君的妹妹");
		    	testMapper.updateByPrimaryKey(selectByPrimaryKey);
		    	Test selectByPrimaryKey2 = testMapper.selectByPrimaryKey(5);
		    	log.info("更新后的用戶名:"+ selectByPrimaryKey2.getUsername());
				}
	 }

打印結(jié)果:

在這里插入圖片描述

可見,第一次我加入了更新的代碼后再次查詢的時(shí)候,就又執(zhí)行了sql語句,說明當(dāng)執(zhí)行插入、更新、刪除,會(huì)清空SqlSession中的一級(jí)緩存。只有查詢的操作,一級(jí)緩存才不會(huì)被清除。

Mybatis-plus二級(jí)緩存測試

二級(jí)緩存的開啟除了在配置文件中打開開關(guān) 還要在mapper對應(yīng)開啟

在這里插入圖片描述

測試代碼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();
		  }

測試結(jié)果

在這里插入圖片描述

由測試結(jié)果可知,上述代碼第一次查 mapper1.selectByPrimaryKey(5) 的時(shí)候執(zhí)行了sql,然后關(guān)閉了第一個(gè)session 第二次 用別的sqlseeison 去查沒有調(diào)用sql,說明了二級(jí)換粗和sqlseesion 無關(guān),之和mapper有關(guān)。

測試代碼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();
		  }

打印結(jié)果

在這里插入圖片描述

由此可知,做了更新mapper2.updateByPrimaryKey(selectByPrimaryKey); 之后, 二級(jí)緩存才被清空。特性和一級(jí)緩存很類似。

初次之外,我們可以通過userCache是來設(shè)置具體的語句是否禁用二級(jí)緩存

在這里插入圖片描述

重新執(zhí)行 http://localhost:8080/testMybatis2 后的打印結(jié)果

在這里插入圖片描述

可見 selectByPrimaryKey 這個(gè)查詢禁止二級(jí)緩存后,兩次都從數(shù)據(jù)庫里面查了。

 小結(jié)

  • 一級(jí)緩存是默認(rèn)開始的,屬于會(huì)話級(jí)別,一個(gè)會(huì)話做多次做相同查詢會(huì)開啟,如果對查詢的數(shù)據(jù)進(jìn)行更新,刪除等操作時(shí),再次查詢會(huì)從數(shù)據(jù)庫里查而不用一級(jí)緩存。
  • 二級(jí)緩存開啟最重要,請記住三點(diǎn),1.配置文件開啟mybatis-plus.configuration.cache-enabled=true,2.對應(yīng)mapper文件開啟 3.對應(yīng)實(shí)體類實(shí)現(xiàn)Serializable 接口。如果要對某一個(gè)sql語句禁用二級(jí)緩存,則需要在具體的xml 的sql語句定義處加上 useCache=“false” 。另外記住它和會(huì)話無關(guān),和 xml 的 namespace 即具體的mapper 有關(guān)。
  • 在mapper的同一個(gè)namespace中,如果有其它insert、update、delete操作數(shù)據(jù)后需要刷新緩存,如果不執(zhí)行刷新緩存會(huì)出現(xiàn)臟讀。
  • 設(shè)置statement配置中的flushCache=“true” 屬性,可以實(shí)現(xiàn)二級(jí)緩存的刷新,false則可能出現(xiàn)臟讀。openSession.clearCache() 可以實(shí)現(xiàn)對一級(jí)緩存的刷新。

到此這篇關(guān)于SpringBoot + Mybatis-plus實(shí)戰(zhàn)之Mybatis-plus的一級(jí)緩存、二級(jí)緩存的文章就介紹到這了,更多相關(guān)Mybatis-plus一級(jí)緩存、二級(jí)緩存內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Javassist如何操作Java 字節(jié)碼

    Javassist如何操作Java 字節(jié)碼

    這篇文章主要介紹了Javassist如何操作Java 字節(jié)碼,幫助大家更好的理解和學(xué)習(xí)Java,感興趣的朋友可以了解下
    2020-09-09
  • java8中的默認(rèn)垃圾回收器(GC)

    java8中的默認(rèn)垃圾回收器(GC)

    這篇文章主要介紹了java8中的默認(rèn)垃圾回收器(GC),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Java之HashMap案例詳解

    Java之HashMap案例詳解

    這篇文章主要介紹了Java之HashMap案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • 一次因HashSet引起的并發(fā)問題詳解

    一次因HashSet引起的并發(fā)問題詳解

    這篇文章主要給大家介紹了一次因HashSet引起的并發(fā)問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • Spring5使用JSR 330標(biāo)準(zhǔn)注解的方法

    Spring5使用JSR 330標(biāo)準(zhǔn)注解的方法

    從Spring3.0之后,除了Spring自帶的注解,我們也可以使用JSR330的標(biāo)準(zhǔn)注解,本文主要介紹了Spring5使用JSR 330標(biāo)準(zhǔn)注解,感興趣的可以了解一下
    2021-09-09
  • SpringBoot項(xiàng)目使用@Scheduled注解實(shí)現(xiàn)定時(shí)任務(wù)的方法

    SpringBoot項(xiàng)目使用@Scheduled注解實(shí)現(xiàn)定時(shí)任務(wù)的方法

    文章介紹了在SpringBoot項(xiàng)目中使用@Scheduled注解實(shí)現(xiàn)定時(shí)任務(wù)的三種方式:基于注解、基于接口和基于注解設(shè)定多線程定時(shí)任務(wù),詳細(xì)講解了@Scheduled注解的使用方法、各個(gè)參數(shù)以及如何配置動(dòng)態(tài)定時(shí)任務(wù)和多線程定時(shí)任務(wù),感興趣的朋友一起看看吧
    2025-03-03
  • java基于JDBC連接Oracle 11g Release2實(shí)例分析

    java基于JDBC連接Oracle 11g Release2實(shí)例分析

    這篇文章主要介紹了java基于JDBC連接Oracle 11g Release2的方法,實(shí)例分析了JDBC連接Oracle 11g Release2容易出現(xiàn)的異常與解決方法,需要的朋友可以參考下
    2015-06-06
  • MyBatis中的XML實(shí)現(xiàn)和動(dòng)態(tài)SQL實(shí)現(xiàn)示例詳解

    MyBatis中的XML實(shí)現(xiàn)和動(dòng)態(tài)SQL實(shí)現(xiàn)示例詳解

    這篇文章主要介紹了MyBatis中的XML實(shí)現(xiàn)和動(dòng)態(tài)SQL實(shí)現(xiàn),我們可以將XML中重復(fù)出現(xiàn)的內(nèi)容提取出來放到sql標(biāo)簽中,當(dāng)需要用到sql標(biāo)簽中的內(nèi)容時(shí),用include標(biāo)簽將sql標(biāo)簽中的內(nèi)容引進(jìn)來即可,感興趣的朋友跟隨小編一起看看吧
    2024-02-02
  • java操作mongodb實(shí)現(xiàn)CURD功能實(shí)例

    java操作mongodb實(shí)現(xiàn)CURD功能實(shí)例

    mongodb支持多種語言,并且提供了多種語言的驅(qū)動(dòng),本文使用java操作mongodb實(shí)現(xiàn)CURD功能,大家參考使用吧
    2013-12-12
  • JAVA實(shí)現(xiàn)空間索引編碼——GeoHash的示例

    JAVA實(shí)現(xiàn)空間索引編碼——GeoHash的示例

    本篇文章主要介紹了JAVA實(shí)現(xiàn)空間索引編碼——GeoHash的示例,如何從眾多的位置信息中查找到離自己最近的位置,有興趣的朋友可以了解一下
    2016-10-10

最新評(píng)論

朝阳区| 湖南省| 康乐县| 临邑县| 彰武县| 奉化市| 治县。| 黔江区| 胶州市| 海门市| 呼玛县| 托里县| 柞水县| 黔江区| 准格尔旗| 正宁县| 邵阳县| 和龙市| 德令哈市| 河西区| 兰溪市| 旺苍县| 沈阳市| 宣威市| 渑池县| 宝山区| 合江县| 疏附县| 塘沽区| 文水县| 甘南县| 南陵县| 根河市| 彰武县| 孝义市| 酒泉市| 亳州市| 隆化县| 屏东市| 天门市| 正阳县|