Springboot整合mybatis開啟二級緩存的實現(xiàn)示例
前言
下面大部分內(nèi)容來源于網(wǎng)上的相關帖子和官網(wǎng),自己簡單寫了個demo體驗了下,個人感覺mybatis的緩存并不是很合適
查詢做緩存時,遇到更新操作就會刷新緩存,尤其是多表查詢時,就會很難控制。對于那些需要緩存的熱數(shù)據(jù)應該抽出來放到redis上做。
mybatis 一級緩存和二級緩存的概念
之所以稱之為“二級緩存”,是相對于“一級緩存”而言的。既然有了一級緩存,那么為什么要提供二級緩存呢?我們知道,在一級緩存中,不同session進行相同SQL查詢的時候,是查詢兩次數(shù)據(jù)庫的。顯然這是一種浪費,既然SQL查詢相同,就沒有必要再次查庫了,直接利用緩存數(shù)據(jù)即可,這種思想就是MyBatis二級緩存的初衷。
另外,Spring和MyBatis整合時,每次查詢之后都要進行關閉sqlsession,關閉之后數(shù)據(jù)被清空。所以MyBatis和Spring整合之后,一級緩存是沒有意義的。如果開啟二級緩存,關閉sqlsession后,會把該sqlsession一級緩存中的數(shù)據(jù)添加到mapper namespace的二級緩存中。這樣,緩存在sqlsession關閉之后依然存在。
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ū)動依賴 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"> ? ? <!-- 這個cache 是關鍵 --> ? ? <cache eviction="LRU" flushInterval="100000" readOnly="true" size="1024"/> ? ? <!--可以通過設置useCache來規(guī)定這個sql是否開啟緩存,ture是開啟,false是關閉,刷新緩存:flushCache="true" ?useCache="true"--> ? ? <select id="selectAll" resultType="com.example.mybatis_cache.bean.BscDictInfoTestDO"> ? ? ? ? select * ? ? ? ? from bsc_dict_info_test ? ? </select> ? ? <!-- 多個命名空間緩存共享 級聯(lián)標簽 cache-ref 指定namespace即可 --> ? ? <!--<cache-ref namespace=""/> --> </mapper>
默認情況下,只啟用了本地的會話緩存,它僅僅對一個會話中的數(shù)據(jù)進行緩存。 要啟用全局的二級緩存,只需要在你的 SQL 映射文件中(XXXMapper.xml)添加一行:<cache/>
這個簡單語句的效果如下:
- 映射語句文件中的所有 select 語句的結(jié)果將會被緩存。
- 映射語句文件中的所有 insert、update 和 delete 語句會刷新緩存。
- 緩存會使用最近最少使用算法(LRU, Least Recently Used)算法來清除不需要的緩存。
- 緩存不會定時進行刷新(也就是說,沒有刷新間隔)。
- 緩存會保存列表或?qū)ο螅o論查詢方法返回哪種)的 1024 個引用。
- 緩存會被視為讀/寫緩存,這意味著獲取到的對象并不是共享的,可以安全地被調(diào)用者修改,而不干擾其他調(diào)用者或線程所做的潛在修改。
<cache
eviction="FIFO"
flushInterval="60000"
size="512"
readOnly="true"/>
這個更高級的配置創(chuàng)建了一個 FIFO 緩存,每隔 60 秒刷新,最多可以存儲結(jié)果對象或列表的 512 個引用,而且返回的對象被認為是只讀的,因此對它們進行修改可能會在不同線程中的調(diào)用者產(chǎn)生沖突。
可用的清除策略有:
LRU – 最近最少使用:移除最長時間不被使用的對象。 FIFO – 先進先出:按對象進入緩存的順序來移除它們。 SOFT – 軟引用:基于垃圾回收器狀態(tài)和軟引用規(guī)則移除對象。 WEAK –
弱引用:更積極地基于垃圾收集器狀態(tài)和弱引用規(guī)則移除對象。 默認的清除策略是 LRU。
flushInterval(刷新間隔)屬性可以被設置為任意的正整數(shù),設置的值應該是一個以毫秒為單位的合理時間量。 默認情況是不設置,也就是沒有刷新間隔,緩存僅僅會在調(diào)用語句時刷新。
size(引用數(shù)目)屬性可以被設置為任意正整數(shù),要注意欲緩存對象的大小和運行環(huán)境中可用的內(nèi)存資源。默認值是 1024。
readOnly(只讀)屬性可以被設置為 true 或 false。只讀的緩存會給所有調(diào)用者返回緩存對象的相同實例。 因此這些對象不能被修改。這就提供了可觀的性能提升。而可讀寫的緩存會(通過序列化)返回緩存對象的拷貝。
速度上會慢一些,但是更安全,因此默認值是 false。
提示:二級緩存是事務性的。這意味著,當 SqlSession 完成并提交時,或是完成并回滾,但沒有執(zhí)行 flushCache=true 的 insert/delete/update 語句時,緩存會獲得更新。
cache-ref
回想一下上一節(jié)的內(nèi)容,對某一命名空間的語句,只會使用該命名空間的緩存進行緩存或刷新。 但你可能會想要在多個命名空間中共享相同的緩存配置和實例。要實現(xiàn)這種需求,你可以使用 cache-ref 元素來引用另一個緩存。
<cache-ref namespace="com.someone.application.data.SomeMapper"/>
完整示例代碼
數(shù)據(jù)庫表結(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 '字典標識',
? ? `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 '字典子項標識',
? ? `dict_sitm_name` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci ?NOT NULL DEFAULT ' ' COMMENT '字典子項名稱',
? ? `suse_flag` ? ? ?char(1) CHARACTER SET utf8 COLLATE utf8_general_ci ? ? ? NOT NULL DEFAULT '0' COMMENT '啟用標志',
? ? `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', '擔保人/被擔保人與上市公司關聯(lián)關系', '999', '無關聯(lián)關系', '0', 190, '1');
INSERT INTO `bsc_dict_info_test`
VALUES ('aaaaatest', '擔保人/被擔保人與上市公司關聯(lián)關系', '999', '無關聯(lián)關系', '0', 190, '1');
INSERT INTO `bsc_dict_info_test`
VALUES ('bbbbbtest', '擔保人/被擔保人與上市公司關聯(lián)關系', '999', '無關聯(lián)關系', '0', 190, '1');
INSERT INTO `bsc_dict_info_test`
VALUES ('aaaaatest', '擔保人/被擔保人與上市公司關聯(lián)關系', '999', '無關聯(lián)關系', '0', 190, '1');
INSERT INTO `bsc_dict_info_test`
VALUES ('bbbbbtest', '擔保人/被擔保人與上市公司關聯(lián)關系', '999', '無關聯(liá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;
/**
?* 測試服務
?*
?* @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);
? ? ? ? });
? ? }
}測試類
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() {
? ? }
}測試結(jié)果
執(zhí)行2次查詢,只執(zhí)行了一次,第二次命中的是緩存

踩坑
啟動拋異常:
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>
啟動拋異常:
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二級緩存介紹
mybatis官網(wǎng)中文
MyBatis 二級緩存全詳解
Spring Cache和EhCache實現(xiàn)緩存管理
第四篇:SpringBoot中Cache緩存的使用
到此這篇關于Springboot整合mybatis開啟二級緩存的實現(xiàn)示例的文章就介紹到這了,更多相關Springboot mybatis二級緩存內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java數(shù)據(jù)結(jié)構(gòu)之有向圖設計與實現(xiàn)詳解
有向圖是具有方向性的圖,由一組頂點和一組有方向的邊組成,每條方向的邊都連著一對有序的頂點。本文為大家介紹的是有向圖的設計與實現(xiàn),需要的可以參考一下2022-11-11
springboot中如何配置LocalDateTime JSON返回時間戳
這篇文章主要介紹了springboot中如何配置LocalDateTime JSON返回時間戳問題。具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
SpringBoot中@EnableAutoConfiguration注解源碼分析
這篇文章主要介紹了SpringBoot中@EnableAutoConfiguration注解源碼分析,@EnableAutoConfiguration,主要是用于加載Starter目錄包之外的、需要Spring自動生成Bean對象的、帶有@Configuration注解的類,需要的朋友可以參考下2023-08-08

