mapstruct中qualifiedby用法
时间: 2023-07-08 11:32:55 浏览: 115
在 MapStruct 中,@QualifiedBy 注解可以用来标注一个或多个自定义的 Qualifier,用于指定在多个类之间进行映射时使用哪个映射方法。
@Qualifier 是一个自定义的注解,用于指定映射方法。通过 @QualifiedBy 注解,MapStruct 可以判断哪个方法是最适合的。
下面是一个简单的示例:
```
@Mapper
public interface CarMapper {
@Mapping(source = "make", target = "manufacturer")
@Mapping(source = "numberOfSeats", target = "seatCount")
@Mapping(source = "type", target = "type")
CarDto carToCarDto(Car car);
@Mapping(source = "manufacturer", target = "make")
@Mapping(source = "seatCount", target = "numberOfSeats")
@Mapping(source = "type", target = "type")
@QualifiedBy(SecondMapper.class)
CarDto carDtoToCar(CarDto carDto);
}
@Mapper
public interface SecondMapper {
@Mapping(source = "manufacturer", target = "make")
@Mapping(source = "seatCount", target = "numberOfSeats")
@Mapping(source = "type", target = "type")
CarDto carDtoToCar(CarDto carDto);
}
```
在上面的示例中,我们定义了两个 Mapper 接口,CarMapper 和 SecondMapper。在 CarMapper 中,我们定义了一个 carDtoToCar 的方法,并使用 @QualifiedBy(SecondMapper.class) 注解指定了 SecondMapper 中的 carDtoToCar 方法。
这样,当我们在映射 CarDto 对象到 Car 对象时,MapStruct 将会使用 SecondMapper 中的 carDtoToCar 方法进行映射。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)