SpringBoot使用Redis緩存MySql的方法步驟
1 項目組成
- 應(yīng)用:springboot rest api
- 數(shù)據(jù)庫:mysql
- jdbc框架:jpa
- 緩存中間件:redis
2 運行springboot
2.1 官網(wǎng)download最基本的restful應(yīng)用
教程地址:https://spring.io/guides/gs/rest-service/
直接download成品,找到git命令 :git clone https://github.com/spring-guides/gs-rest-service.git
創(chuàng)建一個文件夾,打開git bash here(安裝git)

Idea打開成品 (complete文件夾)

2.2 運行應(yīng)用
gradle -> bootRun右鍵 -> Run/Deubg

通過http://localhost:8080/greeting?name=lanxingisthebest訪問
3 訪問mysql
增加gradle依賴 (通過jpa)
implementation(‘mysql:mysql-connector-java') implementation(‘org.springframework.boot:spring-boot-starter-data-jpa')
增加配置文件及數(shù)據(jù)庫配置
創(chuàng)建文件application.yml

spring: datasource: url: jdbc:mysql://localhost:3306/user_info username: root password: root jpa: show-sql: true
類調(diào)整



mysql insert一條數(shù)據(jù),然后通過 http://localhost:8080/listAllUser 查詢數(shù)據(jù)庫
4 設(shè)置redis緩存
增加gradle依賴
implementation(‘org.springframework.boot:spring-boot-starter-data-redis') implementation(‘org.springframework.boot:spring-boot-starter-cache')
配置文件配置redis參數(shù)
spring:
datasource:
url: jdbc:mysql://localhost:3306/user_info
username: root
password: root
jpa:
show-sql: true
## Redis 配置
redis:
## Redis數(shù)據(jù)庫索引(默認為0)
database: 0
## Redis服務(wù)器地址
host: localhost
## Redis服務(wù)器連接端口
port: 6379
## Redis服務(wù)器連接密碼(默認為空)
password:
jedis:
pool:
## 連接池最大連接數(shù)(使用負值表示沒有限制)
#spring.redis.pool.max-active=8
max-active: 8
## 連接池最大阻塞等待時間(使用負值表示沒有限制)
#spring.redis.pool.max-wait=-1
max-wait: -1
## 連接池中的最大空閑連接
#spring.redis.pool.max-idle=8
max-idle: 8
## 連接池中的最小空閑連接
#spring.redis.pool.min-idle=0
min-idle: 0
## 連接超時時間(毫秒)
timeout: 1200
Redis配置類

RedisConfig代碼
package com.example.restservice.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
/**
* @author lzh
* create 2019-09-24-15:07
*/
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
/**
* 選擇redis作為默認緩存工具
* @param redisConnectionFactory
* @return
*/
/*@Bean
//springboot 1.xx
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
return rcm;
}*/
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofHours(1)); // 設(shè)置緩存有效期一小時
return RedisCacheManager
.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
.cacheDefaults(redisCacheConfiguration).build();
}
/**
* retemplate相關(guān)配置
* @param factory
* @return
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 配置連接工廠
template.setConnectionFactory(factory);
//使用Jackson2JsonRedisSerializer來序列化和反序列化redis的value值(默認使用JDK的序列化方式)
Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
// 指定要序列化的域,field,get和set,以及修飾符范圍,ANY是都有包括private和public
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 指定序列化輸入的類型,類必須是非final修飾的,final修飾的類,比如String,Integer等會跑出異常
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jacksonSeial.setObjectMapper(om);
// 值采用json序列化
template.setValueSerializer(jacksonSeial);
//使用StringRedisSerializer來序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
// 設(shè)置hash key 和value序列化模式
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(jacksonSeial);
template.afterPropertiesSet();
return template;
}
/**
* 對hash類型的數(shù)據(jù)操作
*
* @param redisTemplate
* @return
*/
@Bean
public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForHash();
}
/**
* 對redis字符串類型數(shù)據(jù)操作
*
* @param redisTemplate
* @return
*/
@Bean
public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForValue();
}
/**
* 對鏈表類型的數(shù)據(jù)操作
*
* @param redisTemplate
* @return
*/
@Bean
public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForList();
}
/**
* 對無序集合類型的數(shù)據(jù)操作
*
* @param redisTemplate
* @return
*/
@Bean
public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForSet();
}
/**
* 對有序集合類型的數(shù)據(jù)操作
*
* @param redisTemplate
* @return
*/
@Bean
public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForZSet();
}
}
代碼通過@Cacheable使用 redis緩存

訪問接口后,通過redis工具查詢數(shù)據(jù)
點擊 redis-lic.exe
命令 keys *

到此這篇關(guān)于SpringBoot使用Redis緩存MySql的方法步驟的文章就介紹到這了,更多相關(guān)SpringBoot Redis緩存MySql內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot項目中使用redis緩存的方法步驟
- SpringBoot 開啟Redis緩存及使用方法
- springboot+mybatis+redis 二級緩存問題實例詳解
- SpringBoot AOP控制Redis自動緩存和更新的示例
- SpringBoot2整合Redis緩存三步驟代碼詳解
- SpringBoot+SpringCache實現(xiàn)兩級緩存(Redis+Caffeine)
- 詳解SpringBoot集成Redis來實現(xiàn)緩存技術(shù)方案
- SpringBoot使用Redis緩存的實現(xiàn)方法
- springboot使用shiro-整合redis作為緩存的操作
- SpringBoot redis分布式緩存實現(xiàn)過程解析
- SpringBoot Redis緩存數(shù)據(jù)實現(xiàn)解析
- SpringBoot結(jié)合Redis實現(xiàn)緩存
相關(guān)文章
Java實現(xiàn)XML與JSON的互相轉(zhuǎn)換詳解
這篇文章主要為大家詳細介紹了如何使用Java實現(xiàn)XML與JSON的互相轉(zhuǎn)換,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2025-03-03
Java 實戰(zhàn)項目錘煉之小區(qū)物業(yè)管理系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實現(xiàn)一個小區(qū)物業(yè)管理系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11
java中Memcached的使用實例(包括與Spring整合)
這篇文章主要介紹了java中Memcached的使用實例(包括與Spring整合),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
MyBatis二級緩存實現(xiàn)關(guān)聯(lián)刷新
本文主要介紹了MyBatis二級緩存實現(xiàn)關(guān)聯(lián)刷新,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-01-01
用java實現(xiàn)的獲取優(yōu)酷等視頻縮略圖的實現(xiàn)代碼
想獲取優(yōu)酷等視頻縮略圖,在網(wǎng)上沒有找到滿意的資料,參考了huangdijia的PHP版工具一些思路,寫了下面的JAVA版代碼。。其實也可以做成JS版的2013-05-05

