1.在pom.xml文件中添加如下依赖
<!--整合Redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2.配置application.yml文件
spring: profiles: active: abc,def #把JdbcConfig 类中的druid的配置删除或注释 #在刚才引入jdbc启动器的时候,SpringBoot已经自动帮我们引入了一个连接池,HikariCP应该是目前速度最快的连接池 datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/xxx?useUnicode=true&characterEncoding=utf8&useSSL=false username: root password: root redis: host: localhost port: 6379 timeout: 10000
3.编写测试代码时遇到的问题
package com.itheima.redis; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.junit4.SpringRunner; import java.util.List; import java.util.Set; @RunWith(SpringRunner.class) @SpringBootTest public class RedisTest { @Autowired private RedisTemplate redisTemplate; @Test public void test(){ //string字符串 redisTemplate.opsForValue().set("str", "xxx"); redisTemplate.boundValueOps("str").set("xxx"); System.out.println("str = " + redisTemplate.opsForValue().get("str")); // hash散列 redisTemplate.boundHashOps("h_key").put("name", "xxx"); redisTemplate.boundHashOps("h_key").put("age", 13); //获取所有域对应的值 Set set = redisTemplate.boundHashOps("h_key").keys(); System.out.println("hash散列所有的域:" + set); List list = redisTemplate.boundHashOps("h_key").values(); System.out.println("hash散列所有的域值:" + list); //list列表 redisTemplate.boundListOps("l_key").leftPush("c"); redisTemplate.boundListOps("l_key").leftPush("b"); redisTemplate.boundListOps("l_key").leftPush("a"); list = redisTemplate.boundListOps("l_key").range(0, -1); System.out.println("列表的值:" + list); //set集合 redisTemplate.boundSetOps("set_key").add("a", "b", "c"); set = redisTemplate.boundSetOps("set_key").members(); System.out.println("集合的元素:" + set); //sorted set有序集合 redisTemplate.boundZSetOps("z_key").add("a", 30); redisTemplate.boundZSetOps("z_key").add("b", 20); redisTemplate.boundZSetOps("z_key").add("c", 10); set = redisTemplate.boundZSetOps("z_key").range(0, -1); System.out.println("有序集合的元素:" + set); } }
4.测试问题
org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379
(1)启动命令
redis-server redis.windows.conf
(2)设置服务命令
redis-server --service-install redis.windows-service.conf --loglevel verbose
(3)常用Redis服务命令
卸载服务:redis-server --service-uninstall
开启服务:redis-server --service-start
停止服务:redis-server --service-stop
(4)优质Redis文章
https://www.cnblogs.com/yunqing/p/10605934.html 安装
https://blog.youkuaiyun.com/qian_qian_123/article/details/85161320 用法
https://blog.youkuaiyun.com/lei396601057/article/details/77527061 服务