javaBean属性复制 工具类


/**
 * Bean属性复制 工具类 <br>
 */
@Slf4j
public class BeanCopyUtils {

    /**
     * 将源POJO对象数据复制给目标POJO对象的同名属性
     *
     * @param source 源
     * @param target 目标
     * @param <S>
     * @param <T>
     * @return 目标
     * @date 2018年12月4日
     * @version 1.0
     */
    public static <S, T> T copy(S source, T target) {
        if (null == source || null == target) {
            return null;
        }
        BeanUtil.copyProperties(source, target);
        return target;
    }

    /**
     * 将源POJO对象数据复制给目标POJO对象的同名属性
     *
     * @param source :
     * @param clazz :
     * @return T
     * @author Roc
     * @date 2020/11/3
     **/
    public static <T, S> T copy(S source, Class<T> clazz) throws IllegalAccessException, InstantiationException {
        T t = clazz.newInstance();
        return copy(source, t);
    }


    /**
     * 将源POJO对象数据复制给目标POJO对象的同名属性
     *
     * @param source :
     * @param clazz :
     * @return T
     * @author Roc
     * @date 2020/11/3
     **/
    public static <T, S> T copy(S source, Class<T> clazz, String... ignoreProperties) throws IllegalAccessException, InstantiationException {
        T t = clazz.newInstance();
        return copy(source, t, ignoreProperties);
    }


    /**
     * 将源POJO对象数据复制给目标POJO对象的同名属性
     *
     * @param source 源
     * @param target 目标
     * @param ignoreProperties 无需转换的属性
     * @param <S>
     * @param <T>
     * @return 目标
     * @date 2019-1-29
     */
    public static <S, T> T copy(S source, T target, String... ignoreProperties) {
        if (null == source || null == target) {
            return null;
        }
        BeanUtil.copyProperties(source, target, ignoreProperties);
        return target;
    }


    /**
     * 将源POJO对象数据复制给目标POJO对象的同名属性
     *
     * @param source 源
     * @param target 目标
     * @param ignoreNullProperties 是否无视null值字段(如果为true则无视)
     * @param <S>
     * @param <T>
     * @return 目标
     * @date 2019-1-29
     */
    public static <S, T> T copy(S source, T target, boolean ignoreNullProperties) {
        if (null == source || null == target) {
            return null;
        }
        BeanUtil.copyProperties(source, target, CopyOptions.create().setIgnoreNullValue(ignoreNullProperties));
        return target;
    }

    /**
     * 对象List复制
     *
     * @param ss
     * @param cls
     * @param <S>
     * @param <T>
     * @return
     */
    public static <S, T> List<T> copyList(List<S> ss, Class<T> cls) {
        if (CollectionUtil.isEmpty(ss)) {
            return new ArrayList<>();
        }
        List<T> tRes = new ArrayList<>();
        try {
            for (S s : ss) {
                T t = cls.newInstance();
                BeanUtil.copyProperties(s, t);
                tRes.add(t);
            }
        } catch (Exception e) {
            log.info("类型转换异常,异常信息: ", e);
        }

        return tRes;
    }


    /**
     * 对象List复制
     *
     * @param ss
     * @param cls
     * @param ignoreProperties
     * @param <S>
     * @param <T>
     * @return
     */
    public static <S, T> List<T> copyList(List<S> ss, Class<T> cls, String... ignoreProperties) {
        if (CollectionUtil.isEmpty(ss)) {
            return new ArrayList<>();
        }
        List<T> tRes = new ArrayList<>();
        try {
            for (S s : ss) {
                T t = cls.newInstance();
                BeanUtil.copyProperties(s, t, ignoreProperties);
                tRes.add(t);
            }
        } catch (Exception e) {
            log.info("类型转换异常,异常信息: ", e);
        }

        return tRes;
    }

    /**
     * 对象List复制
     *
     * @param ss
     * @param cls
     * @param ignoreNullProperties
     * @param <S>
     * @param <T>
     * @return
     */
    public static <S, T> List<T> copyList(List<S> ss, Class<T> cls, boolean ignoreNullProperties) {
        if (CollectionUtil.isEmpty(ss)) {
            return new ArrayList<>();
        }
        List<T> tRes = new ArrayList<>();
        try {
            for (S s : ss) {
                T t = cls.newInstance();
                BeanUtil.copyProperties(s, t, CopyOptions.create().setIgnoreNullValue(ignoreNullProperties));
                tRes.add(t);
            }
        } catch (Exception e) {
            log.info("类型转换异常,异常信息: ", e);
        }

        return tRes;
    }


    /**
     * 序列化为byte[]
     *
     * @param object :
     * @return byte[]
     * @date 2020/5/27
     **/
    public static byte[] serialize(Object object) {
        if (null == object) {
            return null;
        }
        ObjectOutputStream oos = null;
        ByteArrayOutputStream bos = null;
        try {
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(object);
            return bos.toByteArray();
        } catch (IOException e) {
            log.error("Serialization failed: ", e);
            return null;
        } finally {
            IoUtil.close(oos);
            IoUtil.close(bos);
        }
    }

    /**
     * 反序列化为Object
     *
     * @param bytes :
     * @return java.lang.Object
     * @date 2020/5/27
     **/
    public static <T> T deserialize(byte[] bytes, Class<T> classType) {
        if (null == bytes) {
            return null;
        }
        ByteArrayInputStream bais = null;
        try {
            bais = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bais);
            return (T) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            log.error("Bytes Could not deserialize: ", e);
            return null;
        } finally {
            IoUtil.close(bais);
        }
    }


    /**
     * 深度拷贝List
     *
     * @param src :
     * @return List<T>
     * @date 2020/5/27
     **/
    public static <T> List<T> deepCopyList(List<T> src) {
        if (CollectionUtil.isEmpty(src)) {
            return new ArrayList<>();
        }

        ObjectOutputStream out = null;
        try {
            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
            out = new ObjectOutputStream(byteOut);
            out.writeObject(src);
            ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
            ObjectInputStream in = new ObjectInputStream(byteIn);
            return (List<T>) in.readObject();
        } catch (IOException | ClassNotFoundException e) {
            log.error("Bytes Could not deserialize: ", e);
            return null;
        } finally {
            IoUtil.close(out);
        }

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java复制一个JavaBean可以使用以下两种方法: 1. 手动复制属性值 手动复制属性值是一种简单但繁琐的方法。对于每个属性,我们需要从原始对象中获取它的值并将其设置到目标对象中。这种方法适用于只有很少属性需要复制的情况。 例如: ``` public class Person { private String name; private int age; private String address; // getters and setters } // 手动复制属性值 Person source = new Person(); source.setName("张三"); source.setAge(18); source.setAddress("北京"); Person target = new Person(); target.setName(source.getName()); target.setAge(source.getAge()); target.setAddress(source.getAddress()); ``` 2. 使用BeanUtils.copyProperties() Apache Commons BeanUtils提供了一个方便的工具类,可以将一个JavaBean属性复制到另一个对象中。这个方法使用Java反射机制来获取和设置属性值,因此它比手动复制属性值更方便,但可能会更慢。 例如: ``` import org.apache.commons.beanutils.BeanUtils; public class Person { private String name; private int age; private String address; // getters and setters } // 使用BeanUtils.copyProperties() Person source = new Person(); source.setName("张三"); source.setAge(18); source.setAddress("北京"); Person target = new Person(); BeanUtils.copyProperties(target, source); ``` 使用BeanUtils.copyProperties()方法时,需要注意两个对象的属性名和数据类型应该相同,否则可能会抛出异常。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值