Bean拷贝

BeanUtils工具类使用方法:

第一个是参数对象,第二个是目标对象
将source中的字段添加到target中
注意:两个对象中对应字段名和类型应完全相同,否则无法拷贝

BeanUtils.copyProperties(Object source, Object target);

准备实体,DTO,VO

DTO是封装前端传回来的字段,使用Bean拷贝可以实现:将前端传入Dto对象转为实体类对象。
VO是后端将前端查询的字段数据封装成VO返给前端,使用Bean拷贝可以实现:将前端查询实体对象转为VO对象。

实体

package com.ruoyi.project.system.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 订单表(Bill)实体类
 *
 * @author xct
 * @since 2023-06-06 09:25:11
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Bill implements Serializable {
    /**
     * 订单id
     */
    @TableId(value = "id",type = IdType.AUTO)
    private Integer id;
    /**
     * 用户id
     */
    private Integer userId;
    /**
     * 商品id
     */
    private Integer shopId;
    /**
     * 订单状态(1.待付款 2.待发货)
     */
    private Integer status;
}

DTO

package com.ruoyi.project.system.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;


/**
 * @author xct
 * @version 1.0
 * @date 2023-06-08 14:47
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BillDTO  {
    /**
     * 用户id
     */
    private Integer userId;
    /**
     * 商品id
     */
    private Integer shopId;
}

VO

package com.ruoyi.project.system.domain.vo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;


@Data
@AllArgsConstructor
@NoArgsConstructor
public class BillVO  {
    /**
     * 订单id
     */
    @TableId(value = "id",type = IdType.AUTO)
    private Integer id;
    /**
     * 用户id
     */
    private Integer userId;
    /**
     * 商品id
     */
    private Integer shopId;
    /**
     * 订单状态(1.待付款 2.待发货)
     */
    private Integer status;


}

普通Bean对象拷贝

  public R save(BillDTO billDTO){
        //新建Bill对象
        Bill bill = new Bill();
        //实现将billDTO拷贝到bill对象
        BeanUtils.copyProperties(billDTO,bill);

        billMapper.insert(bill);
        return R.ok();
    }

Bean拷贝工具类封装

因为基础的BeanUtils在使用时拷贝非常不方便,还需要我们自己去创建新的User拷贝,对List集合的拷贝还需要我们自己去遍历,这里我们封装工具类来实现这些功能。

BeanCopyUtils工具类

package com.ruoyi.common.utils;


import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;
/**
 * @author xct
 * @version 1.0
 * @date 2023-06-08 15:01
 */
public class BeanCopyUtils {
    private BeanCopyUtils() {
    }

    public static <V> V copyBean(Object source, Class<V> clazz) {
        // 创建目标对象
        V result = null;
        try {
            result = clazz.newInstance();
            // 实现属性拷贝
            BeanUtils.copyProperties(source, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //返回
        return result;
    }

    public static <O, V> List<V> copyBeanList(List<O> list, Class<V> clazz) {
        return list.stream()
                .map(o -> copyBean(o, clazz))
                .collect(Collectors.toList());
    }
}

普通Bean对象拷贝

public R save(BillDTO billDTO){
        //实现将billDTO拷贝到bill对象
        Bill bill = BeanCopyUtils.copyBean(billDTO, Bill.class);

        billMapper.insert(bill);
        return R.ok();
    }

List拷贝

 public R getBill() {
        List<Bill> bills = getBillList();
        List<BillVO> billVOS = BeanCopyUtils.copyBeanList(bills, BillVO.class);
        return R.ok(billVOS);
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BUG忠实爱好者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值