<> Mode 1 : utilize FastJson Convert objects to Map
/** * * @MethodName: getUserInfoDataByUserId * @Description:
according to useId Query user information , Packaged as map key: Attribute name ,value: Attribute value * home.php?mod=space&uid=952169 userId
user 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; }
<> Mode II : Converting with reflection
public class BeanMapUtilByReflect { /** * Object transfer 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 To object * @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; } }

Technology
Daily Recommendation