1.pom引入
<!-- redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> 复制代码
2.配置文件application.properties
#-----------------redis-------------------------- # REDIS (RedisProperties) # Redis数据库索引(默认为0) spring.redis.database=0 # Redis服务器地址 spring.redis.host=127.0.0.1 # Redis服务器连接端口 spring.redis.port=6379 # Redis服务器连接密码(默认为空) spring.redis.password=123654 # 连接池最大连接数(使用负值表示没有限制) spring.redis.jedis.pool.max-active=8 # 连接池最大阻塞等待时间(使用负值表示没有限制) spring.redis.jedis.pool.max-wait=-1 # 连接池中的最大空闲连接 spring.redis.jedis.pool.max-idle=8 # 连接池中的最小空闲连接 spring.redis.jedis.pool.min-idle=0 # 连接超时时间(毫秒) spring.redis.timeout=5000 #-----------------redis end------------------------ 复制代码
3.创建个RedisConfig类
操作redis的时候可以又多种序列化方法,直接字符串 JSON,JSON带有过滤敏感属性的 复制代码
@Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplateString(RedisConnectionFactory redisConnectionFactory) { // 配置redisTemplate RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); redisTemplate.setConnectionFactory(redisConnectionFactory); RedisSerializer stringSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringSerializer); // key序列化 redisTemplate.setValueSerializer(stringSerializer); // value序列化 redisTemplate.setHashKeySerializer(stringSerializer); // Hash key序列化 redisTemplate.setHashValueSerializer(stringSerializer); // Hash value序列化 redisTemplate.afterPropertiesSet(); return redisTemplate; } @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { // 配置redisTemplate RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); redisTemplate.setConnectionFactory(redisConnectionFactory); RedisSerializer stringSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringSerializer); // key序列化 redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); // value序列化 redisTemplate.setHashKeySerializer(stringSerializer); // Hash key序列化 redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); // Hash value序列化 redisTemplate.afterPropertiesSet(); return redisTemplate; } @Bean public RedisTemplate<String, Object> redisTemplateGroup(RedisConnectionFactory redisConnectionFactory) { // 配置redisTemplate RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); redisTemplate.setConnectionFactory(redisConnectionFactory); RedisSerializer stringSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringSerializer); // key序列化 redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer(getMapper())); // value序列化 redisTemplate.setHashKeySerializer(stringSerializer); // Hash key序列化 redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer(getMapper())); // Hash value序列化 redisTemplate.afterPropertiesSet(); return redisTemplate; } // 获取JSON工具 private final ObjectMapper getMapper() { ObjectMapper mapper = new ObjectMapper(); JsonFilterUtil.addFilterForMapper(mapper); //将类名称序列化到json串中,去掉会导致得出来的的是LinkedHashMap对象,直接转换实体对象会失败 mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,ObjectMapper.DefaultTyping.NON_FINAL); //设置输入时忽略JSON字符串中存在而Java对象实际没有的属性 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); return mapper; } } 复制代码
序列化参数过滤
public static void addFilterForMapper(ObjectMapper mapper) { Set<String> sets =new HashSet<String>(); sets.add("userEmail"); sets.add("userPhone"); sets.add("userImagePath"); sets.add("userPassword"); sets.add("userSex"); sets.add("userStatus"); //sets.add("liveType"); sets.add("ip"); sets.add("userCreateDate"); SimpleBeanPropertyFilter fieldFilter = SimpleBeanPropertyFilter.serializeAllExcept(sets); SimpleFilterProvider filterProvider = new SimpleFilterProvider().addFilter("fieldFilter", fieldFilter); mapper.setFilterProvider(filterProvider).addMixIn(ChartUser.class, FieldFilterMixIn.class); } 复制代码
4.使用
/** * 添加数据 * @param key * @param value */ public void set(String key, Object value) { //delete(key); ValueOperations<String, Object> vo = redisTemplate.opsForValue(); vo.set(key, value); } /** * 获取数据 * @param key * @return */ public Object get(String key) { ValueOperations<String, Object> vo = redisTemplate.opsForValue(); return vo.get(key); } /** * 删除数据 * @param key * @return */ public boolean delete(String key) { return redisTemplate.delete(key); }