一、新建测试实体
1.UserA
package com.dechy.hebswj.test; public class UserA { 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; } }
2.UserB
package com.dechy.hebswj.test; public class UserB { private String a; private String b; private String c; 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; } public String getC() { return c; } public void setC(String c) { this.c = c; } @Override public String toString() { return "UserB [a=" + a + ", b=" + b + ", c=" + c + "]"; } }
二、测试
package com.dechy.hebswj.test; import org.apache.commons.beanutils.BeanUtils; import org.junit.Test; public class userTest { @Test public void test() throws Exception{ UserA userA=new UserA(); userA.setA("a"); userA.setB("b"); UserB userB=new UserB(); BeanUtils.copyProperties(userB, userA); System.out.println(userB); } }
结果为
UserB [a=a, b=b, c=null]