您的位置:首页 > 其它

BeanUtils使用之复制两个实体类中的属性

2016-01-05 15:00 197 查看
package com.guozz.test;

public class TestA {

private String a;

private String b;

public String getA() {
return a;
}

public void setA(String a) {
this.a = a;
}

public String getB() {
return b;
}

public void setB(String b) {
this.b = b;
}
}


package com.guozz.test;

public class TestB {

private String a;

private String b;

public String getA() {
return a;
}

public void setA(String a) {
this.a = a;
}

public String getB() {
return b;
}

public void setB(String b) {
this.b = b;
}

}


package com.guozz.test;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;

public class Test {

public static void main(String[] args) throws IllegalAccessException, InvocationTargetException {
TestA testA = new TestA();
testA.setA("aa");
testA.setB("bb");
TestB testB = new TestB();
BeanUtils.copyProperties(testB, testA);
System.out.println(testB.getA());
System.out.println(testB.getB());
}
}


输出

aa
bb
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: