还没有弄清楚如何传递其他参数或替代方法。

目前,我正在映射Order和OrderLines。这两个对象是不同的,需要@mappings。示例我有一个Order和OrderRequest对象,两者都不同,并且需要@Mappings批注来映射值,与OrderLines相同。

我创建了以下映射器

  • OrderMapper(用途= OrderLineMappers.class)
  • OrderLineMappers(用法= OrderLineMapper.class)
  • OrderLineMapper

  • 所以我的问题是OrderLine需要Order对象中的OrderId。但是,在OrderLineMapper中,它传递的是OrderLine,而不是Order。如何将OrderId发送到OrderLineMapper?目前,我让OrderMapper做一个@AfterMapper,遍历orderLines并填充OrderId。

    任何帮助都会很棒。

    类OrderMapper
    @Mapper(componentModel = "spring",  uses = {OrderLineMappers.class})
    public abstract class OrderMapper {
    
    @AfterMapping
    protected void orderRequestFromOrder( Order order, @MappingTarget
    OrderRequest orderRequest ) {
    
        //Wanting to do this at the OrderLineMapper class and not here
        String orderId = order.getId();
        List<OrderLineRequest> lines = orderRequest.getOrderLines();
        List<OrderLineRequest> updatedLines = new ArrayList<>();
        for (OrderLineRequest line : lines) {
            line.setOrderId(orderId);
            updatedLines.add(line);
        }
        orderRequest.setOrderLines(updatedLines);
    }
    
    @Mappings({
        @Mapping( source = "orderId", target = "id" ),
        @Mapping( source = "orderNumber", target = "order_number" ),
        @Mapping( source = "orderLines", target = "orderLines")
    })
    public abstract Order orderRequestToOrder(OrderRequest orderRequest);
    

    OrderLineMappers类
    @Mapper(componentModel = "spring",  uses = {OrderLineMapper.class})
    public interface OrderLineMappers {
    
        List<OrderLine> orderLines(List<OrderLineRequest> orderLineRequest);
    
        @InheritInverseConfiguration
        List<OrderLineRequest> orderLineRequests(List<OrderLine> orderLine);
    
    }
    

    OrderLineMapper类
    @Mapper(componentModel = "spring")
    public abstract class OrderLineMapper {
        @Mappings({
            @Mapping( target = "orderId", source = "orderLineId" ),
            @Mapping( target = "order_line_number", source = "orderLineNumber")
    
        })
        public abstract OrderLine orderLineRequestToOrderLine(OrderLineRequest orderLineRequest);
    }
    

    再次尝试将OrderId传递给OrderLineMapper。不确定如何执行此操作。

    最佳答案

    你真的不能做到这一点。相反,您可以使用@Context并在其中执行逻辑。您可以在上下文中包含@AfterMapping@BeforeMapping,在其中可以存储Order id并在其执行期间使用它

    08-18 15:02