1

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

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())
    }
}
4

1 回答 1

0

只需使用 @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())
    }
}
于 2017-03-05T00:36:41.760 回答