SpringBoot使用Redis緩存MySql的方法步驟
1 項(xiàng)目組成
- 應(yīng)用:springboot rest api
- 數(shù)據(jù)庫:mysql
- jdbc框架:jpa
- 緩存中間件:redis
2 運(yùn)行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)建一個(gè)文件夾,打開git bash here(安裝git)

Idea打開成品 (complete文件夾)

2.2 運(yùn)行應(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ù)庫索引(默認(rèn)為0)
database: 0
## Redis服務(wù)器地址
host: localhost
## Redis服務(wù)器連接端口
port: 6379
## Redis服務(wù)器連接密碼(默認(rèn)為空)
password:
jedis:
pool:
## 連接池最大連接數(shù)(使用負(fù)值表示沒有限制)
#spring.redis.pool.max-active=8
max-active: 8
## 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒有限制)
#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
## 連接超時(shí)時(shí)間(毫秒)
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作為默認(rèn)緩存工具
* @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è)置緩存有效期一小時(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值(默認(rèn)使用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等會(huì)跑出異常
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;
}
/**
* 對(duì)hash類型的數(shù)據(jù)操作
*
* @param redisTemplate
* @return
*/
@Bean
public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForHash();
}
/**
* 對(duì)redis字符串類型數(shù)據(jù)操作
*
* @param redisTemplate
* @return
*/
@Bean
public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForValue();
}
/**
* 對(duì)鏈表類型的數(shù)據(jù)操作
*
* @param redisTemplate
* @return
*/
@Bean
public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForList();
}
/**
* 對(duì)無序集合類型的數(shù)據(jù)操作
*
* @param redisTemplate
* @return
*/
@Bean
public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForSet();
}
/**
* 對(duì)有序集合類型的數(shù)據(jù)操作
*
* @param redisTemplate
* @return
*/
@Bean
public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForZSet();
}
}
代碼通過@Cacheable使用 redis緩存

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

到此這篇關(guān)于SpringBoot使用Redis緩存MySql的方法步驟的文章就介紹到這了,更多相關(guān)SpringBoot Redis緩存MySql內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot項(xiàng)目中使用redis緩存的方法步驟
- SpringBoot 開啟Redis緩存及使用方法
- springboot+mybatis+redis 二級(jí)緩存問題實(shí)例詳解
- SpringBoot AOP控制Redis自動(dòng)緩存和更新的示例
- SpringBoot2整合Redis緩存三步驟代碼詳解
- SpringBoot+SpringCache實(shí)現(xiàn)兩級(jí)緩存(Redis+Caffeine)
- 詳解SpringBoot集成Redis來實(shí)現(xiàn)緩存技術(shù)方案
- SpringBoot使用Redis緩存的實(shí)現(xiàn)方法
- springboot使用shiro-整合redis作為緩存的操作
- SpringBoot redis分布式緩存實(shí)現(xiàn)過程解析
- SpringBoot Redis緩存數(shù)據(jù)實(shí)現(xiàn)解析
- SpringBoot結(jié)合Redis實(shí)現(xiàn)緩存
相關(guān)文章
解決idea啟動(dòng)報(bào)錯(cuò)javax.imageio.IIOException的問題
這篇文章主要介紹了idea啟動(dòng)報(bào)錯(cuò)javax.imageio.IIOException,解決打不開idea問題,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Java實(shí)現(xiàn)常見排序算法的優(yōu)化
今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識(shí),文章圍繞著Java實(shí)現(xiàn)常見排序算法的優(yōu)化展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-01-01
Feign+mybatisplus搭建項(xiàng)目遇到的坑及解決
這篇文章主要介紹了Feign+mybatisplus搭建項(xiàng)目遇到的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Java的帶GUI界面猜數(shù)字游戲的實(shí)現(xiàn)示例
這篇文章主要介紹了Java的帶GUI界面猜數(shù)字游戲的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
mybatis同一張表多次連接查詢相同列賦值問題小結(jié)
這篇文章主要介紹了mybatis同一張表多次連接查詢相同列賦值問題,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下2017-01-01
springboot項(xiàng)目事務(wù)標(biāo)簽驗(yàn)證
本文主要介紹了springboot項(xiàng)目事務(wù)標(biāo)簽驗(yàn)證,文中通過示例代碼介紹的非常詳細(xì),詳細(xì)的介紹了不加事務(wù)標(biāo)簽和加事物標(biāo)簽的使用,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
解析Spring中@Controller@Service等線程安全問題
這篇文章主要為大家介紹解析了Spring中@Controller@Service等線程的安全問題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03

