Spring整合Redis
引入依赖
Spring-boot-starter-data-redis
配置Redis配置数据库参数
#RedisProperties spring.redis.database=10 spring.redis.host=localhost spring.redis.port=6379
编写配置类,构造RedisTemplate
@Configuration public class RedisConfig { @Bean public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){ RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(factory); //设置key的序列化方式 template.setKeySerializer(RedisSerializer.string()); //设置value的序列化方式 template.setValueSerializer(RedisSerializer.json()); //设置hash的key的序列化方式 template.setHashKeySerializer(RedisSerializer.string()); //设置hash的value的序列化方式 template.setHashValueSerializer(RedisSerializer.json()); return template; } }
```
访问Redis
- redisTemplate.opsForValue()
- redisTemplate.opsForHash()
- redisTemplate.opsForList()
- redisTemplate.opsForSet()
- redisTemplate.opsForZset()