zoukankan      html  css  js  c++  java
  • BeanUtils.copyProperties 忽略null值

    // 获取控制字段列表
    public static String[] getNullPropertyNames(Object source) {
      final BeanWrapper src = new BeanWrapperImpl(source);
      PropertyDescriptor[] pds = src.getPropertyDescriptors();
      Set<String> emptyNames = new HashSet<String>();
      for(PropertyDescriptor pd : pds) {
        Object srcValue = src.getPropertyValue(pd.getName());
        if (srcValue == null) {
          emptyNames.add(pd.getName());
        }
      }
      String[] result = new String[emptyNames.size()];
      return emptyNames.toArray(result);
    }
    
    
    // 新增字段拷贝,只复制不为空的字段
    LimitRuleDO entity = new LimitRuleDO();
    LimitRuleCommonDo limitRuleCommonDo = new LimitRuleCommonDo();
    // 获取空值字段
    String[] names = getNullPropertyNames(limitRuleCommonDo);
    // 忽略空值字段
    BeanUtils.copyProperties(limitRuleCommonDo,entity,names);
    
    通过知识/经验的分享,节省开发者的时间.
  • 相关阅读:
    HDU 1058
    Codeforces 349C
    HDU 2602
    HDU 2571
    HDU 2955
    HDU 2084
    HDU 1003
    HDU 1506 & 1505
    POJ 1854
    HDU 2095
  • 原文地址:https://www.cnblogs.com/ysloong/p/14742483.html
Copyright © 2011-2022 走看看