gpt4 book ai didi

java - 将附加参数传递给 MapStruct 映射器

转载 作者:行者123 更新时间:2023-11-30 06:09:48 42 4
gpt4 key购买 nike

尚未弄清楚如何传递附加参数或替代方案。

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

我创建了以下映射器

  • OrderMapper(使用 = OrderLineMappers.class)
  • OrderLineMappers(使用 = OrderLineMapper.class)
  • 订单行映射器

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

任何帮助都会很棒。

订单映射器类

@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);

}

订单行映射器类

@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 并在执行期间使用它

例如

public class OrderMappingContext {

protected String currentOrderId;

@BeforeMapping
public void startMappingOrder(@MappingTarget Order order, OrderRequest request) {
this.currentOrderId = request.getId();
}

@AfterMapping
public void finishOrderMapping(@MappingTarget Order order) {
this.currentOrderId = null;
}

@AfterMapping
public void afterOrderLineMapping(@MappingTarget OrderLine orderLine) {
orderLine.setOrderId(this.currentOrderId);
}
}

您的映射器将如下所示:

@Mapper(componentModel = "spring",  uses = {OrderLineMappers.class})
public abstract class OrderMapper {



@Mapping( source = "orderId", target = "id" ),
@Mapping( source = "orderNumber", target = "order_number" ),
@Mapping( source = "orderLines", target = "orderLines")
public abstract Order orderRequestToOrder(OrderRequest orderRequest, @Context OrderMappingContext context);
@Mapper(componentModel = "spring",  uses = {OrderLineMapper.class})
public interface OrderLineMappers {

List<OrderLine> orderLines(List<OrderLineRequest> orderLineRequest, @Context OrderMappingContext context);

@InheritInverseConfiguration
List<OrderLineRequest> orderLineRequests(List<OrderLine> orderLine);

}
@Mapper(componentModel = "spring")
public abstract class OrderLineMapper {

@Mapping( target = "orderId", source = "orderLineId" ),
@Mapping( target = "order_line_number", source = "orderLineNumber")
public abstract OrderLine orderLineRequestToOrderLine(OrderLineRequest orderLineRequest, @Context OrderMappingContext context);
}

在调用方面,您将执行类似的操作

orderMapper.orderRequestToOrder(request, new OrderMappingContext());

关于java - 将附加参数传递给 MapStruct 映射器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50517750/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com