java - How to write mapper(MapStruct) from a list type to a list
问题描述
We have a class representing Animal transfering attribute value to WildAnimal.Both types are rather similar, only the attributes have different names and the type attribute is same.
public class Animal{
private List<Cat> cats;
}
public class Cat {
private String colour;
private Integer weight;
}
public class WildAnimal{
private List<WildCat> wildCats;
}
public class WildCat {
private String shade;
private Integer mass;
}
@Mapper
public interface AnimalMapper {
@Mapping( source = "cats", target = "wildCats")
WildAnimal animalTowildAnimal(Animal animal);
}
This is not working. Could you please tell me the correct way of doing it. Thank you in adavance.
解决方案
您必须提供与您的“父”映射操作相关的其他类型的映射说明。
例子:
@Mapper
public interface AnimalMapper {
@Mapping(source = "colour", target = "shade")
@Mapping(source = "weight", target = "mass")
WildCat catToWildCat(Cat cat);
@Mapping(source = "cats", target = "wildCats")
WildAnimal animalToWildAnimal(Animal animal);
}
推荐阅读
- c++ - 在数组中查找元素的问题 - c++20
- python - 重新连接观察到的共享文件夹后如何保持看门狗工作?
- sql - 第 1 行第 14 列(已创建)的批量加载数据转换错误(截断)
- javascript - 使用 Audio() 的怪癖
- ruby-on-rails - 预加载连接多个值的记录
- javascript - 请求的资源(AWS、API Gateway、S3、CORS)上不存在“Access-Control-Allow-Origin”标头
- curl - 如何将 curl 安装到 jetbrains CLion
- python - 如何使变量资源管理器中的变量可从新文件访问
- c++ - 为什么将返回值分配给 int 时,它本身会减一?
- javascript - 将输入图像保存在 localStorage 中,然后使用 JS 检索它