您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> Java >> 如何实现beanutilscopyproperties不复制某些字段?

如何实现beanutilscopyproperties不复制某些字段?

来源:网络整理     时间:2016/5/18 22:46:20     关键词:properties,beanutils

关于网友提出的“如何实现beanutilscopyproperties不复制某些字段?”问题疑问,本网通过在网上对“如何实现beanutilscopyproperties不复制某些字段?”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题:如何实现beanutilscopyproperties不复制某些字段?
描述:

beanutils.copyproperties不复制某些字段?

BeanUtils.copyProperties(information, informationData);

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


解决方案1:

引用来自“孙亮”的评论

beanutils,commons beanutils,beanutils.populate,beanutils包下载,beanutils下载,beanutils.jar,beanutils api,java beanutils,common beanutils,beanutils.describe,apache beanutils,spring beanutils,beanutils中文api,beanutils源码,beanutils的作

BeanUtils.copyProperties 方法里有忽略掉的属性。直接写在对应的位置就可以了啊

例如:BeanUtils.copyProperties(product, pProduct, new String[] { "id", "createDate", "modifyDate" });

解决方案2:

beanutils,commons beanutils,beanutils.populate,beanutils包下载,beanutils下载,beanutils.jar,beanutils api,java beanutils,common beanutils,beanutils.describe,apache beanutils,spring beanutils,beanutils中文api,beanutils源码,beanutils的作

BeanUtils.copyProperties 方法里有忽略掉的属性。直接写在对应的位置就可以了啊

例如:BeanUtils.copyProperties(product, pProduct, new String[] { "id", "createDate", "modifyDate" });

解决方案3:

/**
 * 复制属性,过滤掉不复制的属性
 */
public static void copyBeanProperties(
    final Object source,//1,待复制的原始对象
    final Object target,//2,复制后的结果对象
    //3,获取保存你不需要复制的属性名
    final Collection excludes = new ArrayList();
    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()]));
}


以上介绍了“如何实现beanutilscopyproperties不复制某些字段?”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/1168292.html

相关图片

相关文章