注意:
- StringRedisTemplate是默认就有的直接@AutoWried就可以 它里边存的都是String类型的
- RedisTemplate也是默认有直接@AutoWried就可以 它里边存的是Object类型
1. 引入坐标
<!-- fastJson依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.52</version>
</dependency>
<!-- redis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2. 配置数据库
spring:
data:
redis:
host: localhost
port: 6379
3. 如果要自定义RedisTemplate就操作这一步,否则跳过
package com.ape.springboot_redis.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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<Object,Object> jsonRedisTemplate(RedisConnectionFactory factory){
// 1.创建自定义模板类
RedisTemplate<Object, Object> template = new RedisTemplate<>();
// 2.配置json类型的序列化工具
template.setKeySerializer(new StringRedisSerializer());//String
template.setDefaultSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
template.setConnectionFactory(factory);
return template;
}
}
4. 实体类因为使用默认的RedisTemplate要序列化,所以实现Serializable接口
package com.ape.springboot_redis.pojo;
import lombok.*;
import java.io.Serializable;
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
public class Student implements Serializable {
private Integer sId;
private String sName;
private String sHobby;
}
5. 测试StringRedisTemplate
package com.ape.springboot_redis.test;
import com.ape.springboot_redis.pojo.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.List;
import java.util.Set;
@SpringBootTest
public class Test01 {
// 专用对象
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
// 字符串类型模板
@Autowired
private StringRedisTemplate stringRedisTemplate;
// 自定义模板
@Autowired
private RedisTemplate jsonRedisTemplate;
/**
* 测试使用stringRedisTemplate
*/
@Test
public void testStringTemplate() {
// 使用正则表达找到数据库中的key
Set<String> keys = stringRedisTemplate.keys("*");
for (String key : keys) {
System.out.println(key);
}
// 类型
stringRedisTemplate.opsForValue();//String
stringRedisTemplate.opsForList();//List
stringRedisTemplate.opsForHash();//Hash
stringRedisTemplate.opsForSet();//Set
stringRedisTemplate.opsForZSet();//ZSet
// 举例
stringRedisTemplate.opsForValue().set("name", "zyk");
System.out.println(stringRedisTemplate.opsForValue().get("name"));
// 操作List
stringRedisTemplate.opsForList().leftPush("list1", "曹操");
stringRedisTemplate.opsForList().leftPush("list1", "曹植");
stringRedisTemplate.opsForList().leftPushAll("list1", "曹睿", "曹爽", "曹真");
List<String> list1 = stringRedisTemplate.opsForList().range("list1", 0, -1);
for (String s : list1) {
System.out.println(s);
}
}
}
6. 测试RedisTemplate
package com.ape.springboot_redis.test;
import com.ape.springboot_redis.pojo.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.List;
import java.util.Set;
@SpringBootTest
public class Test01 {
// 专用对象
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
// 字符串类型模板
@Autowired
private StringRedisTemplate stringRedisTemplate;
// 自定义模板
@Autowired
private RedisTemplate jsonRedisTemplate;
/**
* 测试默认模板RedisTemplate
* 1.测试RedisTemplate与stringRedisTemplate存的数据相互独立
* 2.redisTemplate默认使用key序列化方式和value的序列化方式都使用的是jdk serializer序列化
* 所以存对象会乱码
*/
@Test
public void testRedisTemplate() {
// 添加/修改的set方法
redisTemplate.opsForValue().set("name", "张无忌");
String name = (String) redisTemplate.opsForValue().get("name");
System.out.println(name);
// 类型
redisTemplate.opsForValue();//String
redisTemplate.opsForList();//List
redisTemplate.opsForHash();//Hash
redisTemplate.opsForSet();//Set
redisTemplate.opsForZSet();//ZSet
// 存一个Student对象
Student s = new Student(999, "周永康", "睡觉");
redisTemplate.opsForValue().set("student", s);
Student student = (Student) redisTemplate.opsForValue().get("student");
System.out.println(student);
// 操作set集合
redisTemplate.opsForSet().add("set1", s, new Student(1, "刘备", "编制草鞋"), new Student(2, "张三", "法律科普"));
Set<Object> set1 = redisTemplate.opsForSet().members("set1");
for (Object stu : set1) {
Student stu1 = (Student) stu;
System.out.println(stu1);
}
}
}