导包 :
import org.springframework.beans.BeanUtils;
使用,把t1的属性值复制到t2里面
BeanUtils.copyProperties(t1,t2);
注 : 效率可能不是很高,效率的话mapStruct,这个好像很多人推荐
测试代码
@Getter
@Setter
public static class Test {
private List<Data> datas;
}
@Getter
@Setter
public static class Data {
private String id;
private Double d;
private Source s;
}
@Getter
@Setter
public static class Source {
private String u;
private String i;
private String c;
}
public static void main(String[] args) {
Test t1 = new Test();
Data d1 = new Data();
Source s1 = new Source();
s1.setC("1");
s1.setI("2");
s1.setU("3");
d1.setS(s1);
d1.setD(1.2);
d1.setId("11");
Data d2 = new Data();
Source s2 = new Source();
s2.setC("1");
s2.setI("2");
s2.setU("3");
d2.setS(s2);
d2.setD(1.2);
d2.setId("11");
List<Data> datas = new LinkedList<>();
datas.add(d1);
datas.add(d2);
t1.setDatas(datas);
Test t2 = new Test();
BeanUtils.copyProperties(t1, t2);
System.err.println(JSONObject.toJSONString(t1));
System.err.println(JSONObject.toJSONString(t2));
}