欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot使用Redis緩存MySql的方法步驟

 更新時(shí)間:2022年02月22日 11:05:54   作者:lanxing_thk  
本文主要介紹了SpringBoot使用Redis緩存MySql的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

1 項(xiàng)目組成

  • 應(yīng)用:springboot rest api
  • 數(shù)據(jù)庫(kù):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è)文件夾,打開(kāi)git bash here(安裝git)

在這里插入圖片描述

Idea打開(kāi)成品 (complete文件夾)

在這里插入圖片描述

2.2 運(yùn)行應(yīng)用

gradle -> bootRun右鍵 -> Run/Deubg

在這里插入圖片描述

通過(guò)http://localhost:8080/greeting?name=lanxingisthebest訪問(wèn)

3 訪問(wèn)mysql

增加gradle依賴 (通過(guò)jpa)

implementation(‘mysql:mysql-connector-java')
implementation(‘org.springframework.boot:spring-boot-starter-data-jpa')

增加配置文件及數(shù)據(jù)庫(kù)配置

創(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ù),然后通過(guò) http://localhost:8080/listAllUser 查詢數(shù)據(jù)庫(kù)

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ù)庫(kù)索引(默認(rèn)為0)
    database: 0
    ## Redis服務(wù)器地址
    host: localhost
    ## Redis服務(wù)器連接端口
    port: 6379
    ## Redis服務(wù)器連接密碼(默認(rèn)為空)
    password:
    jedis:
      pool:
        ## 連接池最大連接數(shù)(使用負(fù)值表示沒(méi)有限制)
        #spring.redis.pool.max-active=8
        max-active: 8
        ## 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制)
        #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來(lái)序列化和反序列化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來(lái)序列化和反序列化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ì)無(wú)序集合類型的數(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();
    }
}

代碼通過(guò)@Cacheable使用 redis緩存

在這里插入圖片描述

訪問(wèn)接口后,通過(guò)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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 解決idea啟動(dòng)報(bào)錯(cuò)javax.imageio.IIOException的問(wèn)題

    解決idea啟動(dòng)報(bào)錯(cuò)javax.imageio.IIOException的問(wèn)題

    這篇文章主要介紹了idea啟動(dòng)報(bào)錯(cuò)javax.imageio.IIOException,解決打不開(kāi)idea問(wèn)題,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • 詳解SpringBoot迭代發(fā)布JAR瘦身配置

    詳解SpringBoot迭代發(fā)布JAR瘦身配置

    這篇文章主要介紹了詳解SpringBoot迭代發(fā)布JAR瘦身配置,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • Java實(shí)現(xiàn)常見(jiàn)排序算法的優(yōu)化

    Java實(shí)現(xiàn)常見(jiàn)排序算法的優(yōu)化

    今天給大家?guī)?lái)的是關(guān)于Java的相關(guān)知識(shí),文章圍繞著Java實(shí)現(xiàn)常見(jiàn)排序算法的優(yōu)化展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-01-01
  • Feign+mybatisplus搭建項(xiàng)目遇到的坑及解決

    Feign+mybatisplus搭建項(xiàng)目遇到的坑及解決

    這篇文章主要介紹了Feign+mybatisplus搭建項(xiàng)目遇到的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • 使用feign配置網(wǎng)絡(luò)ip代理

    使用feign配置網(wǎng)絡(luò)ip代理

    這篇文章主要介紹了使用feign配置網(wǎng)絡(luò)ip代理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 詳解Java線性結(jié)構(gòu)中的鏈表

    詳解Java線性結(jié)構(gòu)中的鏈表

    除了一些算法之外,我們還有掌握一些常見(jiàn)的數(shù)據(jù)結(jié)構(gòu),比如數(shù)組、鏈表、棧、隊(duì)列、樹(shù)等結(jié)構(gòu),所以接下來(lái)就給大家詳細(xì)講解一下線性結(jié)構(gòu)中的鏈表,需要的朋友可以參考下
    2023-07-07
  • Java的帶GUI界面猜數(shù)字游戲的實(shí)現(xiàn)示例

    Java的帶GUI界面猜數(shù)字游戲的實(shí)現(xiàn)示例

    這篇文章主要介紹了Java的帶GUI界面猜數(shù)字游戲的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • mybatis同一張表多次連接查詢相同列賦值問(wèn)題小結(jié)

    mybatis同一張表多次連接查詢相同列賦值問(wèn)題小結(jié)

    這篇文章主要介紹了mybatis同一張表多次連接查詢相同列賦值問(wèn)題,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下
    2017-01-01
  • springboot項(xiàng)目事務(wù)標(biāo)簽驗(yàn)證

    springboot項(xiàng)目事務(wù)標(biāo)簽驗(yàn)證

    本文主要介紹了springboot項(xiàng)目事務(wù)標(biāo)簽驗(yàn)證,文中通過(guò)示例代碼介紹的非常詳細(xì),詳細(xì)的介紹了不加事務(wù)標(biāo)簽和加事物標(biāo)簽的使用,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07
  • 解析Spring中@Controller@Service等線程安全問(wèn)題

    解析Spring中@Controller@Service等線程安全問(wèn)題

    這篇文章主要為大家介紹解析了Spring中@Controller@Service等線程的安全問(wèn)題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-03-03

最新評(píng)論