作者:港乄漫天地 | 来源:互联网 | 2024-11-03 15:17
参考博客:基于Spring BeanUtils 实现 JavaBean克隆及属性拷贝 基于apache BeanUtils 实现 JavaBean克隆及属性拷贝 基于 MapStruct实现 Java
参考博客:
基于Spring BeanUtils 实现 JavaBean克隆及属性拷贝 基于apache BeanUtils 实现 JavaBean克隆及属性拷贝 基于 MapStruct实现 JavaBean克隆及属性拷贝 Intellij 插件 GenerateO2O
实体类
Goods.java
@Getter
@Setter
@ToString
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Goods {
private Long id;
private String name;
private String barcode;
private BigDecimal price1;
private BigDecimal price2;
private Integer amount;
private String img;
private String pics;
private Integer categoryId;
private Integer countryId;
private String addr;
private String sellPoint;
private Integer priority;
private Integer status;
private LocalDateTime createTime;
private LocalDateTime updateTime;
}
GoodsItem.java
@Getter
@Setter
@ToString
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class GoodsItem {
private Long id;
private String name;
private BigDecimal price1;
private BigDecimal price2;
private String img;
private String sellPoint;
}
测试代码
public class BeanUtilsTest {
public static void main(String[] args) {
Goods goods = DataUtil.build(Goods.class);
Goods g = new Goods();
BeanUtils.copyProperties(goods, g);
System.out.println(g);
GoodsItem goodsItem = new GoodsItem();
BeanUtils.copyProperties(goods,goodsItem);
System.out.println(goodsItem);
}
}