当前位置导航:炫浪网>>网络学院>>网页制作>>JSP教程

避免繁复的get/set操作


                      避免繁复的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();


相关内容
赞助商链接