BeanUtils.copyProperties()的用法;

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

导包:import org.springframework.beans.BeanUtils;

源代码的介绍:

/**
      * Copy the property values of the given source bean into the target bean.
      * <p>Note: The source and target classes do not have to match or even be derived
      * from each other, as long as the properties match. Any bean properties that the
      * source bean exposes but the target bean does not will silently be ignored.
      * <p>This is just a convenience method. For more complex transfer needs,
      * consider using a full BeanWrapper.
      * @param source the source bean
      * @param target the target bean
      * @throws BeansException if the copying 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异常。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值