java - Mapstruct:将对象内部的列表映射到对象列表
问题描述
鉴于:
public class Car {
private String plate;
private List<String> tires;
}
public class TirePlate {
private String plate;
private String tire;
}
我想将所有 Car.tires 映射到单独的 TirePlates 中。我知道我可以制作一个映射器List<String>
,List<tires>
但如果我这样做了,我会失去盘子。
怎么把盘子放进去?
解决方案
您可以做的是为将获取的列表创建一个自定义映射器,plate
然后您将有一个自定义方法映射plate
到tire
一个TirePlate
.
例如:
@Mapper
public interface TireMapper {
CarDto map(Car car);
default List<TirePlate> map(List<String> tires, String plate) {
List<TirePlate> tirePlates = new ArrayList<>(tires.size());
for(String tire: tires) {
tirePlates.add(map(tire, plate));
}
return tirePlates;
}
TirePlate map(String tire, String plate);
}
推荐阅读
- excel - 将列表框值传输到特定行和列到新工作簿
- python - django用芹菜任务发送电子邮件
- java - 多租户架构中的 JaVers
- python - 在 matplotlib 图上添加 y 轴的值,使用 plt.annotate 命令错位文本
- c++ - 如何在 C++ 中从两个单独的 for 循环中添加两个数组
- forms - Microsoft Access Form 组合框和计算
- javascript - 在线类型错误:无法读取未定义的属性“推送”
- javascript - 通过 JavaScript 动态注入谷歌分析代码
- json - 通过 JSON 在 Draw2D.js 中保存和恢复图表
- android - Flutter:build/app/outputs下的apk/release目录和flutter-apk目录有什么区别?