zoukankan      html  css  js  c++  java
  • Map转Bean小工具

    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;
    }
    

      

  • 相关阅读:
    DotnetCore 单文件发布
    Parallel.ForEach 之 MaxDegreeOfParallelism
    Quartznet速记
    C# SendAysnc 超时
    微耕门禁跨网段搜索代理
    服务守护DOS脚本
    Flutter速记
    ShowDoc速记
    Docker运行dotnetcore
    Redis速记
  • 原文地址:https://www.cnblogs.com/spring87/p/4770435.html
Copyright © 2011-2022 走看看