用法:将source对象中的属性复制给target对象,避免多次set、get
源码:
public static void copyProperties(Object source, Object target) throws BeansException {
copyProperties(source, target, (Class)null, (String[])null);
}
测试:
public class BeanUtils {
@Test
public void testCopyProperties(){
Employee employee = new Employee();
employee.setAge(10);
employee.setName("mc");
employee.setDept("test");
User user = new User();
System.out.println("user==>"+user);
System.out.println("employee==>"+employee);
BeanUtils.copyProperties(employee,user);
System.out.println("user2==>"+user);
System.out.println("employee2==>"+employee);
}
}
@Data
class Employee{
public String name;
public String dept;
public Integer age;
}
@Data
class User{
public String name;
public Integer age;
}
执行结果
user==>User(name=null, age=null)
employee==>Employee(name=mc, dept=test, age=10)
user2==>User(name=mc, age=10)
employee2==>Employee(name=mc, dept=test, age=10)