使用目的: 将bean转化为map作为value 使用stirngredistemplate存储到hash结构的redis中。 1.userDTO类中的属性都为String类型。
Map<String, Object> userMap = BeanUtil.beanToMap(userDTO);//bean转换成map
stringRedisTemplate.opsForHash().putAll(tokenKey, userMap);//存储
stringRedisTemplate.expire(tokenKey, LOGIN_USER_TTL, TimeUnit.MINUTES);//设置有效期
2.由于stirngredistemplate 要求键值对都为string类型,将类中不为string类型的属性转化为String类型。
Map<String, Object> userMap = BeanUtil.beanToMap(userDTO,new HashMap<>(), CopyOptions.create()
.setIgnoreNullValue(true)
.setFieldValueEditor((fieldName,fieldValue)->fieldValue.toString()));