基于spring boot整合.
1 引入依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2 编写redis的配置文件
application.yml进行编写.
属性:
url
port
password
spring:application:name: spring-boot-redis-demo
redis:database: 0 # 默认选择的数据库,第0个库.host: # 修改成你的ip地址.port: 6379 # 端口事情配置password: tingyiwa # 密码配置.
# 如果使用的是云服务, 你一定开放一下访问端口. 一般: 阿里云/华为云 --> 安全组,把redis端口号# 放行一下.如果用的是腾讯云 --> 防火墙. 将6379 添加放行列表当中.
3 传统方式
jedis, 之前传统的操作redis的方式.
spring boot –> 封装一切东西. —> RedisTemplate, Redis操作模板. 「封装了redis的所有的操作.」
类似的操作,在spring boot当中随处可以见: JdbcTempalte, ….
现在, 我们使用RedisTempalte
, 由spring boot给我们封装好的工具类。用起来特别舒服。类似的:ElasticsearchTempalte, KafkaTemplate, RabbitMQTemplate, JdbcTempalte. ……
4 对ReidsTemplate进行一下配置
把RedisTemplate放到容器当中. 写一个配置类.通过@Bean的方式.
这里默认使用的是Lettuce客户端.
以下主要是针对string类型进行序列化配置.
package com.tingyi.configs;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/*** @author tingyi*/
@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory redisConnectionFactory) {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(redisConnectionFactory);
ObjectMapper objectMapper = new ObjectMapper();// 使用Jackson2JsonRedisSerializer替换默认序列化;Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);objectMapper.configure(MapperFeature.USE_ANNOTATIONS, false);objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
// 此项必须配置,否则会报java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to XXXobjectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(objectMapper);// setVisibility: 允许更改底层VisibilityChecker的配置,以更改自动检测的属性类型的详细信息// 这个代码这将使所有成员字段无需进一步注释即可序列化,而不仅仅是公共字段;objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 设置key和value的序列化规则redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
// 设置hashKey和hashValue的序列化规则redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
// 设置支持事务redisTemplate.setEnableTransactionSupport(true);redisTemplate.afterPropertiesSet();
return redisTemplate;}
}
5 单元测试
package com.tingyi;
import com.tingyi.entity.User;
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 java.util.concurrent.TimeUnit;
/*** spring boot单元测试的注解.这里可以注入其它的对象了.*/
@SpringBootTest
class Qf37SpringBootRedisDemo01ApplicationTests {
@Autowiredprivate RedisTemplate<String, Object> redisTemplate;
/*** 操作String类型的单元测试.*/@Testpublic void redisStringTest(){redisTemplate.opsForValue().set("username", "齐天大圣", 120, TimeUnit.SECONDS);}
@Testpublic void redisObjectTest(){User user = new User();user.setUsername("听忆");user.setSex("男");long id = 1024L;user.setId(id);// redisTemplate.opsForHash().put("users", "user:" + id , user);redisTemplate.opsForValue().set("user:" + id, user, 300, TimeUnit.SECONDS);}
}