java 使用PropertyUtilsBean将泛型T转换为Map

包名:commons-beanutils-1.9.3.jar

类名:org.apache.commons.beanutils.PropertyUtilsBean.PropertyUtilsBean()

转换代码:

private <T> Map<String, Object> conversionToMap(T bean) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
		PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(bean);

		for (PropertyDescriptor d : descriptors) {
			String fieldName = d.getName();
			Object value = propertyUtilsBean.getNestedProperty(bean, fieldName);
			if (!"class".equals(fieldName))
				map.put(fieldName, value);
		}
		return map;
	}

注:该方法可以用来做通用excel导出,后面会加上导出通用代码

猜你喜欢

转载自blog.csdn.net/DavidSoCool/article/details/81538463