ZG_ForJava 2020-03-12 23:02 采纳率: 0%
浏览 3956

spring中BeanUtils.copyProperties()无法拷贝父类属性

想问一下,子类继承父类之后,如果使用BeanUtils.copyProperties(),能将父类的属性值拷贝到子类中吗?如果没问题的话,我这里怎么没有拷贝成功

下面是我的代码

public class Items {
    private Integer id;

    private String name;

    private Float price;

    private String pic;

    private Date createtime;

    private String detail;

        下面是get()和set()方法

}

public class ItemsCustom extends Items {

    @Override
    public String toString() {
        return "ItemsCustom [
            getId()=" + getId() 
                    + ", getName()=" + getName()
                    + ", getPrice()=" + getPrice() 
                    +   ", getPic()=" + getPic()
                    + ", getCreatetime()=" + getCreatetime() 
                    + ", getDetail()="
                    + getDetail() 
                + "]";
}

这是测试方法内容:
Items items = itemsMapper.selectByPrimaryKey(id);
        //中间对商品信息进行业务处理
        //.....
        //返回ItemsCustom
        ItemsCustom itemsCustom = new ItemsCustom();
        //将items的属性值拷贝到itemsCustom
        BeanUtils.copyProperties(items,itemsCustom);
        System.out.println(itemsCustom);

运行结果是:ItemsCustom [getId()=null, getName()=null, getPrice()=null, getPic()=null, getCreatetime()=null, getDetail()=null]
子类并没有拷贝父类的属性值。不知道怎么回事,请大家帮忙

  • 写回答

3条回答 默认 最新

  • MORE_FOCUS 2020-05-28 17:48
    关注

    父类属性用protected

    评论

报告相同问题?