spring-两个对象之间的属性拷贝工具

import java.util.Arrays;
import java.util.List;


import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;


public class PropertiesCopyUtil {


    /**
     * 属性复制
     * 
     * @param source
     * @param clazz
     * @return
     */
    public static <T> T copyProperties(Object source, Class<T> clazz) {
        if (source == null) {
            return null;
        }
        try {
            T target = BeanUtils.instantiate(clazz);
            BeanUtils.copyProperties(source, target);
            return target;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 忽略部分属性
     * 
     * @param source
     * @param clazz
     * @param ignoreProperties
     * @return
     */
    public static <T> T copyPropertiesIgnore(Object source, Class<T> clazz,
            String... ignoreProperties) {
        if (source == null) {
            return null;
        }
        try {
            T target = BeanUtils.instantiate(clazz);
            BeanUtils.copyProperties(source, target, ignoreProperties);
            return target;
        } catch (Exception e) {
        }
        return null;
    }


    /**
     * 复制部分属性
     * 
     * @param source
     * @param clazz
     * @param specificProperties
     * @return
     */
    public static <T> T copyPropertiesSpecific(Object source, Class<T> clazz,
            String... specificProperties) {
        if (source == null) {
            return null;
        }
        try {
            T target = BeanUtils.instantiate(clazz);
            if (specificProperties == null) {
                return target;
            }
            List<String> specificList = Arrays.asList(specificProperties);
            copySpecificProperties(source, target, specificList);
            return target;
        } catch (Exception e) {
        }
        return null;
    }


    private static void copySpecificProperties(final Object source,
            final Object target, final Iterable<String> properties) {


        final BeanWrapper src = new BeanWrapperImpl(source);
        final BeanWrapper trg = new BeanWrapperImpl(target);


        for (final String propertyName : properties) {
            trg.setPropertyValue(propertyName,
                    src.getPropertyValue(propertyName));
        }


    }
}
可以使用 Java 对象属性拷贝工具,比如 Apache Commons BeanUtils 或 Spring Framework 中的 BeanUtils。具体实现步骤如下: 1. 导入对应的工具包,比如 Apache Commons BeanUtils 或 Spring Framework 中的 BeanUtils。 2. 定义源对象和目标对象。 3. 使用工具包中的属性拷贝方法,将源对象中需要赋值的属性拷贝到目标对象中相应的属性上。 4. 如果需要将源对象的部分属性赋值给目标对象的部分属性,可以使用工具包中的过滤器或忽略属性方法,只拷贝需要的属性。 下面是一个使用 Apache Commons BeanUtils 实现对象属性拷贝的示例代码: ```java import org.apache.commons.beanutils.BeanUtils; // 定义源对象和目标对象 Person sourcePerson = new Person(); sourcePerson.setName("张三"); sourcePerson.setAge(20); sourcePerson.setAddress("北京市"); Person targetPerson = new Person(); // 将源对象的部分属性赋值给目标对象的部分属性 try { // 过滤不需要拷贝属性 String[] ignoreProperties = {"address"}; BeanUtils.copyProperties(targetPerson, sourcePerson, ignoreProperties); } catch (Exception e) { e.printStackTrace(); } System.out.println(targetPerson.getName()); // 输出:张三 System.out.println(targetPerson.getAge()); // 输出:20 System.out.println(targetPerson.getAddress()); // 输出:null ``` 其中,`BeanUtils.copyProperties()` 方法用于将源对象属性拷贝到目标对象中,第三个参数 `ignoreProperties` 用于过滤不需要拷贝属性。在本示例中,我们过滤了源对象的 `address` 属性,只将 `name` 和 `age` 两个属性赋值给目标对象
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值