简体   繁体   English

如何将 mapstruct 与具有两个参数的源一起使用?

[英]How to use mapstruct with a source that has two parameters?

It seems the mapstruct can work with source with only one parameter.似乎 mapstruct 可以与只有一个参数的源一起使用。 If do set "source = {"id","name"} " when I will have error.如果设置 "source = {"id","name"} " 时我会出错。 How solve it?怎么解决?

 @Mapping(source = {"id","name"}, target = "person", qualifiedByName = "toPerson")
 public MainData toEntity(InfoDTO dto);
 
    @Named("toPerson")
    public Person toPerson(Long id, String name) {
        //some to do
    }

My entities:我的实体:

MyData{
Person person;
}

InfoDTO{
Long id;
String name;
}

Could be done in this way:可以通过这种方式完成:

@Mapper(componentModel = "spring")
public interface PersonMapper {

    @Mapping(expression = "java(toPerson(dto))", target = "person")
    MyData toEntity(InfoDto dto);

    default Person toPerson(InfoDto dto) {
        return new Person(dto.getId(),dto.getName());
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM