springdataredis 存储数据

//以cityName:城市名为key,城市code为value 存到redis数据库中
eg: cityName:北京 ,PEK
    public classs redis{
            @Autowired
	 private RedisTemplate redisTemplate;

         public void save(){	
	    RedisConnectionFactory factory =redisTemplate.getConnectionFactory();
            RedisStringCommands redisStringCommands = factory.getConnection();
            RedisSerializer redisSerializer=redisTemplate.getStringSerializer();
                
            Map<String,String> map=CityCodeAndNameUtil.getCityNameCodeMap();
		   for (Map.Entry<String, String> entry : map.entrySet()) {
	           byte[] key =redisSerializer.serialize("cityName:"+entry.getKey());
	    	   byte[] value=redisSerializer.serialize(entry.getValue());
	    	   redisStringCommands.set(key,value);
		}
     }

你可能感兴趣的:(redis)