有没有办法将封闭的对象引用作为参数传递给映射方法?
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())
}
}
答案 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())
}
}