避免繁复的get/set操作

发表于:2007-07-01来源:作者:点击数: 标签:
避免繁复的get/set操作 摘自:夏昕 Hibernate_DEV_GUIDE 为避免繁复的get/set操作,你可以通过Apache Jakarta Commons Beanutils 组件提供的属性批量复制功能(http://jakarta.apache.org/commons/beanutils/).下面的例子中,我们把user对象的所有属性复制

                      避免繁复的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 (IllegalAclearcase/" target="_blank" >ccessException 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();


原文转自:http://www.ltesting.net