java Beanutils.copyProperties( )用法

1.用法

在这里插入图片描述

BeanUtils.copyProperties(“要转换的类”, “转换后的类”);

2.扩展:Spring 的BeanUtils.copyProperties在拷贝属性时忽略空值

代码

public class CommonUtils {

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

用法:

BeanUtils.copyProperties(quote, existQuote, CommonUtils.getNullPropertyNames(quote));
posted @ 2019-09-09 13:59  叶落无蝉鸣  阅读(60)  评论(0编辑  收藏  举报