MapStruct映射后实现params convert

Implement params convert after MapStruct mapping

我想使用 MapStruct 框架并扩展我映射的 Java Class。目前我使用这个:

// @Mapper(config = BaseMapperConfig.class)
public interface MerchantsMapper {

    MerchantNewDTO toNewDTO(Merchants merchant);
}

自定义实现:

public MerchantNewDTO toNewDTO(Merchants merchant)
  {
    MerchantNewDTO merchantNewDTO = new MerchantNewDTO();

    merchantNewDTO.setId(Integer.valueOf(merchant.getId()));
    ......

    MerchantConfigurationUtils merchant_config = new MerchantConfigurationUtils();
    Map<MerchantConfigurationFeatureBitString, Boolean> features = merchant_config.initFromDatabaseValue(merchant.getSupported_features());

    merchantNewDTO.setSupports_api(features.get(MerchantConfigurationFeatureBitString.Supports_api));

    return merchantNewDTO;
  }

如您所见,我想要获取 getSupported_features 并填充 Supports_api 值。

但是添加新值是一个非常痛苦的过程。有什么方法可以创建扩展映射接口和 set/get 值的适配器吗?

你能推荐一些解决方案吗?

您可以使用 @AfterMapping@BeforeMapping

@Mapper
public interface MerchantsMapper {

    @Mapping(target = "supports_api", ignore = "true")
    MerchantNewDTO toNewDTO(Merchant merchant);

    @AfterMapping
    default applyFeatures(@MappingTarget MerchatNewDTO merchantNewDTO, Merchant merchant) {

        MerchantConfigurationUtils merchant_config = new MerchantConfigurationUtils();
        Map<MerchantConfigurationFeatureBitString, Boolean> features = merchant_config.initFromDatabaseValue(merchant.getSupported_features());

        merchatNewDTO.setSupports_api(features.get(MerchantConfigurationFeatureBitString.Supports_api));
    }
}