依赖
<!--springboot 整合redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置依赖
# 配置redis
spring.redis.host=localhost
spring.redis.port=6379
测试
@SpringBootTest
class RedisDemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
redisTemplate.opsForValue().set("name", "吕星辰");
System.out.println(redisTemplate.opsForValue().get("name"));
}
}
打印成功:
可以在redis中看到我们存储的key(只不过存储的时候需要序列化key)
redisTemplate.
1、常用的操作字符串、集合、Geo、Hash、Set方法
2、获取redis连接对象
redisTemplate.getConnectionFactory().getConnection()
RedisConnection redisConnection = redisTemplate.getConnectionFactory().getConnection();
3、序列化器
关于对象的保存,需要把对象序列化,才能存到redis中
@SpringBootTest
class RedisDemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
User user = new User("吕星辰", 20);
// 先看下序列化之后的
String seUser = JSON.toJSONString(user, SerializerFeature.PrettyFormat);
redisTemplate.opsForValue().set("user", seUser);
System.out.println(redisTemplate.opsForValue().get("user"));
}
}
在看下没序列化的,直接报错了:
@Test
void contextLoads() {
User user = new User("吕星辰", 20);
redisTemplate.opsForValue().set("user", user);
System.out.println(redisTemplate.opsForValue().get("user"));
}
底层,存储一个键值对时,会使用JdkSerializationRedisSerializer 对key进行序列化,序列化的样子如下:
在redis中查看是一堆乱码:
此时需要自定义序列化的配置:
自定义序列化器时,如果ObjectMapper 不存在的话,需要下载依赖:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
自定义配置:
package com.example.redisdemo.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;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
@Configuration
public class RedisConfig {
/**
* RedisTemplate配置
*/
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
// 我们为了自己开发方便,一般直接使用 <String, Object>
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
// Json序列化配置
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
// String 的序列化
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
配置万我们再来测试下:
@Test
void contextLoads() {
User user = new User("吕星辰", 20);
redisTemplate.opsForValue().set("user", user);
System.out.println(redisTemplate.opsForValue().get("user"));
}
在命令行查看: