zoukankan      html  css  js  c++  java
  • 【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

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

  • 相关阅读:
    HTTP协议
    python中os模块简介
    什么是HTML
    Django框架
    python递归
    web工程中的各种路径(eclipse开发)
    小白向:web中利用request.getPart()上传文件到服务器
    利用 html+css 画同心圆(concentric circles)——绝对布局与相对布局
    理解z-index和css中的层叠顺序问题(大神技术博的读后感?)
    二、系统初始化
  • 原文地址:https://www.cnblogs.com/sxdcgaq8080/p/8316960.html
Copyright © 2011-2022 走看看