1. 程式人生 > >不同實體類相同屬性之間的快速賦值

不同實體類相同屬性之間的快速賦值

關鍵:使用org.springframework.beans.BeanUtils

下面描述的是兩個實體類的相同屬性之間的快速賦值:

實體類1:

public class TrackConditionModel {

    private Long groupCustomerId;
    private String indexCode;
    private String indexName;
    private String enumValue; 
    private String name;
    private String dataType; 
    private Integer trackType;
    private Integer imageType;
    private Date startTime;
    private Date endTime;
    private Date created;

    public TrackConditionModel() {
    }

    public TrackConditionModel(Long groupCustomerId, String indexCode, String indexName, Integer trackType, Integer imageType, Date created,String imageName) {
        this.groupCustomerId = groupCustomerId;
        this.indexCode = indexCode;
        this.indexName = indexName;
        this.trackType = trackType;
        this.imageType = imageType;
        this.created = created;
    }

    public Long getGroupCustomerId() {
        return groupCustomerId;
    }

    public void setGroupCustomerId(Long groupCustomerId) {
        this.groupCustomerId = groupCustomerId;
    }

    public String getIndexCode() {
        return indexCode;
    }

    public void setIndexCode(String indexCode) {
        this.indexCode = indexCode;
    }

    public Integer getTrackType() {
        return trackType;
    }

    public void setTrackType(Integer trackType) {
        this.trackType = trackType;
    }

    public Integer getImageType() {
        return imageType;
    }

    public void setImageType(Integer imageType) {
        this.imageType = imageType;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public String getIndexName() {
        return indexName;
    }

    public void setIndexName(String indexName) {
        this.indexName = indexName;
    }

    public String getEnumValue() {
        return enumValue;
    }

    public void setEnumValue(String enumValue) {
        this.enumValue = enumValue;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDataType() {
        return dataType;
    }

    public void setDataType(String dataType) {
        this.dataType = dataType;
    }
}

實體類2:

public class TrackConditionVo extends TrackConditionModel {
    private Integer page;
    private Integer rows;
    private String trackSubmit;

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getRows() {
        return rows;
    }

    public void setRows(Integer rows) {
        this.rows = rows;
    }

    public String getTrackSubmit() {
        return trackSubmit;
    }

    public void setTrackSubmit(String trackSubmit) {
        this.trackSubmit = trackSubmit;
    }
}

測試

import org.springframework.beans.BeanUtils;

public class test{

    public static void main(String[] args) {
		//屬性少的賦值給屬性多的
        TrackConditionVo trackConditionVo = new TrackConditionVo();
        TrackConditionModel trackConditionModel = new TrackConditionModel();
        trackConditionModel.setName("111");
        trackConditionModel.setIndexCode("AGE");
        trackConditionModel.setGroupCustomerId(1L);
        BeanUtils.copyProperties(trackConditionModel,trackConditionVo);
        System.out.println(trackConditionVo.getName());
        System.out.println(trackConditionVo.getIndexCode());

		//屬性多的賦值給屬性少的
        TrackConditionVo trackConditionVo2 = new TrackConditionVo();
        TrackConditionModel trackConditionModel2 = new TrackConditionModel();
        trackConditionVo2.setName("111");
        trackConditionVo2.setIndexCode("AGE");
        trackConditionVo2.setGroupCustomerId(1L);
        BeanUtils.copyProperties(trackConditionVo2,trackConditionModel2);
        System.out.println(trackConditionModel2.getName());
        System.out.println(trackConditionModel2.getIndexCode());
    }
}