java两个实体对象有映射关系,则怎么用反射把一个对象的数据拿到,赋给另一个对象?
publicclassDomain1{//idpublicLongid;//资源编码privateStringresCode;//重量privateDoubleweigh...
public class Domain1 {
//id
public Long id;
//资源编码
private String resCode;
//重量
private Double weight;
//投放日期
private Date putDate;
//是否可使用银票支付
private Boolean flagYinpiao;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getResCode() {
return resCode;
}
public void setResCode(String resCode) {
this.resCode = resCode;
}
public Double getWeight() {
return weight;
}
public void setWeight(Double weight) {
this.weight = weight;
}
public Date getPutDate() {
return putDate;
}
public void setPutDate(Date putDate) {
this.putDate = putDate;
}
public Boolean getFlagYinpiao() {
return flagYinpiao;
}
public void setFlagYinpiao(Boolean flagYinpiao) {
this.flagYinpiao = flagYinpiao;
}
}
public class Domain2 {
//id
public Long id2;
//资源编码
private String resCode2;
//重量
private Double weight2;
//投放日期
private Date putDate2;
//是否可使用银票支付
private Boolean flagYinpiao2;
public Long getId2() {
return id2;
}
public void setId2(Long id2) {
this.id2 = id2;
}
public String getResCode2() {
return resCode2;
}
public void setResCode2(String resCode2) {
this.resCode2 = resCode2;
}
public Double getWeight2() {
return weight2;
}
public void setWeight2(Double weight2) {
this.weight2 = weight2;
}
public Date getPutDate2() {
return putDate2;
}
public void setPutDate2(Date putDate2) {
this.putDate2 = putDate2;
}
public Boolean getFlagYinpiao2() {
return flagYinpiao2;
}
public void setFlagYinpiao2(Boolean flagYinpiao2) {
this.flagYinpiao2 = flagYinpiao2;
}
}
domain1里有5个对象,类型都不同,把domain1的数据拿到,赋给domain2,这5字段意义对应,但是字段名称不一样。这只是一个例子,我两个对象,都有一百多个属性,不能一个一个写。怎么通过反射把domain1每个字段的值赋给domain2,映射关系用配置文件写 展开
//id
public Long id;
//资源编码
private String resCode;
//重量
private Double weight;
//投放日期
private Date putDate;
//是否可使用银票支付
private Boolean flagYinpiao;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getResCode() {
return resCode;
}
public void setResCode(String resCode) {
this.resCode = resCode;
}
public Double getWeight() {
return weight;
}
public void setWeight(Double weight) {
this.weight = weight;
}
public Date getPutDate() {
return putDate;
}
public void setPutDate(Date putDate) {
this.putDate = putDate;
}
public Boolean getFlagYinpiao() {
return flagYinpiao;
}
public void setFlagYinpiao(Boolean flagYinpiao) {
this.flagYinpiao = flagYinpiao;
}
}
public class Domain2 {
//id
public Long id2;
//资源编码
private String resCode2;
//重量
private Double weight2;
//投放日期
private Date putDate2;
//是否可使用银票支付
private Boolean flagYinpiao2;
public Long getId2() {
return id2;
}
public void setId2(Long id2) {
this.id2 = id2;
}
public String getResCode2() {
return resCode2;
}
public void setResCode2(String resCode2) {
this.resCode2 = resCode2;
}
public Double getWeight2() {
return weight2;
}
public void setWeight2(Double weight2) {
this.weight2 = weight2;
}
public Date getPutDate2() {
return putDate2;
}
public void setPutDate2(Date putDate2) {
this.putDate2 = putDate2;
}
public Boolean getFlagYinpiao2() {
return flagYinpiao2;
}
public void setFlagYinpiao2(Boolean flagYinpiao2) {
this.flagYinpiao2 = flagYinpiao2;
}
}
domain1里有5个对象,类型都不同,把domain1的数据拿到,赋给domain2,这5字段意义对应,但是字段名称不一样。这只是一个例子,我两个对象,都有一百多个属性,不能一个一个写。怎么通过反射把domain1每个字段的值赋给domain2,映射关系用配置文件写 展开
1个回答
展开全部
这个没什么复杂的,你的核心意思是需要对二个对象的实例进行copy动作,做法如下:
1、对第一个类进行反射获取属性的值
2、对第二个类进行反射设置属性的值(将1中的值设入,采用配置文件进行关系映射)
建议你看下java反射机制,不用太理解他的含义,了解怎么用就可以,百度一下“java反射代码”。
下面2个网站可以看看:
http://847353020-qq-com.iteye.com/blog/1003440
http://yuncode.net/code/c_51fca6e300b5b88
1、对第一个类进行反射获取属性的值
2、对第二个类进行反射设置属性的值(将1中的值设入,采用配置文件进行关系映射)
建议你看下java反射机制,不用太理解他的含义,了解怎么用就可以,百度一下“java反射代码”。
下面2个网站可以看看:
http://847353020-qq-com.iteye.com/blog/1003440
http://yuncode.net/code/c_51fca6e300b5b88
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询