JavaBean和Map相互转换

import org.apache.commons.beanutils.BeanUtils;

private Map beanToMap(Object obj) {
	try {
		return BeanUtils.describe(obj);
	} catch (IllegalAccessException | InvocationTargetException
			| NoSuchMethodException e) {
		e.printStackTrace();
	}
	return null;
}

private List mapToBean(List> data, Class clazz) {
	List list = new ArrayList<>();
	try {
		for (int i = 0; i < data.size(); i++) {
			Object newInstance = clazz.newInstance();
			Map map = data.get(i);
			BeanUtils.populate(newInstance, map);
			list.add(newInstance);
		}
		return list;
	} catch (Exception e) {
	}
	return null;
}
 
  

                            
                        
                    
                    
                    

你可能感兴趣的:(java实战案例,java工具)