標籤: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進行本類間的複製