Springboot整合mybatis開啟二級(jí)緩存的實(shí)現(xiàn)示例
前言
下面大部分內(nèi)容來(lái)源于網(wǎng)上的相關(guān)帖子和官網(wǎng),自己簡(jiǎn)單寫了個(gè)demo體驗(yàn)了下,個(gè)人感覺(jué)mybatis的緩存并不是很合適
查詢做緩存時(shí),遇到更新操作就會(huì)刷新緩存,尤其是多表查詢時(shí),就會(huì)很難控制。對(duì)于那些需要緩存的熱數(shù)據(jù)應(yīng)該抽出來(lái)放到redis上做。
mybatis 一級(jí)緩存和二級(jí)緩存的概念
之所以稱之為“二級(jí)緩存”,是相對(duì)于“一級(jí)緩存”而言的。既然有了一級(jí)緩存,那么為什么要提供二級(jí)緩存呢?我們知道,在一級(jí)緩存中,不同session進(jìn)行相同SQL查詢的時(shí)候,是查詢兩次數(shù)據(jù)庫(kù)的。顯然這是一種浪費(fèi),既然SQL查詢相同,就沒(méi)有必要再次查庫(kù)了,直接利用緩存數(shù)據(jù)即可,這種思想就是MyBatis二級(jí)緩存的初衷。
另外,Spring和MyBatis整合時(shí),每次查詢之后都要進(jìn)行關(guān)閉sqlsession,關(guān)閉之后數(shù)據(jù)被清空。所以MyBatis和Spring整合之后,一級(jí)緩存是沒(méi)有意義的。如果開啟二級(jí)緩存,關(guān)閉sqlsession后,會(huì)把該sqlsession一級(jí)緩存中的數(shù)據(jù)添加到mapper namespace的二級(jí)緩存中。這樣,緩存在sqlsession關(guān)閉之后依然存在。
pom引入依賴
<dependencies> ? ? <dependency> ? ? ? ? <groupId>org.mybatis.spring.boot</groupId> ? ? ? ? <artifactId>mybatis-spring-boot-starter</artifactId> ? ? ? ? <version>2.2.2</version> ? ? </dependency> ? ? <dependency> ? ? ? ? <groupId>mysql</groupId> ? ? ? ? <artifactId>mysql-connector-java</artifactId> ? ? ? ? <scope>runtime</scope> ? ? </dependency> ? ? <dependencies> ? ?
application.properties 文件配置
spring.datasource.type=com.zaxxer.hikari.HikariDataSource #Spring jdbc 數(shù)據(jù)源配置 需要mysql-connector-java驅(qū)動(dòng)依賴 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/gefrm?useUnicode=true&characterEncoding=utf-8&useSSL=false&useInformationSchema=true&serverTimezone=Asia/Shanghai&autoReconnect=true&failOverReadOnly=false spring.datasource.username=root spring.datasource.password=123456 #mybatis配置 mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.example.mybatis_cache.bean mybatis.configuration.map-underscore-to-camel-case=true #mybatis緩存 mybatis.configuration.cache-enabled=true #mybatisSQL執(zhí)行打印 logging.level.com.example.mybatis_cache.mapper=debug
mapper.xml 文件配置
<?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.example.mybatis_cache.mapper.BscDictInfoTestMapper"> ? ? <!-- 這個(gè)cache 是關(guān)鍵 --> ? ? <cache eviction="LRU" flushInterval="100000" readOnly="true" size="1024"/> ? ? <!--可以通過(guò)設(shè)置useCache來(lái)規(guī)定這個(gè)sql是否開啟緩存,ture是開啟,false是關(guān)閉,刷新緩存:flushCache="true" ?useCache="true"--> ? ? <select id="selectAll" resultType="com.example.mybatis_cache.bean.BscDictInfoTestDO"> ? ? ? ? select * ? ? ? ? from bsc_dict_info_test ? ? </select> ? ? <!-- 多個(gè)命名空間緩存共享 級(jí)聯(lián)標(biāo)簽 cache-ref 指定namespace即可 --> ? ? <!--<cache-ref namespace=""/> --> </mapper>
默認(rèn)情況下,只啟用了本地的會(huì)話緩存,它僅僅對(duì)一個(gè)會(huì)話中的數(shù)據(jù)進(jìn)行緩存。 要啟用全局的二級(jí)緩存,只需要在你的 SQL 映射文件中(XXXMapper.xml)添加一行:<cache/>
這個(gè)簡(jiǎn)單語(yǔ)句的效果如下:
- 映射語(yǔ)句文件中的所有 select 語(yǔ)句的結(jié)果將會(huì)被緩存。
- 映射語(yǔ)句文件中的所有 insert、update 和 delete 語(yǔ)句會(huì)刷新緩存。
- 緩存會(huì)使用最近最少使用算法(LRU, Least Recently Used)算法來(lái)清除不需要的緩存。
- 緩存不會(huì)定時(shí)進(jìn)行刷新(也就是說(shuō),沒(méi)有刷新間隔)。
- 緩存會(huì)保存列表或?qū)ο螅o(wú)論查詢方法返回哪種)的 1024 個(gè)引用。
- 緩存會(huì)被視為讀/寫緩存,這意味著獲取到的對(duì)象并不是共享的,可以安全地被調(diào)用者修改,而不干擾其他調(diào)用者或線程所做的潛在修改。
<cache
eviction="FIFO"
flushInterval="60000"
size="512"
readOnly="true"/>
這個(gè)更高級(jí)的配置創(chuàng)建了一個(gè) FIFO 緩存,每隔 60 秒刷新,最多可以存儲(chǔ)結(jié)果對(duì)象或列表的 512 個(gè)引用,而且返回的對(duì)象被認(rèn)為是只讀的,因此對(duì)它們進(jìn)行修改可能會(huì)在不同線程中的調(diào)用者產(chǎn)生沖突。
可用的清除策略有:
LRU – 最近最少使用:移除最長(zhǎng)時(shí)間不被使用的對(duì)象。 FIFO – 先進(jìn)先出:按對(duì)象進(jìn)入緩存的順序來(lái)移除它們。 SOFT – 軟引用:基于垃圾回收器狀態(tài)和軟引用規(guī)則移除對(duì)象。 WEAK –
弱引用:更積極地基于垃圾收集器狀態(tài)和弱引用規(guī)則移除對(duì)象。 默認(rèn)的清除策略是 LRU。
flushInterval(刷新間隔)屬性可以被設(shè)置為任意的正整數(shù),設(shè)置的值應(yīng)該是一個(gè)以毫秒為單位的合理時(shí)間量。 默認(rèn)情況是不設(shè)置,也就是沒(méi)有刷新間隔,緩存僅僅會(huì)在調(diào)用語(yǔ)句時(shí)刷新。
size(引用數(shù)目)屬性可以被設(shè)置為任意正整數(shù),要注意欲緩存對(duì)象的大小和運(yùn)行環(huán)境中可用的內(nèi)存資源。默認(rèn)值是 1024。
readOnly(只讀)屬性可以被設(shè)置為 true 或 false。只讀的緩存會(huì)給所有調(diào)用者返回緩存對(duì)象的相同實(shí)例。 因此這些對(duì)象不能被修改。這就提供了可觀的性能提升。而可讀寫的緩存會(huì)(通過(guò)序列化)返回緩存對(duì)象的拷貝。
速度上會(huì)慢一些,但是更安全,因此默認(rèn)值是 false。
提示:二級(jí)緩存是事務(wù)性的。這意味著,當(dāng) SqlSession 完成并提交時(shí),或是完成并回滾,但沒(méi)有執(zhí)行 flushCache=true 的 insert/delete/update 語(yǔ)句時(shí),緩存會(huì)獲得更新。
cache-ref
回想一下上一節(jié)的內(nèi)容,對(duì)某一命名空間的語(yǔ)句,只會(huì)使用該命名空間的緩存進(jìn)行緩存或刷新。 但你可能會(huì)想要在多個(gè)命名空間中共享相同的緩存配置和實(shí)例。要實(shí)現(xiàn)這種需求,你可以使用 cache-ref 元素來(lái)引用另一個(gè)緩存。
<cache-ref namespace="com.someone.application.data.SomeMapper"/>
完整示例代碼
數(shù)據(jù)庫(kù)表結(jié)構(gòu)
/*
?Navicat Premium Data Transfer
?Source Server ? ? ? ? : localhost_3306
?Source Server Type ? ?: MySQL
?Source Server Version : 80015
?Source Host ? ? ? ? ? : localhost:3306
?Source Schema ? ? ? ? : gefrm
?Target Server Type ? ?: MySQL
?Target Server Version : 80015
?File Encoding ? ? ? ? : 65001
?Date: 17/05/2022 16:02:21
*/
SET NAMES utf8mb4;
SET
FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for bsc_dict_info_test
-- ----------------------------
DROP TABLE IF EXISTS `bsc_dict_info_test`;
CREATE TABLE `bsc_dict_info_test`
(
? ? `dict_id` ? ? ? ?varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci ? NOT NULL DEFAULT ' ' COMMENT '字典標(biāo)識(shí)',
? ? `dict_name` ? ? ?varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci ?NOT NULL DEFAULT ' ' COMMENT '字典名稱',
? ? `dict_sitm_id` ? varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci ? NOT NULL DEFAULT ' ' COMMENT '字典子項(xiàng)標(biāo)識(shí)',
? ? `dict_sitm_name` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci ?NOT NULL DEFAULT ' ' COMMENT '字典子項(xiàng)名稱',
? ? `suse_flag` ? ? ?char(1) CHARACTER SET utf8 COLLATE utf8_general_ci ? ? ? NOT NULL DEFAULT '0' COMMENT '啟用標(biāo)志',
? ? `disp_orde` ? ? ?int(11) NOT NULL DEFAULT 0 COMMENT '展示順序',
? ? `memo` ? ? ? ? ? varchar(4000) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT ' ' COMMENT '備注'
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of bsc_dict_info_test
-- ----------------------------
INSERT INTO `bsc_dict_info_test`
VALUES ('bbbbbtest', '擔(dān)保人/被擔(dān)保人與上市公司關(guān)聯(lián)關(guān)系', '999', '無(wú)關(guān)聯(lián)關(guān)系', '0', 190, '1');
INSERT INTO `bsc_dict_info_test`
VALUES ('aaaaatest', '擔(dān)保人/被擔(dān)保人與上市公司關(guān)聯(lián)關(guān)系', '999', '無(wú)關(guān)聯(lián)關(guān)系', '0', 190, '1');
INSERT INTO `bsc_dict_info_test`
VALUES ('bbbbbtest', '擔(dān)保人/被擔(dān)保人與上市公司關(guān)聯(lián)關(guān)系', '999', '無(wú)關(guān)聯(lián)關(guān)系', '0', 190, '1');
INSERT INTO `bsc_dict_info_test`
VALUES ('aaaaatest', '擔(dān)保人/被擔(dān)保人與上市公司關(guān)聯(lián)關(guān)系', '999', '無(wú)關(guān)聯(lián)關(guān)系', '0', 190, '1');
INSERT INTO `bsc_dict_info_test`
VALUES ('bbbbbtest', '擔(dān)保人/被擔(dān)保人與上市公司關(guān)聯(lián)關(guān)系', '999', '無(wú)關(guān)聯(lián)關(guān)系', '0', 190, '\'0\'');
SET
FOREIGN_KEY_CHECKS = 1;bean 類
package com.example.mybatis_cache.bean;
import lombok.Builder;
import lombok.Data;
import java.io.Serializable;
/**
?* @author 起鳳
?* @description: TODO
?* @date 2022/4/15
?*/
@Data
@Builder
public class BscDictInfoTestDO implements Serializable {
? ? private String dictId;
? ? private String dictName;
? ? private String dictSitmId;
? ? private String dictSitmName;
? ? private String suseFlag;
? ? private String dispOrde;
? ? private String memo;
}mapper
package com.example.mybatis_cache.mapper;
import com.example.mybatis_cache.bean.BscDictInfoTestDO;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
?* @author 起鳳
?* @description: TODO
?* @date 2022/4/15
?*/
@Mapper
public interface BscDictInfoTestMapper {
? ? /**
? ? ?* 查詢所有
? ? ?*
? ? ?* @return
? ? ?*/
? ? List<BscDictInfoTestDO> selectAll();
}service
package com.example.mybatis_cache.service;
/**
?* 測(cè)試服務(wù)
?*
?* @author 起鳳
?* @description: TODO
?* @date 2022/4/15
?*/
public interface BscDictService {
? ? void info(String name, int age);
}impl
package com.example.mybatis_cache.service.impl;
import com.example.mybatis_cache.bean.BscDictInfoTestDO;
import com.example.mybatis_cache.mapper.BscDictInfoTestMapper;
import com.example.mybatis_cache.service.BscDictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
/**
?* @author 起鳳
?* @description: TODO
?* @date 2022/4/15
?*/
@Slf4j
@Component
public class BscDictServiceImpl implements BscDictService {
? ? @Resource
? ? BscDictInfoTestMapper mapper;
? ? @Override
? ? public void info(String name, int age) {
? ? ? ? log.warn("name:{}, age:{}", name, age);
? ? ? ? List<BscDictInfoTestDO> list = mapper.selectAll();
? ? ? ? list.forEach(item -> {
? ? ? ? ? ? log.error("{}", item);
? ? ? ? });
? ? }
}測(cè)試類
package com.example.mybatis_cache.service.impl;
import com.example.mybatis_cache.service.BscDictService;
import com.mysql.cj.xdevapi.SessionFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.*;
/**
?* @author 起鳳
?* @description: TODO
?* @date 2022/5/16
?*/
@Slf4j
@SpringBootTest
class BscDictServiceImplTest {
? ? @Autowired
? ? private BscDictService dictService;
? ? @Test
? ? void levelOneCacheTest() {
? ? ? ? dictService.info("a", 1);
? ? ? ? log.warn("======================== 分割線 ?======================");
? ? ? ? dictService.info("a", 2);
? ? }
? ? @Test
? ? void testRawLevelOne() {
? ? }
}測(cè)試結(jié)果
執(zhí)行2次查詢,只執(zhí)行了一次,第二次命中的是緩存

踩坑
啟動(dòng)拋異常:
Error creating bean with name 'cacheAutoConfigurationValidator' defined in class path resource [org/springframework/boot/autoconfigure/cache/CacheAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No cache manager could be auto-configured, check your configuration (caching type is 'EHCACHE')
解決 添加依賴 spring-boot-starter-cache
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
啟動(dòng)拋異常:
Error creating bean with name 'cacheManager' defined in class path resource [org/springframework/boot/autoconfigure/cache/EhCacheCacheConfiguration.class]: Unsatisfied dependency expressed through method 'cacheManager' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ehCacheCacheManager' defined in class path resource [org/springframework/boot/autoconfigure/cache/EhCacheCacheConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [net.sf.ehcache.CacheManager]: Factory method 'ehCacheCacheManager' threw exception; nested exception is java.lang.IllegalArgumentException: Cache configuration does not exist 'ServletContext resource [/ehcache.xml]'
解決:注意classpath:不能少
spring.cache.ehcache.config=classpath:ehcache.xml
參考資料
MyBatis二級(jí)緩存介紹
mybatis官網(wǎng)中文
MyBatis 二級(jí)緩存全詳解
Spring Cache和EhCache實(shí)現(xiàn)緩存管理
第四篇:SpringBoot中Cache緩存的使用
到此這篇關(guān)于Springboot整合mybatis開啟二級(jí)緩存的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Springboot mybatis二級(jí)緩存內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
偵聽消息隊(duì)列的Message Listener類示例詳解
Spring AMQP 是基于 Spring 框架的AMQP消息解決方案,提供模板化的發(fā)送和接收消息的抽象層,提供基于消息驅(qū)動(dòng)的 POJO的消息監(jiān)聽等,簡(jiǎn)化了我們對(duì)于RabbitMQ相關(guān)程序的開發(fā),本文給大家介紹偵聽消息隊(duì)列的Message Listener類,感興趣的朋友一起看看吧2023-12-12
Java concurrency之CountDownLatch原理和示例_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
CountDownLatch是一個(gè)同步輔助類,在完成一組正在其他線程中執(zhí)行的操作之前,它允許一個(gè)或多個(gè)線程一直等待。 下面通過(guò)本文給大家分享Java concurrency之CountDownLatch原理和示例,需要的的朋友參考下吧2017-06-06
使用注解@Validated效驗(yàn)VO參數(shù)是否合規(guī)
這篇文章主要為大家介紹了使用注解@Validated效驗(yàn)VO參數(shù)是否合規(guī)過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
spring mvc 和ajax異步交互完整實(shí)例代碼
本篇文章主要介紹了spring mvc 和ajax異步交互完整實(shí)例代碼,簡(jiǎn)單的AJAX+SpringMVC的異步交互小例子,有興趣的可以了解一下。2017-02-02
springcloud Zuul動(dòng)態(tài)路由的實(shí)現(xiàn)
這篇文章主要介紹了springcloud Zuul動(dòng)態(tài)路由的實(shí)現(xiàn),詳細(xì)的介紹了什么是Zuu及其動(dòng)態(tài)路由的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
解讀SpringBoot接收List<Bean>參數(shù)問(wèn)題(POST請(qǐng)求方式)
這篇文章主要介紹了解讀SpringBoot接收List<Bean>參數(shù)問(wèn)題(POST請(qǐng)求方式),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
解決Eclipse/STS中出現(xiàn)Resource is out of sync with the file system
今天小編就為大家分享一篇關(guān)于解決Eclipse/STS中出現(xiàn)Resource is out of sync with the file system的異常問(wèn)題,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12

