首页 > 其他 > 详细

Map转Bean小工具

时间:2015-08-30 11:10:58      阅读:260      评论:0      收藏:0      [点我收藏+]
public static <T> T converter(Map<String, Object> map, Class<T> clz) {
    T obj = null;
    try {
        obj = clz.newInstance();
        BeanInfo beanInfo = Introspector.getBeanInfo(clz);
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            String key = property.getName();
            if (map.containsKey(key)) {
                Object value = map.get(key);
                // 得到property对应的setter方法
                Method setter = property.getWriteMethod();
                setter.invoke(obj, value);
            }
        }
    } catch (Exception e) {
        logger.error("map转{}异常", clz.getName(), e);
    }
    return obj;
}

  

 

public static <T> List<T> converter(List<Map<String, Object>> list, Class<T> clz) {
    List<T> rst = new ArrayList<>();
    for (int i = 0; i < list.size(); i++) {
        rst.add(converter(list.get(i), clz));
    }
    return rst;
}

  

Map转Bean小工具

原文:http://www.cnblogs.com/spring87/p/4770435.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!