需求
三个对象,一个对象Point,一个对象CustomData。第三个对象IotDataCache用来屏蔽二者差异
public class Point extends Model<Point> {
@TableId
private Integer pointId;
private String iotCode;
}
主键ID为整型
public class CustomData extends Model<CustomData> {
@Schema(description = "主键")
@TableId(type = IdType.ASSIGN_UUID)
private String id;
private String iotCode;
}
主键id为字符串
把Point的pointId 和 CustomData的id 都拷贝到IotDataCache中
iotCode字段不变
案例
public static void main(String[] args) {
List<Point> list1 = new ArrayList<>(1);
Point point = new Point();
point.setPointId(1);
point.setIotCode("root.test");
point.setName("123");
list1.add(point);
Point point2 = new Point();
point2.setPointId(2);
point2.setIotCode("root.test2");
point2.setName("123");
list1.add(point2);
Map<String,String> fieldMapping = new HashMap<>(1);
fieldMapping.put("pointId","id");
CopyOptions copyOptions = CopyOptions.create()
.setIgnoreNullValue(false)
.setFieldMapping(fieldMapping);
List<IotDataCache> temp = BeanUtil.copyToList(list1, IotDataCache.class, copyOptions);
System.out.println(temp);
List<CustomData> list2 = new ArrayList<>(2);
CustomData bean1 = new CustomData();
bean1.setId("customdata_h34fdjfsdouf9");
bean1.setIotCode("root.custom");
list2.add(bean1);
CustomData bean2 = new CustomData();
bean2.setId("customdata_h34fdjfsdouf8");
bean2.setIotCode("root.custom2");
list2.add(bean2);
List<IotDataCache> temp2 = BeanUtil.copyToList(list2, IotDataCache.class, null);
System.out.println(temp2);
}
Test
完美实现
拓展
问了AI 很多东西牛头不对马尾的,方法名对不上,参数有问题啊什么