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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM