开发者社区> 问答> 正文

如何实现beanutils.copyproperties不复制某些字段?

beanutils.copyproperties不复制某些字段?

BeanUtils.copyProperties(information, informationData);

information里面的myclss不需要复制过去,那应该怎么操作呢?

展开
收起
a123456678 2016-03-19 10:19:25 9992 0
1 条回答
写回答
取消 提交回答
  • /**
     * 复制属性,过滤掉不复制的属性
     */
    public static void copyBeanProperties(
    
        final Object source,//1,待复制的原始对象
        final Object target,//2,复制后的结果对象
    
        //3,获取保存你不需要复制的属性名
        final Collection<String> excludes = new ArrayList<String>();
        final PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(source.getClass());
        for(final PropertyDescriptor propertyDescriptor : propertyDescriptors){
            String propName = propertyDescriptor.getName();
            if(!includes.contains(propName)){
                excludes.add(propName);
            }
        }
    
        //4,复制操作
        BeanUtils.copyProperties(source, target, excludes.toArray(new String[excludes.size()]));
    }
    2019-07-17 19:07:32
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载