zoukankan      html  css  js  c++  java
  • 复制属性的工具类

    在开发中进行遇到值对象和实体间的数据复制.

    其中最常用的为Apache BeanUtilsBean的copyProperties.

    本次测试发现该工具可兼容大部分数据格式,但在Java.sql.Date和java.util.Date复制时,原数据如为空将发送报错.

    通过注册BeanUtilsBean的转换器,简单封装copyProperties方法.

    完成效果: 调用封装后的工具类方法复制对象属性时, 遇到Date数据一律给予新对象对应属性赋值为null.

    最终使用:

    Attach attach = new Attach();
    attach.setCreatePerson("123");
    //attach.setCreateTime(new Date());
    EbankApply ebankApply = new EbankApply();
    EbankApply ebankApply2 = new EbankApply();
    EbankApply ebankApply4 = new EbankApply();
    try {
    org.apache.commons.beanutils.BeanUtils.copyProperties(ebankApply, attach);//Date 数据为空时报错
    org.apache.commons.beanutils.PropertyUtils.copyProperties(ebankApply2, attach);//正常
    org.springframework.beans.BeanUtils.copyProperties(attach, ebankApply4);//注意参数顺序和Apache的相反
    } catch (IllegalAccessException e) {
    e.printStackTrace();
    } catch (InvocationTargetException e) {
    e.printStackTrace();
    } catch (NoSuchMethodException e) {
    e.printStackTrace();
    }
    System.out.println(attach);
    System.out.println(ebankApply);
    System.out.println(ebankApply2);
    System.out.println(ebankApply4);
    所以不使用BeanUtil,使用PropertyUtils

  • 相关阅读:
    unittest生成html测试报告
    excel类封装
    023-linux(2)
    016-WebDriver API(2)
    015-WebDriver API
    014-unittest扩展
    013- unittest单元测试框架
    011-python列表,元组,字典的用法
    010-利用Selenium+python自动输入博客账号密码登录
    009-python一些问题整理
  • 原文地址:https://www.cnblogs.com/theau/p/14198858.html
Copyright © 2011-2022 走看看