最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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ù)庫索引(默認(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
        ## 連接池最大阻塞等待時間(使用負(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
    ## 連接超時時間(毫秒)
    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è)置緩存有效期一小時
        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等會跑出異常
        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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java實現(xiàn)XML與JSON的互相轉(zhuǎn)換詳解

    Java實現(xiàn)XML與JSON的互相轉(zhuǎn)換詳解

    這篇文章主要為大家詳細(xì)介紹了如何使用Java實現(xiàn)XML與JSON的互相轉(zhuǎn)換,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-03-03
  • Java 實戰(zhàn)項目錘煉之小區(qū)物業(yè)管理系統(tǒng)的實現(xiàn)流程

    Java 實戰(zhàn)項目錘煉之小區(qū)物業(yè)管理系統(tǒng)的實現(xiàn)流程

    讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實現(xiàn)一個小區(qū)物業(yè)管理系統(tǒng),大家可以在過程中查缺補漏,提升水平
    2021-11-11
  • java中Memcached的使用實例(包括與Spring整合)

    java中Memcached的使用實例(包括與Spring整合)

    這篇文章主要介紹了java中Memcached的使用實例(包括與Spring整合),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java中拼接字符串String的N種方法總結(jié)

    Java中拼接字符串String的N種方法總結(jié)

    字符串拼接是我們在Java代碼中比較經(jīng)常要做的事情,就是把多個字符串拼接到一起,下面這篇文章主要給大家介紹了關(guān)于Java中拼接String的N種方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02
  • MyBatis二級緩存實現(xiàn)關(guān)聯(lián)刷新

    MyBatis二級緩存實現(xiàn)關(guān)聯(lián)刷新

    本文主要介紹了MyBatis二級緩存實現(xiàn)關(guān)聯(lián)刷新,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • 基于Java事件監(jiān)聽編寫一個中秋猜燈謎小游戲

    基于Java事件監(jiān)聽編寫一個中秋猜燈謎小游戲

    眾所周知,JavaSwing是Java中關(guān)于窗口開發(fā)的一個工具包,可以開發(fā)一些窗口程序,然后由于工具包的一些限制,導(dǎo)致Java在窗口開發(fā)商并沒有太多優(yōu)勢,不過,在JavaSwing中關(guān)于事件的監(jiān)聽機制是我們需要重點掌握的內(nèi)容,本文將基于Java事件監(jiān)聽編寫一個中秋猜燈謎小游戲
    2023-09-09
  • SpringBoot(十)之郵件服務(wù)

    SpringBoot(十)之郵件服務(wù)

    這篇文章給大家介紹了SpringBoot郵件服務(wù)的相關(guān)知識,此文是使用springboot最新版本1.5.3進(jìn)行開發(fā)的。本文給大家介紹的非常詳細(xì),具有參考借鑒價值,需要的的朋友參考下吧
    2017-05-05
  • JavaScript中棧和隊列應(yīng)用詳情

    JavaScript中棧和隊列應(yīng)用詳情

    這篇文章主要介紹了JavaScript中棧和隊列應(yīng)用詳情,棧如果用數(shù)組模擬的話是類似于一個U形桶狀堆??臻g,文章圍繞制圖展開詳細(xì)的內(nèi)容展開更多相關(guān)內(nèi)容,需要的小伙伴可以參考一下
    2022-06-06
  • IDEA Maven下載依賴包速度過慢的問題及解決方案

    IDEA Maven下載依賴包速度過慢的問題及解決方案

    這篇文章主要介紹了IDEA Maven下載依賴包速度過慢的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • 用java實現(xiàn)的獲取優(yōu)酷等視頻縮略圖的實現(xiàn)代碼

    用java實現(xiàn)的獲取優(yōu)酷等視頻縮略圖的實現(xiàn)代碼

    想獲取優(yōu)酷等視頻縮略圖,在網(wǎng)上沒有找到滿意的資料,參考了huangdijia的PHP版工具一些思路,寫了下面的JAVA版代碼。。其實也可以做成JS版的
    2013-05-05

最新評論

福州市| 义马市| 上饶市| 扎兰屯市| 宜君县| 上高县| 霍城县| 屏东市| 册亨县| 郎溪县| 林口县| 盐源县| 富锦市| 清新县| 拜泉县| 泰宁县| 乐清市| 金川县| 盖州市| 崇阳县| 宁明县| 盈江县| 英超| 大连市| 昭平县| 思南县| 琼中| 平乡县| 威宁| 青铜峡市| 金沙县| 双城市| 甘孜县| 温州市| 开阳县| 阿城市| 洪雅县| 辽宁省| 长宁县| 图们市| 伊春市|