JavaBean和Map互转
class BeanUtil {
/**
* 将对象转换为map
*
* @param bean
* @return
*/
public static <T> Map<String, Object> bean2Map(T bean) {
Map<String, Object> map = new HashMap<>();
if (bean != null) {
BeanMap beanMap = BeanMap.create(bean);
for (Object key : beanMap.keySet()) {
map.put(key.toString(), beanMap.get(key));
}
}
return map;
}
/**
* 将map转换为javabean对象
*
* @param map
* @param bean
* @return
*/
public static <T> T map2Bean(Map<String, Object> map, T bean) {
BeanMap beanMap = BeanMap.create(bean);
beanMap.putAll(map);
return bean;
}
}
学习使我充实,分享给我快乐!