<>方式一: 利用FastJson 把对象转化为Map
/** * * @MethodName: getUserInfoDataByUserId * @Description:
根据useId查询用户信息,封装成map key:属性名,value:属性值 * home.php?mod=space&uid=952169 userId
用户userId * @return */ public Map<String, String> getUserInfoDataByUserId(String
userId) { Map<String, String> userMap = new HashMap<String, String>(); BSysUser
bSysUser = new BSysUser(); if (StringUtils.isNoneBlank(userId)) { bSysUser =
bSysUserService.selectByKey(userId); if (null != bSysUser) { //userMap = new
BeanMap(bSysUser); userMap = JSON.parseObject(JSON.toJSONString(bSysUser), new
TypeReference<Map<String, String>>() { }); } } return userMap; }
<>方式二: 利用反射进行转换
public class BeanMapUtilByReflect { /** * 对象转Map * @param object * @return *
@throws IllegalAccessException */ public static Map beanToMap(Object object)
throws IllegalAccessException { Map<String, Object> map = new HashMap<String,
Object>(); Field[] fields = object.getClass().getDeclaredFields(); for (Field
field : fields) { field.setAccessible(true); map.put(field.getName(),
field.get(object)); } return map; } /** * map转对象 * @param map * @param
beanClass * @param <T> * @return * @throws Exception */ public static <T> T
mapToBean(Map map, Class<T> beanClass) throws Exception { T object =
beanClass.newInstance(); Field[] fields =
object.getClass().getDeclaredFields(); for (Field field : fields) { int mod =
field.getModifiers(); if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
continue; } field.setAccessible(true); if (map.containsKey(field.getName())) {
field.set(object, map.get(field.getName())); } } return object; } }

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信