SpringBoot整合Redis(五)

1. 引入Redis依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<parent>
<artifactId>spring-boot-dependencies</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.3.5.RELEASE</version>
</parent>
<dependencies>
<!-- Redis依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 测试包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

2. 配置Redis连接信息

在application.yaml配置文件中配置:

1
2
3
4
5
6
7
spring:
redis:
host: localhost
port: 6379
password: password
timeout: 6000 #超时时间
database: 0 #第一个数据库,0-15

3. 配置启动类

1
2
3
4
5
6
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}

4. 使用RedisTemplate工具类操作Redis

springboot中使用RedisTemplate来操作redis,需要在我们的bean中注入这个对象,代码如下:

1
2
3
4
5
6
7
8
9
@Autowired
private RedisTemplate<String, String> redisTemplate;

// 用下面5个对象来操作对应的类型
this.redisTemplate.opsForValue(); //提供了操作string类型的所有方法
this.redisTemplate.opsForList(); // 提供了操作list类型的所有方法
this.redisTemplate.opsForSet(); //提供了操作set的所有方法
this.redisTemplate.opsForHash(); //提供了操作hash表的所有方法
this.redisTemplate.opsForZSet(); //提供了操作zset的所有方法

5.RedisTemplate操作Demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* SpringBoot集成Redis,测试常用操作命令
* @author xiaoyuge
*/
@SpringBootTest(classes = App.class)
@RunWith(SpringRunner.class)
public class RedisTemplateTest {

@Autowired
private RedisTemplate<String, String> redisTemplate;

@Test
public void stringTest() {
//删除key
this.redisTemplate.delete("name");
//设置值
this.redisTemplate.opsForValue().set("name", "xiaoyuge");
//获取值
String name = this.redisTemplate.opsForValue().get("name");
System.out.println(name);
}

@Test
public void listTest() {
this.redisTemplate.delete("names");
this.redisTemplate.opsForList().rightPushAll("names", "tom", "chelly", "xiaoyuge", "fan");
List<String> list = this.redisTemplate.opsForList().range("names", 0, -1);
list.forEach(System.out::println);
}

@Test
public void setTest() {
this.redisTemplate.opsForSet().add("courses", "java", "php", "node", "js");
//获取set集合中的值
Set<String> courseSet = this.redisTemplate.opsForSet().members("courses");
courseSet.forEach(System.out::println);
}

@Test
public void hashTest() {
Map<String, String> map = new HashMap<String, String>() {{
put("name", "xiaoyuge");
put("age", "18");
}};
this.redisTemplate.opsForHash().putAll("userMap", map);

//获取值
Map<Object, Object> resultMap = this.redisTemplate.opsForHash().entries("userMap");
System.out.println(resultMap);
}

@Test
public void zsetTest() {
this.redisTemplate.delete("mv");
this.redisTemplate.opsForZSet().add("mv", "赵丽颖", 960d);
this.redisTemplate.opsForZSet().add("mv", "袁冰妍", 98d);
this.redisTemplate.opsForZSet().add("mv", "刘亦菲", 100d);
this.redisTemplate.opsForZSet().add("mv", "杨超越", 80d);

//获取全部的属性值
Set<String> mvs = this.redisTemplate.opsForZSet().range("mv", 0, -1);
mvs.forEach(System.out::println);
}
}