mapstruct中List<DomainObject>转List<DTO>时如何定义source和target?

How to define source and target when converting List<DomainObject> to List<DTO> in mapstruct?

I am Trying to converting List<car> to List<CarDto> but not able to map source and target attribute in mapstruct. Source and target mapping working fine when I convert Car to CarDto but its not working with list<car> object

@Mapper
public interface CarMapper {

     @Mappings({
            @Mapping(source = "carDetailDetail.model", target = "model")
        })
    List<CarDto> carsToCarDtos(List<Car> cars);


}

MapStruct 不支持您查找的内容,已经有针对它的开放功能请求,请查看 #1149

但是,为了使该工作正常进行,您需要在 CarCarDto 之间的方法上定义映射,然后 MapStruct 将使用该方法执行映射。

您的映射器将如下所示:

@Mapper
public interface CarMapper {

    @Mapping(source = "carDetailDetail.model", target = "model")
    CarDto carToCarDto(Car car);

    List<CarDto> carsToCarDtos(List<Car> cars);       
}

如果您不想公开 public carToCarDto 方法,您可以使用抽象 class 并使方法 protectedpackage protected.