将自定义方法映射器映射到Mapstruct

时间:2018-01-30 12:49:00

标签: java mapstruct

我在我的futere项目中创建了一个使用Mapstruct的poc。

现在我有一个问题如何将自定义方法映射到特殊目标。

例如,我有以下界面映射器:

@Mapper
public interface ItemMapper {

    static ItemMapper INSTANCE = Mappers.getMapper(ItemMapper.class);

    @Mappings({ @Mapping(source = "number", target = "itemnumber"),
            @Mapping(source = "description", target = "description"),
            @Mapping(source = "itemClass.name", target = "ic"), @Mapping(source = "optionPart", target = "option"),
            @Mapping(source = "plannerCode.code", target = "plannercode"),
            @Mapping(source = "plannerCode.name", target = "planner"),
            @Mapping(source = "vendor.buyerCode.name", target = "buyer"),
            @Mapping(source = "vendor.buyerCode.code", target = "buyerCode"),
            @Mapping(source = "vendor.number", target = "vendor"),
            @Mapping(source = "vendor.name", target = "vendorName"), @Mapping(source = "pcsItem", target = "pcs"),
            @Mapping(source = "specialColourVariant", target = "specialColors"),
            @Mapping(source = "qtyBufferGreen", target = "greenLine"),
            @Mapping(source = "qtyBufferRed", target = "redine"), @Mapping(source = "leadtime", target = "leadTime"),
            @Mapping(source = "qtyStock", target = "stockAC"),
            @Mapping(source = "qtyStockSupplier", target = "stockSupplier"),
            @Mapping(source = "qtyPurchaseOrder", target = "qtyPo"),
            @Mapping(source = "qtyShopOrder", target = "qtySo"),
            @Mapping(source = "qtyFirmPlannedOrder", target = "qtyFpo"),
            @Mapping(source = "qtyForecast", target = "qtyForecast"),
            @Mapping(source = "standardCost", target = "stdCost"),
            @Mapping(source = "itemCategory.name", target = "category") })
    ItemViewModel itemToDto(Item item);

    default String locationToLocationDto(Item item) {
        return item.getItemsOnDetailedLocations().iterator().next().getLocation().getLocation();
    }

    default double locationToBinType(Item item) {
        return item.getItemsOnDetailedLocations().iterator().next().getBinType();
    }

    default double itemToLotsize(Item item) {
        double lotSize = 0;
        if (item.getLotsize() != null) {
            lotSize = item.getLotsize();
        } else if (item.getItemsOnDetailedLocations() != null && !item.getItemsOnDetailedLocations().isEmpty()) {
            ItemsOnDetailedLocation location = item.getItemsOnDetailedLocations().iterator().next();
            lotSize = location.getLotSize();
        } else {
            lotSize = 0.0;
        }
        return lotSize;
    }

    default double stockRails(Item item) {
        double value = 0;
        for (ItemsOnDetailedLocation detailedLocation : item.getItemsOnDetailedLocations()) {

            if (detailedLocation.getId().getSource().equals("RAILS")) {

                long lotSize2 = detailedLocation.getLotSize();
                long binInStock = detailedLocation.getBinInStock();

                if (binInStock != 0) {

                    value += lotSize2 * (binInStock - 0.5);
                }
            }

        }

        return value;
    }

}

在代码中,您可以看到映射和一些默认方法,其中包含其他映射。如何在Mapstruct映射中使用这些方法,以便mapstruct使用这些方法填充字段中的值?

4 个答案:

答案 0 :(得分:12)

由于您有多个返回相同类型的默认方法。您需要使用Mapping method selection based on qualifiers

这意味着您需要以下列格式编写映射器:

@Mapper
public interface ItemMapper {

    // Omitting other mappings for clarity
    @Mapping(source = "item", target = "locationDto", qualifiedByName = "locationDto")
    @Mapping(source = "item", target = "binType", qualifiedByName = "binType")
    @Mapping(source = "item", target = "lotSize", qualifiedByName = "lotSize")
    @Mapping(source = "item", target = "stockRails", qualifiedByName = "stockRails")
    ItemViewModel itemToDto(Item item);

    @Named("locationDto")
    default String locationToLocationDto(Item item) {
        //Omitting implementation
    }

    @Named("binType")
    default double locationToBinType(Item item) {
        //Omitting implementation
    }

    @Named("lotSize")
    default double itemToLotsize(Item item) {
        //Omitting implementation
    }

    @Named("stockRails")
    default double stockRails(Item item) {
        //Omitting implementation
    }
}

一些重要的注释:

  • 您需要使用MapStruct包中的@Named
  • source中,您还可以指定方法参数的名称
  • qualifiedByName中,您需要指定您在@Named
  • 中编写的值

我强烈建议不要使用表达式来处理这些复杂的事情。要使其正确起来要困难得多,而且维护更加困难

答案 1 :(得分:2)

Mapstruct可以使用类似的结构:

@Mapping(target = "name", expression = "java(user.getName() != null " +
        " ? user.getName() : "DefaultName")")

表达式可以包含java上的任何构造 e.g

 item.getItemsOnDetailedLocations()
.iterator().next().getLocation().getLocation();

如果方法很大,那么将它带到另一个服务并以这种方式调用是值得的

答案 2 :(得分:1)

最简单的方法是使用功能强大的mapstruct @AfterMapping annotation。例如

@AfterMapping
public void treatAdditional(User user, @MappingTarget StudentSkillsTo studentSkillsTo) {
    System.out.println("After mapping!");
}

答案 3 :(得分:0)

您可以像下面这样使用它们

@Mapping( target="/*Enter targetFieldName*/", expression="java( /default method which calculates target field/" )