java 使用PropertyUtilsBean將泛型T轉換為Map
阿新 • • 發佈:2019-01-04
包名: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匯出,後面會加上匯出通用程式碼