網站首頁 編程語言 正文
我的項目版本號如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
首先選一個緩存框架(EhCache )EhCache 是一個廣泛使用的開源 Java 分布式緩存庫,主要用于提高應用程序的性能,減少數據庫訪問次數,通過緩存頻繁讀取的數據來實現。它可以作為 Hibernate、Spring、MyBatis 等框架的緩存提供者,用于提升這些框架在數據處理方面的性能。可以緩存來自數據庫的數據,當應用程序需要這些數據時,可以直接從緩存中讀取,而不是每次都查詢數據庫。這減少了數據庫的訪問壓力和響應時間。
第一步導入依賴
<!-- mybatis 二級緩存使用-->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.8</version>
</dependency>
第二步編寫配置文件
src/main/resources
目錄下創建一個名為 ehcache.xml
的 EhCache 配置文件。這個文件用于定義緩存的具體參數,例如緩存策略、生命周期等:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.ehcache.org/ehcache.xsd">
<cache name="default"
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU">
</cache>
</ehcache>
?第三步加注解
對于 Mapper 接口,可以通過在接口上添加 @CacheNamespace
注解開啟二級緩存
如下:
import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Mapper;
@Mapper
@CacheNamespace
public interface YourMapper {
// 方法定義
}
?第四步修改配置文件
application.properties
spring.cache.type=ehcache
spring.cache.ehcache.config=classpath:ehcache.xml
application.yml
spring:
cache:
type: ehcache
ehcache:
config: classpath:ehcache.xml
原文鏈接:https://blog.csdn.net/m0_71507863/article/details/136113026
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2023-01-12 Android?RecyclerChart其它圖表繪制示例詳解_Android
- 2022-05-14 Python函數中的作用域規則詳解_python
- 2023-07-18 A component required a bean of type ‘xxx‘ that cou
- 2022-01-08 出現WARN org.springframework.web.servlet.PageNotFoun
- 2022-07-26 ES對比兩個索引的數據差
- 2022-10-30 淺析pytest?鉤子函數?之初始鉤子和引導鉤子_python
- 2022-10-13 Python?lambda?匿名函數優點和局限性深度總結_python
- 2022-04-27 一篇文章了解正則表達式的替換技巧_正則表達式
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支