当前位置 : 主页 > 编程语言 > c++ >

20170830-mapTobean-wy

来源:互联网 收集:自由互联 发布时间:2021-07-03
把map封装成javaBean /**List合并-map-bean 反过来beanToMap相对比较简单些主要是设置bean的filed, 与bean的注入和反射相关可以把bean的set方法写成静态代码段*/ private BrandTop getBean(List totalList, Li
把map封装成javaBean
/**
List合并-map-bean        反过来beanToMap相对比较简单些
主要是设置bean的filed,  与bean的注入和反射相关
可以把bean的set方法写成静态代码段
*/

 private BrandTop getBean(List
 
  > totalList, List
  
   > subList, List
   
    > totalCompList,List
    
     > subCompList) { if (check(totalList) || check(subList)|| check(totalCompList)|| check(subCompList)) { return null; }; List
     
      > totalBeanList = compTwoMap(totalList, totalCompList); List
      
       > subComBeanList = compTwoMap(subList, subCompList); BrandTop brandTop = createBrandTop(totalBeanList.get(0)); List
       
         brandTopList = new ArrayList<>(); for (Map
        
          map : subComBeanList) { brandTopList.add(createBrandTop(map)); } brandTop.setSubList(brandTopList); return brandTop; } private boolean check(List
         
          > checkList) { if (checkList == null || !checkList.isEmpty()) { return false; } return true; } private BrandTop createBrandTop(Map
          
            map) { Method[] methods = BrandTop.class.getMethods(); Map
           
             brandTopMap = new HashMap<>(); for (Method method : methods) { if (method.getName().contains("set")) { brandTopMap.put(method.getName(), method); } } BrandTop brandTop = new BrandTop(); for (Entry
            
              entry : map.entrySet()) { String key = "set" +toUpCase(entry.getKey()); //没有此字段 if ("setCompBrandCate".equals(key)) { continue; } Method method = brandTopMap.get(key); try { setFileds(brandTop, entry, method); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { e.printStackTrace(); } } return brandTop; } private void setFileds(BrandTop brandTop, Entry
             
               entry, Method method) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { String fieldType = method.getParameters()[0].getParameterizedType().getTypeName(); String value = entry.getValue().toString(); if ("java.lang.String".equals(fieldType)) { method.invoke(brandTop, value); } else if ("Date".equals(fieldType)) { Date temp = parseDate(value); method.invoke(brandTop, temp); } else if ("Integer".equals(fieldType) || "int".equals(fieldType)) { Integer intval = (int) (Double.parseDouble(value)); method.invoke(brandTop, intval); } else if ("Long".equalsIgnoreCase(fieldType)) { Long temp = Long.parseLong(value); method.invoke(brandTop, temp); } else if ("Double".equalsIgnoreCase(fieldType)) { Double temp = Double.parseDouble(value); method.invoke(brandTop, temp); } else if ("Boolean".equalsIgnoreCase(fieldType)) { Boolean temp = Boolean.parseBoolean(value); method.invoke(brandTop, temp); } else { System.out.println("not supper type" + fieldType); } } private List
              
               > compTwoMap(List
               
                > totalList, List
                
                 > totalCompList) { for (int index = 0; index < totalCompList.size()&& index < totalList.size(); index++) { for (Entry
                 
                   entry : totalCompList.get(index).entrySet()) { String key = toUpCase(entry.getKey()); totalList.get(index).put("comp" + key, entry.getValue()); } } return totalList; } private String toUpCase(String key) { return String.format("%s%s", key.substring(0, 1).toUpperCase(), key.substring(1)); } /** * 格式化string为Date * * @param datestr * @return */ public static Date parseDate(String datestr) { if (null == datestr || "".equals(datestr)) { return null; } try { String fmtstr; if (datestr.contains(":")) { fmtstr = "yyyy-MM-dd HH:mm:ss"; } else { fmtstr = "yyyy-MM-dd"; } SimpleDateFormat sdf = new SimpleDateFormat(fmtstr, Locale.UK); return sdf.parse(datestr); } catch (Exception e) { return null; } }
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 
上一篇:spring mvc
下一篇:SpringMVC学习笔记(一)
网友评论