1、POM中添加maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、application.properties中添加以下
## redis 开发
spring.cache.type=redis
spring.redis.hostName=10.0.31.181
spring.redis.password=dx0210&
spring.redis.port=6380
spring.redis.database=0
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-2
3、添加一个配置类
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
@EnableAutoConfiguration
public class RedisConfig {
private static Logger logger = LoggerFactory.getLogger(RedisConfig.class);
@Bean
@ConfigurationProperties(prefix="spring.redis")
public JedisPoolConfig getRedisConfig(){
JedisPoolConfig config = new JedisPoolConfig();
return config;
}
@Bean
@ConfigurationProperties(prefix="spring.redis")
public JedisConnectionFactory getConnectionFactory(){
JedisConnectionFactory factory = new JedisConnectionFactory();
JedisPoolConfig config = getRedisConfig();
factory.setPoolConfig(config);
logger.info("JedisConnectionFactory bean init success.");
return factory;
}
@Bean
public RedisTemplate<?, ?> getRedisTemplate(){
RedisTemplate<?,?> template = new StringRedisTemplate(getConnectionFactory()); //只能对字符串的键值操作
return template;
}
}
//可以对其他类型的键值进行操作
@Bean
public RedisTemplate<?, ?> getRedisTemplate(){
RedisTemplate<?,?> template = new RedisTemplate();
template.setConnectionFactory(getConnectionFactory());
return template;
}
//可以根据实际需要创建多个实例
@Bean(name = "LongRedisTemplate")
public RedisTemplate<String, Long> getRedisTemplate1(){
RedisTemplate<String,Long> template = new RedisTemplate<String,Long>(); //只能对字符串的键值操作
template.setConnectionFactory(getConnectionFactory());
System.out.println("生成<String, Long>template:" + template);
return template;
}
@Bean(name = "StringRedisTemplate")
public RedisTemplate<String, String> getRedisTemplate2(){
RedisTemplate<String,String> template = new RedisTemplate<String,String>(); //只能对字符串的键值操作
template.setConnectionFactory(getConnectionFactory());
System.out.println("生成<String, String>template:" + template);
return template;
}
@Bean(name = "IntegerRedisTemplate")
public RedisTemplate<String, Integer> getRedisTemplate3(){
RedisTemplate<String,Integer> template = new RedisTemplate<String,Integer>(); //只能对字符串的键值操作
template.setConnectionFactory(getConnectionFactory());
System.out.println("生成<String,Integer>template:" + template);
return template;
}
使用时注入即可
@Resource(name = "StringRedisTemplate")
private RedisTemplate<String, String> stringRedisTemplate;
@Resource(name = "LongRedisTemplate")
private RedisTemplate<String, Long> longRedisTemplate;
@Resource(name = "IntegerRedisTemplate")
private RedisTemplate<String, Integer> integerRedisTemplate;
这里要注意的是,对于StringRedisTemplate
SDR默认采用的序列化策略有两种,一种是String的序列化策略,一种是JDK的序列化策略。
StringRedisTemplate默认采用的是String的序列化策略,保存的key和value都是采用此策略序列化保存的。
RedisTemplate默认采用的是JDK的序列化策略,保存的key和value都是采用此策略序列化保存的。
可以采用以下方式手动修改序列化策略
// 使用Jackson2JsonRedisSerialize 替换默认序列化(默认采用的是JDK序列化)
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setKeySerializer(jackson2JsonRedisSerializer);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashKeySerializer(jackson2JsonRedisSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();