映射多个源的一个映射器方法



我有多个源,这些源是有条件使用的,例如:

@Mapper(uses = CustomMapper.class)
public interface CarFilterMapper {
default CarFilterDTO toCarFilterDTO(Object obj) {
if (obj == null) {
return null;
}
if (obj instanceof com.model.cars.v1.car) {
com.model.cars.v1.CAR targetCar = (com.model.cars.v1.CAR) car;
return toCarFilterDTO(targetCar);
} else if (obj instanceof com.model.cars.v2.CAR) {
com.model.cars.v2.CAR targetCar = (com.model.cars.v2.CAR) car;
return toCarFilterDTO(targetCar);
}

@CarMapping
CarFilterDTO toCarFilterDTO(com.model.cars.v1.CAR.SAL obj);

@CarMapping
CarFilterDTO toCarFilterDTO(com.model.cars.v2.CAR.SAL obj);

我还有CarMapping:

@Retention(RetentionPolicy.CLASS)
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Mapping(target = "number", source = "SAL.INF.HDR.NUM")
@Mapping(target = "tracking", source = "SAL.INF.HDR.TRCNUM")
@Mapping(target = "hbl", source = ".", qualifiedByName = "toHbl")
public @interface CarMapping {
}

我的问题是,我需要为所有CAR版本(有5个版本(编写多个toHbl方法。我想承认这一点。有可能吗?

我不知道有一个内置的解决方案可以解决这个问题,但是,据我所知,这应该由可组合的映射器来解决,我认为它们应该在层中工作。但是,仍然应该为每个源手动实现映射。

@Retention(RetentionPolicy.CLASS)
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Mapping(target = "number", source = "SAL.INF.HDR.NUM")
@Mapping(target = "tracking", source = "SAL.INF.HDR.TRCNUM")
public @interface CarMapping {
@Retention(RetentionPolicy.CLASS)
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@CarMapping
@Mapping(target = "hbl", source = ".", qualifiedByName = "toHbl1")
@interface CarMapperToHbl1 {}
@Retention(RetentionPolicy.CLASS)
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@CarMapping
@Mapping(target = "hbl", source = ".", qualifiedByName = "toHbl2")
@interface CarMapperToHbl2 {}
// ... others
}

这种方法的应用如下:

@Mapper
public interface CarMapper {
@CarMapping.CarMapperToHbl1
CarDto entityToDto(CarEntity entity)
// ... others
}

相关内容

  • 没有找到相关文章

最新更新