package com.newfiber.api.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import com.newfiber.api.pc.model.entity.RealTimePumpData; import com.newfiber.api.pc.model.entity.WxUserTemplate; import com.newfiber.api.pc.model.gaofen.DayByDayForecast; import com.newfiber.api.pc.model.gaofen.OneByOneHour; import com.newfiber.api.pc.model.gaofen.SmartWeather; import com.newfiber.api.pc.model.vo.TempSiteInfoVo; import com.newfiber.api.pc.model.well.WellLidInfo; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfig { @Bean("redisTemplate") public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory){ //1、创建RedisTemplate对象 RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); //2、添加Redis的连接工厂 redisTemplate.setConnectionFactory(factory); //3、定义Jackson2的序列化对象 Jackson2JsonRedisSerializer<Object> jsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper om = new ObjectMapper(); //4、指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); jsonRedisSerializer.setObjectMapper(om); //5、String的序列化方式 StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); //6、配置key 以及 hashkey的序列化方式 redisTemplate.setKeySerializer(stringRedisSerializer); redisTemplate.setHashKeySerializer(stringRedisSerializer); redisTemplate.setValueSerializer(jsonRedisSerializer); redisTemplate.setHashValueSerializer(jsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; } @Bean("redisTemplateTemp") public RedisTemplate<String, TempSiteInfoVo> redisTemplateTemp(RedisConnectionFactory factory){ //1、创建RedisTemplate对象 RedisTemplate<String, TempSiteInfoVo> redisTemplate = new RedisTemplate<>(); //2、添加Redis的连接工厂 redisTemplate.setConnectionFactory(factory); //3、定义Jackson2的序列化对象 Jackson2JsonRedisSerializer<TempSiteInfoVo> jsonRedisSerializer = new Jackson2JsonRedisSerializer<>(TempSiteInfoVo.class); ObjectMapper om = new ObjectMapper(); //4、指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); jsonRedisSerializer.setObjectMapper(om); //5、String的序列化方式 StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); //6、配置key 以及 hashkey的序列化方式 redisTemplate.setKeySerializer(stringRedisSerializer); redisTemplate.setHashKeySerializer(stringRedisSerializer); redisTemplate.setValueSerializer(jsonRedisSerializer); redisTemplate.setHashValueSerializer(jsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; } @Bean("redisTemplateByWxUserTemplate") public RedisTemplate<String, WxUserTemplate> redisTemplateByWxUserTemplate(RedisConnectionFactory factory){ //1、创建RedisTemplate对象 RedisTemplate<String, WxUserTemplate> redisTemplate = new RedisTemplate<>(); //2、添加Redis的连接工厂 redisTemplate.setConnectionFactory(factory); //3、定义Jackson2的序列化对象 Jackson2JsonRedisSerializer<WxUserTemplate> jsonRedisSerializer = new Jackson2JsonRedisSerializer<>(WxUserTemplate.class); ObjectMapper om = new ObjectMapper(); //4、指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); jsonRedisSerializer.setObjectMapper(om); //5、String的序列化方式 StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); //6、配置key 以及 hashkey的序列化方式 redisTemplate.setKeySerializer(stringRedisSerializer); redisTemplate.setHashKeySerializer(stringRedisSerializer); redisTemplate.setValueSerializer(jsonRedisSerializer); redisTemplate.setHashValueSerializer(jsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; } @Bean("redisTemplateBySmartWeather") public RedisTemplate<String, SmartWeather> redisTemplateBySmartWeather(RedisConnectionFactory factory){ //1、创建RedisTemplate对象 RedisTemplate<String, SmartWeather> redisTemplate = new RedisTemplate<>(); //2、添加Redis的连接工厂 redisTemplate.setConnectionFactory(factory); //3、定义Jackson2的序列化对象 Jackson2JsonRedisSerializer<SmartWeather> jsonRedisSerializer = new Jackson2JsonRedisSerializer<>(SmartWeather.class); ObjectMapper om = new ObjectMapper(); //4、指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); jsonRedisSerializer.setObjectMapper(om); //5、String的序列化方式 StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); //6、配置key 以及 hashkey的序列化方式 redisTemplate.setKeySerializer(stringRedisSerializer); redisTemplate.setHashKeySerializer(stringRedisSerializer); redisTemplate.setValueSerializer(jsonRedisSerializer); redisTemplate.setHashValueSerializer(jsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; } @Bean("redisTemplateByDayByDayForecast") public RedisTemplate<String, DayByDayForecast> redisTemplateByDayByDayForecast(RedisConnectionFactory factory){ //1、创建RedisTemplate对象 RedisTemplate<String, DayByDayForecast> redisTemplate = new RedisTemplate<>(); //2、添加Redis的连接工厂 redisTemplate.setConnectionFactory(factory); //3、定义Jackson2的序列化对象 Jackson2JsonRedisSerializer<DayByDayForecast> jsonRedisSerializer = new Jackson2JsonRedisSerializer<>(DayByDayForecast.class); ObjectMapper om = new ObjectMapper(); //4、指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); jsonRedisSerializer.setObjectMapper(om); //5、String的序列化方式 StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); //6、配置key 以及 hashkey的序列化方式 redisTemplate.setKeySerializer(stringRedisSerializer); redisTemplate.setHashKeySerializer(stringRedisSerializer); redisTemplate.setValueSerializer(jsonRedisSerializer); redisTemplate.setHashValueSerializer(jsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; } @Bean("redisTemplateByOneByOne") public RedisTemplate<String, OneByOneHour> redisTemplateByOneByOne(RedisConnectionFactory factory){ //1、创建RedisTemplate对象 RedisTemplate<String, OneByOneHour> redisTemplate = new RedisTemplate<>(); //2、添加Redis的连接工厂 redisTemplate.setConnectionFactory(factory); //3、定义Jackson2的序列化对象 Jackson2JsonRedisSerializer<OneByOneHour> jsonRedisSerializer = new Jackson2JsonRedisSerializer<>(OneByOneHour.class); ObjectMapper om = new ObjectMapper(); //4、指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); jsonRedisSerializer.setObjectMapper(om); //5、String的序列化方式 StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); //6、配置key 以及 hashkey的序列化方式 redisTemplate.setKeySerializer(stringRedisSerializer); redisTemplate.setHashKeySerializer(stringRedisSerializer); redisTemplate.setValueSerializer(jsonRedisSerializer); redisTemplate.setHashValueSerializer(jsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; } @Bean("realTimePumpDataRedisTemplate") public RedisTemplate<String, RealTimePumpData> realTimePumpDataRedisTemplate(RedisConnectionFactory factory){ //1、创建RedisTemplate对象 RedisTemplate<String, RealTimePumpData> redisTemplate = new RedisTemplate<>(); //2、添加Redis的连接工厂 redisTemplate.setConnectionFactory(factory); //3、定义Jackson2的序列化对象 Jackson2JsonRedisSerializer<RealTimePumpData> jsonRedisSerializer = new Jackson2JsonRedisSerializer<>(RealTimePumpData.class); ObjectMapper om = new ObjectMapper(); //4、指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); jsonRedisSerializer.setObjectMapper(om); //5、String的序列化方式 StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); //6、配置key 以及 hashkey的序列化方式 redisTemplate.setKeySerializer(stringRedisSerializer); redisTemplate.setHashKeySerializer(stringRedisSerializer); redisTemplate.setValueSerializer(jsonRedisSerializer); redisTemplate.setHashValueSerializer(jsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; } @Bean("wellLidInfoRedisTemplate") public RedisTemplate<String, WellLidInfo> wellLidInfoRedisTemplate(RedisConnectionFactory factory){ //1、创建RedisTemplate对象 RedisTemplate<String, WellLidInfo> redisTemplate = new RedisTemplate<>(); //2、添加Redis的连接工厂 redisTemplate.setConnectionFactory(factory); //3、定义Jackson2的序列化对象 Jackson2JsonRedisSerializer<WellLidInfo> jsonRedisSerializer = new Jackson2JsonRedisSerializer<>(WellLidInfo.class); ObjectMapper om = new ObjectMapper(); //4、指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); jsonRedisSerializer.setObjectMapper(om); //5、String的序列化方式 StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); //6、配置key 以及 hashkey的序列化方式 redisTemplate.setKeySerializer(stringRedisSerializer); redisTemplate.setHashKeySerializer(stringRedisSerializer); redisTemplate.setValueSerializer(jsonRedisSerializer); redisTemplate.setHashValueSerializer(jsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; } }