一、配置
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory"></property> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> </bean>
方式一:
二、插入
/** * 将检验报告存入redis * @param response * @param key */ private void setRedis(ResponseResult<LisReponse> response,String key,String patientType){ String redisKey = key; String mapKey = key + "-" + patientType + "-"+ (new Date().getTime()); BoundHashOperations<String, String, Object> boundHashOperations = redisTemplate.boundHashOps(redisKey); Map<String, Object> data = new HashMap<String, Object>(); List<LisReponse> list = response.getItems(); //根据检验日期倒序 Collections.sort(list,new Comparator<LisReponse>() { @Override public int compare(LisReponse o1, LisReponse o2) { return DateUtil.parseDate(o2.getCheck_time(), "yyyy/MM/dd HH:mm:ss").compareTo(DateUtil.parseDate(o1.getCheck_time(), "yyyy/MM/dd HH:mm:ss")); } }); data.put(mapKey, list); boundHashOperations.putAll(data); }
三、获取
/** * 从redis获取结果 * @param key * @return */ private Map<String ,Object> getResultFromRedis(String key){ BoundHashOperations<String, String, Object> boundHashOperations = redisTemplate.boundHashOps(key); Map<String, Object> data = boundHashOperations.entries(); return data; }