我们有一个代表Animal的类,它将属性值转移到WildAmal。这两种类型非常相似,只是属性的名称不同,类型属性相同。
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);
}
这不起作用。你能告诉我做这件事的正确方法吗。提前谢谢你。
您必须为与您的"父"-映射操作。
示例:
@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);
}