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);
}