日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Redis下載部署并加入idea應(yīng)用的小結(jié)_Redis

作者:藍(lán)多多的小倉庫 ? 更新時(shí)間: 2022-11-26 編程語言

前言

復(fù)習(xí)一下Redis的部署和應(yīng)用,并記錄了下來!

一、下載Window版本的redis

1.打開網(wǎng)址:github上的redis安裝包,找到Redis on Windows,點(diǎn)擊 release page。

2.選擇你要下載的版本,點(diǎn)擊安裝程序進(jìn)行下載

3.安裝 一直點(diǎn) 下一步 直至完成安裝就行,注意自己的安裝目錄(下面的配置環(huán)境變量要用到,我自己的路徑是D:\Redis)

二、配置環(huán)境變量

1.右擊我的電腦,選擇屬性

2.點(diǎn)擊 高級系統(tǒng)設(shè)置 ,我這是win11系統(tǒng),你們自己找哈!

3.點(diǎn)擊環(huán)境變量

4.雙擊Path

5.點(diǎn)擊新建,把安裝redis的對應(yīng)目錄寫進(jìn)去,然后確定。

6.點(diǎn)擊win+R,輸入cmd

7.輸入命令redis-cli,連接成功!

到這里redis部署就完成了!!!下面是redis在idea里面的應(yīng)用! 三、redis在idea的應(yīng)用 1.打開pom.xml文件,引入redis架包,代碼如下

代碼如下:

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>

2.打開application.properties配置文件,寫入redis的相關(guān)配置

代碼如下:

# RedisProperties
#redis一共有16(0-15)個(gè)數(shù)據(jù)庫,隨便給一個(gè)
spring.redis.database=11
spring.redis.host=localhost
spring.redis.port=6379

3.新建一個(gè)配置類redisConfig.java文件,代碼如下

package com.example.community.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * @ClassName redisConfig
 * @Description TODO
 * @Author 加辣椒了嗎?
 * @Date 2022/4/28 2:33
 * @Version 1.0
 **/
@Configuration
public class redisConfig {
    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
        // 將redis注入工廠
        RedisTemplate<String,Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        // 設(shè)置key的序列化方式
        template.setKeySerializer (RedisSerializer.string());
        //設(shè)置value的序列化方式
        template.setValueSerializer (RedisSerializer.json());
        // 設(shè)置hash的key的序列化方式
        template. setHashKeySerializer (RedisSerializer.string());
        // 設(shè)置hash的value的序列化方式
        template.setHashValueSerializer (RedisSerializer.json());
        // 使設(shè)置生效
        template.afterPropertiesSet();

        return template;
    }
}

4.測試 在測試類里面添加測試方法,測試通過

代碼如下:

package com.example.community.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * @ClassName redisConfig
 * @Description TODO
 * @Author 加辣椒了嗎?
 * @Date 2022/4/28 2:33
 * @Version 1.0
 **/
@Configuration
public class redisConfig {
    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
        // 將redis注入工廠
        RedisTemplate<String,Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        // 設(shè)置key的序列化方式
        template.setKeySerializer (RedisSerializer.string());
        //設(shè)置value的序列化方式
        template.setValueSerializer (RedisSerializer.json());
        // 設(shè)置hash的key的序列化方式
        template. setHashKeySerializer (RedisSerializer.string());
        // 設(shè)置hash的value的序列化方式
        template.setHashValueSerializer (RedisSerializer.json());
        // 使設(shè)置生效
        template.afterPropertiesSet();

        return template;
    }
}

或者
打開redis控制臺,輸入以下命令,測試通過!

總結(jié)

原文鏈接:https://blog.csdn.net/weixin_44797539/article/details/124560730

欄目分類
最近更新