美文网首页
BeanUtils.copyProperties() 用法

BeanUtils.copyProperties() 用法

作者: William_Wei007 | 来源:发表于2017-02-07 16:19 被阅读366次

    虽然目前已经有很多文章介绍此方法的用法,但是既然自己遇到了。 还是记录一下吧。


    /**

    * Copythepropertyvaluesofthegivensource beanintothetarget bean.

    *

    Note: The sourceandtarget classes donothavetomatchoreven be derived

    *fromeach other,aslongastheproperties match. Any bean propertiesthatthe* source bean exposesbutthetarget beandoesnotwill silently be ignored.

    *

    Thisisjust a convenience method. For more complex transfer needs,

    * consider using a full BeanWrapper.

    * @param sourcethesource bean

    * @param targetthetarget bean

    * @throws BeansExceptionifthecopying failed

    * @see BeanWrapper

    */

    public static void copyProperties(Object source, Object target) throws BeansException {

    copyProperties(source, target, null, (String[]) null);

    }

    介绍:

    BeanUtils.copyProperties(bean, detailBean); //bean 给detailBean 赋值

    此方法是将前面的bean 给detailBean赋值。

    详细介绍:

    BorrowInfoBean bean=new BorrowInfoBean();

    bean=this.getBorrowInfo(999); //此句意思就是查询bean的信息;

    BorrowInfoBean detailBean=new BorrowInfoBean (); //新的bean

    BeanUtils.copyProperties(bean, detailBean); //bean 给detailBean 赋值

    (未验证,从别的博客看到)要注意一点,Java.util.Date是不被支持的,而它的子类java.sql.Date是被支持的。因此如果对象包含时间类型的属性,且希望被转换的时候,一定要使用java.sql.Date类型。否则在转换时会提示argument mistype异常。

    相关文章

      网友评论

          本文标题:BeanUtils.copyProperties() 用法

          本文链接:https://www.haomeiwen.com/subject/rnhmittx.html