Person person = new Person();
person.setName("zhangsan");
person.setAge(18);
ObjectMapper objectMapper = new ObjectMapper();
//实体类转map使用jackson2HashMapper,需要一个ObjectMapper,所以实例化一个
Jackson2HashMapper jackson2HashMapper = new Jackson2HashMapper(objectMapper, false);
//实体类转Map
Map<String, Object> map = jackson2HashMapper.toHash(person);
//重写序列化器
stringRedisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
//缓存数据
stringRedisTemplate.opsForHash().putAll("zs",map);
//读取缓存数据
Map zs = stringRedisTemplate.opsForHash().entries("zs");
//map转实体
Person p = objectMapper.convertValue(zs, Person.class);
System.out.println(p.toString());
来源:oschina
链接:https://my.oschina.net/u/3141521/blog/4316644