您的位置:首页 > 其它

不同实体类相同属性之间的快速赋值

2017-06-09 08:41 369 查看
关键:使用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());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: