工具类BeanUtils是阿帕奇公司提供的,它有一个方法copeProperties(a,b),可以将b对象属性名与a对象属性名相同的值付给a对象,这样让我们少些很多get,set方法,精简了代码;
如何使用呢?
只要向项目中导入:commons-beanutils-1.7.0.jar,commons-logging.jar(依赖包),这样我们就可以使用copeProperties(a,b)方法了;
=============================================================================================
=============================================================================================
@Test
public void fun(){
//首先创建一个Student对象;
Student st = new Student();
st.setName("jim");
st.setSex("男");
StudentDTO dto = new StudentDTO();
//测试copeProperties的作用;
try {
BeanUtils.copyProperties( dto,st);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(dto.getName());
System.out.println(dto.getMobile());
}