|
![]() |
名片设计 CorelDRAW Illustrator AuotoCAD Painter 其他软件 Photoshop Fireworks Flash |
|
避免繁复的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(); } 返回类别: 教程 上一教程: Java多线程编程详解 下一教程: Java NIO API详解 您可以阅读与"避免繁复的get/set操作"相关的教程: · 一个stmt多个rs进行操作引起的ResultSet已经关闭错误 · JSP文件操作之读取篇 · 一个JAVABEAN轻松实现对数据库的各种操作 · jdk 1.5学习之RowSet(二) · JAVAMAIL操作的总结 |
![]() ![]() |
快精灵印艺坊 版权所有 |
首页![]() ![]() ![]() ![]() ![]() ![]() ![]() |