练习redis保存为hash

编程

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());

 

以上是 练习redis保存为hash 的全部内容, 来源链接: utcz.com/z/517615.html

回到顶部