• 【java】spring项目中 对entity进行本类间的克隆


    方法1:

    【使用spring自带BeanUtils实现克隆】

    【要求:需要被克隆的类实现Cloneable接口并且重写clone()方法】

    》例子:

    》》实体:

    package com.agen.orderdiscount.entity;
    
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import lombok.experimental.Accessors;
    import org.hibernate.annotations.GenericGenerator;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import java.util.Date;
    
    /**
     * 预估金额
     * 订单创建成功 往预估金额表中存入记录
     * @author SXD
     * @date 2018/1/16
     */
    @Data(staticConstructor = "of")
    @NoArgsConstructor
    @AllArgsConstructor
    @Accessors(chain = true)
    @Entity
    @GenericGenerator(name = "uuid2", strategy = "org.hibernate.id.UUIDGenerator" )
    public class EsAmount implements Cloneable{
    
        /**
         * 预估ID
         */
        @Id
        @GeneratedValue(generator = "uuid2")
        @Column(length = 36)
        private String Id;
        /**
         * 本次转入预估金额
         */
        @Column(nullable = false,precision = 10,scale = 2)
        private Double esAccount;
        /**
         * 本次转入时间
         */
        @Column(nullable = false)
        private Date esDate;
        /**
         * 订单ID
         */
        @Column(nullable = false)
        private Integer orderId;
        /**
         * 订单编号SN
         */
        @Column(nullable = false,length = 20)
        private String orderSn;
        /**
         * 机构ID
         */
        @Column(nullable = false)
        private Integer adminId;
        /**
         * 产品ID
         */
        @Column(nullable = false)
        private Integer productId;
        /**
         * 会员ID
         */
        @Column(nullable = false)
        private Integer memberId;
        /**
         * 采样包ID
         */
        @Column(nullable = false)
        private Integer cybId;
        /**
         * 操作来源
         */
        @Column(nullable = false,length = 20)
        private String esOperater;
        /**
         * 预估金额备注1
         */
        @Column(length = 500)
        private String esCre1;
        /**
         * 预估金额备注2
         */
        @Column(length = 500)
        private String esCre2;
    
        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    
    
    
    
    }
    View Code

    》》使用:

    EsAmount esAmount = esAmountRepository.findEsAmountByAdminIdAndOrderIdAndProductId(adminId,orderId,productId);
                                    if(Objects.nonNull(esAmount)){
                                        //1.1 将预估金额扣除到可提现金额的记录也记录到预估金额记录表中
                                        EsAmount esAmount1 = new EsAmount();
                                        BeanUtils.copyProperties(esAmount,esAmount1);
                                        if(Objects.nonNull(esAmount1)){
    View Code
    BeanUtils.copyProperties(esAmount,esAmount1);  将参数1克隆一份给参数2

    =====================================================================================

    方法2:

    【使用java.lang.Serializable实现深度克隆】

    【要求,不需要类实现Cloneable,只需要它们实现java.lang.Serializable即可】

    》工具类:

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    public abstract class BeanUtil {
        @SuppressWarnings("unchecked")
        public static <T> T cloneTo(T src) throws RuntimeException {
            ByteArrayOutputStream memoryBuffer = new ByteArrayOutputStream();
            ObjectOutputStream out = null;
            ObjectInputStream in = null;
            T dist = null;
            try {
                out = new ObjectOutputStream(memoryBuffer);
                out.writeObject(src);
                out.flush();
                in = new ObjectInputStream(new ByteArrayInputStream(memoryBuffer.toByteArray()));
                dist = (T) in.readObject();
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                if (out != null)
                    try {
                        out.close();
                        out = null;
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                if (in != null)
                    try {
                        in.close();
                        in = null;
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
            }
            return dist;
        }
    }
    View Code

    》使用:

    Administrator src = new Administrator(new User("Kent", "123456", new Date()), true);
            Administrator dist = BeanUtil.cloneTo(src);
    View Code

    =====================================================================================

  • 相关阅读:
    Jetty和tomcat的比较
    Spring Boot – Jetty配置
    Java规则之条件语句中做空判断时使用||和&&常犯的错误
    bboss oreach循环嵌套遍历map
    url全部信息打印
    ajax省市县三级联动
    关于mysql中的count()函数
    vue——统一配置axios的baseUrl和所有请求的路径
    js——substr与substring的区别
    vue——axios请求成功却进入catch的原因
  • 原文地址:https://www.cnblogs.com/sxdcgaq8080/p/8316960.html
Copyright © 2020-2023  润新知