目录
1.引入依赖
在SpringBoot项目的POM文件下引入Redis的依赖。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2.注入Redis配置
在SpringBoot项目的application.yaml文件中注入配置
spring: data: # Redis缓存配置 redis: host: 127.0.0.1 database: 0 port: 6379 password: 123456 # redis的windows安装默认没有密码 timeout: 300ms
3.测试Redis连接
在SpringBoot的测试类下进行测试
package com.qxx.bigweb.RedisTests; import jakarta.annotation.Resource; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.StringRedisTemplate; @SpringBootTest public class RedisConnnectTest { @Resource private StringRedisTemplate stringRedisTemplate; @Test public void RedisConnnectTest1(){ stringRedisTemplate.opsForValue().set("name:1","赵一"); } @Test public void RedisConnnectTest2(){ String name = stringRedisTemplate.opsForValue().get("name:1"); System.out.println(name); } }