SpringBoot使用Redis快速开始教程
版本环境介绍
-
springboot 2.6.4
-
jdk1.8
-
redis 5.0.10
开始前准备
- 创建springboot项目时,勾选LomBok,Redis
搭建环境
- 创建对象,用于模拟数据
package cn.kgc.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Users {
private Integer id;
private String name;
private Integer age;
}
- 创建Redis序列化配置工具类
package cn.kgc.redisconfig;
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.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class MyRedisConfig {
@Bean(name = "redisTemplate")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
参照StringRedisTemplate内部实现指定序列化器
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(keySerializer());
redisTemplate.setHashKeySerializer(keySerializer());
redisTemplate.setValueSerializer(valueSerializer());
redisTemplate.setHashValueSerializer(valueSerializer());
return redisTemplate;
}
private RedisSerializer<String> keySerializer(){
return new StringRedisSerializer();
}
//使用Jackson序列化器
private RedisSerializer<Object> valueSerializer(){
return new GenericJackson2JsonRedisSerializer();
}
}
- 添加 application.properties 配置数据
## 是否启动日志SQL语句
spring.jpa.show-sql=true
# Redis 数据库索引(默认为 0)
spring.redis.database=0
spring.redis.host=localhost
spring.redis.port=6379
# Redis 服务器连接密码(默认为空)
spring.redis.password=
# springboot 2.0 redis默认客户端已换成lettuce
# 连接池最大连接数(使用负值表示没有限制) 默认 8
spring.redis.lettuce.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
spring.redis.lettuce.pool.max-wait=-1
# 连接池中的最大空闲连接 默认 8
spring.redis.lettuce.pool.max-idle=8
# 连接池中的最小空闲连接 默认 0
spring.redis.lettuce.pool.min-idle=0
spring.redis.timeout=5000
完成测试,编写测试类
package cn.kgc;
import cn.kgc.model.Users;
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.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest
class SpringbootRedisApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void test(){
redisTemplate.opsForValue().set("test","您好");
System.out.println(redisTemplate.opsForValue().get("test"));
redisTemplate.opsForValue().set("MyTests", "好的");
System.out.println(redisTemplate.opsForValue().get("MyTest"));
Map<String, Object> map = new HashMap<>();
for (int i=0; i<10; i++){
Users user = new Users();
user.setId(i);
user.setName(String.format("测试%d", i));
user.setAge(i+10);
map.put(String.valueOf(i),user);
}
redisTemplate.opsForHash().putAll("测试", map);
BoundHashOperations hashOps = redisTemplate.boundHashOps("测试");
Map map1 = hashOps.entries();
System.out.println(map1);
}
}