版权声明:转载请声明原文出处!!!! https://blog.csdn.net/weixin_40461281/article/details/82011185
首先,需要安装redis , 可以去看我的另一篇文章 https://blog.csdn.net/weixin_40461281/article/details/81365150
windows安装网上教程有很多 , 再此就不介绍了
添加springboot数据操作jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
填写基础配置yml格式
spring:
redis:
# 第0号库
database: 0
host: ip地址
# 端口号
port: 6379
password: 密码
# 连接超时时间
timeout: 5000
创建RedisConfig.java配置类
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
// @Bean
// public CacheManager cacheManager(RedisTemplate<?, ?> redisTemplate) {
// CacheManager cacheManager = new RedisCacheManager(redisTemplate);
// return cacheManager;
// /*RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
// // 多个缓存的名称,目前只定义了一个
// rcm.setCacheNames(Arrays.asList("thisredis"));
// //设置缓存默认过期时间(秒)
// rcm.setDefaultExpiration(600);
// return rcm;*/
// }
// 以下两种redisTemplate自由根据场景选择
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
serializer.setObjectMapper(mapper);
//设置默认序列化方案
template.setDefaultSerializer(serializer);
//使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
//设置序列化Value的实例化对象
template.afterPropertiesSet();
return template;
}
@Bean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(factory);
return stringRedisTemplate;
}
}
SpringRedisTemplate 用户操作字符串更为便捷 RedisTemplate 可操作多类型数据
RedisTemplate 用法 请参考我的另一篇博客 https://mp.csdn.net/postedit/82011670
编写测试类
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testRedis() {
// key value 设置key过期时间 单位秒
stringRedisTemplate.opsForValue().set("111","222",60,TimeUnit.SECONDS);
String s = stringRedisTemplate.opsForValue().get("111");
System.out.println(s);
}
连接成功