Java 在MapStruct映射后实现参数转换
我想使用MapStruct框架并扩展我映射的Java类。目前我使用的是:Java 在MapStruct映射后实现参数转换,java,mapstruct,Java,Mapstruct,我想使用MapStruct框架并扩展我映射的Java类。目前我使用的是: // @Mapper(config = BaseMapperConfig.class) public interface MerchantsMapper { MerchantNewDTO toNewDTO(Merchants merchant); } 自定义实现: public MerchantNewDTO toNewDTO(Merchants merchant) { MerchantNewDTO
// @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;
}
public MerchantNewTo至NewTo(商户)
{
MerchantNewTo MerchantNewTo=新MerchantNewTo();
merchantNewDTO.setId(Integer.valueOf(merchant.getId());
......
MerchantConfiguration Utils merchant_config=新MerchantConfiguration Utils();
Map features=merchant_config.initFromDatabaseValue(merchant.getSupported_features());
MerchantNewTo.setSupports_api(features.get(MerchantConfiguration功能BitString.Supports_api));
退回商品新地址;
}
如您所见,我希望获得getSupported\u功能
,并填充Supports\u api
值
但增加新的价值是一个非常痛苦的过程。有没有办法创建扩展映射接口并设置/获取值的适配器
您能推荐一些解决方案吗?您可以使用
@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));
}
}
@Mapper
公共界面商品映射器{
@映射(target=“supports_api”,ignore=“true”)
商户newto至newto(商户);
@后映射
默认应用程序功能(@MappingTarget MerchatNewDTO-merchantNewDTO,Merchant-Merchant){
MerchantConfiguration Utils merchant_config=新MerchantConfiguration Utils();
Map features=merchant_config.initFromDatabaseValue(merchant.getSupported_features());
merchatNewDTO.setSupports_api(features.get(MerchantConfigurationFeatureBitString.Supports_api));
}
}
您能给我们看一下课程吗?MerchantNewTo和Merchants?