Mapstruct将封闭对象传递给映射方法

时间:2017-01-27 17:02:26

标签: mapstruct

有没有办法将封闭的对象引用作为参数传递给映射方法?

public class Car {
   public int id;
   public Person driver;
}
public class Person {
   public int carId;
}

personToPersonDtoAfterMapping自定义方法中映射人物对象时,我需要对封闭的汽车对象的引用。

@Mapper
public abstract class CarMapper {

    public abstract CarDto carToCarDto(Car car);
    public abstract PersonDto personToPersonDto(Person person);
    @AfterMapping
    protected void personToPersonDtoAfterMapping(Person person, @MappingTarget PersonDto dto,  Car enclosedCar) {
        dto.setCarId(enclosedCar.getId())
    }
}

1 个答案:

答案 0 :(得分:1)

只需使用@Context注释即可。你试过吗?

@Mapper
public abstract class CarMapper {

    public abstract CarDto carToCarDto(Car car);
    public abstract PersonDto personToPersonDto(Person person, @Context Car enclosedCar);
    @AfterMapping
    protected void personToPersonDtoAfterMapping(Person person, @MappingTarget PersonDto dto, @Context Car enclosedCar) {
        dto.setCarId(enclosedCar.getId())
    }
}