maven依赖
<dependency><groupId>commons-beanutils</groupId><artifactId>commons-beanutils</artifactId><version>1.9.3</version>
</dependency>
工具类
/*** 对象工具类* 王洪岐 20190726*/
public class ObjectUtil {public static <T> T mapToObject(Map map, Class<T> beanClass) throws Exception {if (map == null)return null;T obj = beanClass.newInstance();org.apache.commons.beanutils.BeanUtils.populate(obj, map);return obj;}public static Map<?, ?> objectToMap(Object obj) {if(obj == null)return null;return new org.apache.commons.beanutils.BeanMap(obj);}
}
使用:
ObjectUtil.mapToObject(map, UserInfo.class)