/**
* javaBean 转 Map
*
* @param target 所要的结果集
* @param source 数据来源
* @param c 数据来源 Class
* @throws IntrospectionException
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
private void beanToMap(Map<String, Object> target, Object source, Class<?> c) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
if (source == null || target == null || c == null) {
return;
}
BeanInfo beanInfo = Introspector.getBeanInfo(c, Object.class);
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor pd : pds) {
Object value = pd.getReadMethod().invoke(source, null);
target.put(pd.getName(), value);
}
}
调用方式
Map<String, Object> map = new HashMap<>();
NovaShopOrder novaShopOrder = novaShopOrderMapper.selectByVcOrderId(vcOrderId);
try {
//该方法执行后,实体bean的值会遍历到map中
beanToMap(map, novaShopOrder, NovaShopOrder.class);
} catch (Exception e) {
e.printStackTrace();
}