【java】spring項目中 對entity進行本類間的複製

來源:互聯網
上載者:User

標籤:cep   gif   nts   ons   記錄   b16   product   lan   nullable   

方法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(esAmount1,esAmount);                                    if(Objects.nonNull(esAmount1)){
View Code
BeanUtils.copyProperties(esAmount1,esAmount);  將參數2複製一份給參數1

 

 

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

方法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

 

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

【java】spring項目中 對entity進行本類間的複製

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.