java 实体bean 转map

论坛 期权论坛 脚本     
匿名技术用户   2021-1-5 08:51   42   0
/**
 * 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();
}
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP