lfjigu 发表于 2016-12-31 06:16:26

Apache BeanUtils 妙用

  1) 从 FormBean 复制值到 JavaBean 或者互相复制.
            TdepartmentForm deptForm = (TdepartmentForm) form;
            Tdepartment tdepartment = new Tdepartment();
            BeanUtils.copyProperties(tdepartment, deptForm);
2) 复制实体(实体一般是动态的代理类)为 ValueObject 防止原始实体的值被更新掉
            Tproviderbill billVO = new Tproviderbill();
            BeanUtils.copyProperties(billVO, dao.getBill(1));// 复制属性, 防止原实体被修改
            if(billVO.xxx == xxx) {
                billVO.setName(“aaaa”);
            }
        // 保存查询结果
        request.setAttribute("bill", billVO);
  不过, 又有人指出 CGLIB 复制 Bean 速度更快, 效率更高(目前尚未做相关测试)!
  static BeanCopier copy = BeanCopier.create(Bean.class, Bean2.class, false);
  void beanCopies(Object source , Object target){
    copy.copy(source, target, null);
}

http://www.blogjava.net/beansoft/aggbug/268206.html

BeanSoft 2009-04-29 19:19 发表评论
页: [1]
查看完整版本: Apache BeanUtils 妙用