避免繁复的get/set操作

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

避免繁复的get/set操作

摘自:夏昕 Hibernate_DEV_GUIDE

为避免繁复的get/set操作,你可以通过Apache Jakarta Commons Beanutils 组件提供的属性批量复制功能(http://jakarta.apache.org/commons/beanutils/).

下面的例子中,我们把user对象的所有属性复制到anotherUser对象中: TUser user = new TUser();

TUser anotherUser = new TUser();

user.setName("Emma");

user.setUserType(1);

try {

BeanUtils.copyProperties(anotherUser,user);

System.out.println("UserName => "

+anotherUser.getName()

);

System.out.println("UserType => "

+ anotherUser.getUserType()

);

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

TUser user = new TUser();

TUser anotherUser = new TUser();

user.setName("Emma");

user.setUserType(1);

try {

BeanUtils.copyProperties(anotherUser,user);

System.out.println("UserName => "

+anotherUser.getName()

);

System.out.println("UserType => "

+ anotherUser.getUserType()

);

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

TUser user = new TUser();

TUser anotherUser = new TUser();

user.setName("Emma");

user.setUserType(1);

try {

BeanUtils.copyProperties(anotherUser,user);

System.out.println("UserName => "

+anotherUser.getName()

);

System.out.println("UserType => "

+ anotherUser.getUserType()

);

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航