将多个字段映射到一个字段在Mapstruct中怎么做?

huangapple go评论80阅读模式
英文:

Mapping multiple fields to one in Mapstruct?

问题

我需要将一个开放银行类映射到我的核心类我正在使用 `mapstruct`。我需要将多个枚举字段映射到我核心类中的一个字段但我认为 mapstruct 不支持映射多个字段是否有巧妙的方法或解决方案呢
提前感谢
以下是开放银行的枚举值

```java  
public enum Category {

  TRANS_AND_SAVINGS_ACCOUNTS("TRANS_AND_SAVINGS_ACCOUNTS"),  
  REGULATED_TRUST_ACCOUNTS("REGULATED_TRUST_ACCOUNTS"),  
  CRED_AND_CHRG_CARDS("CRED_AND_CHRG_CARDS"),  
  PERS_LOANS("PERS_LOANS"),  
  MARGIN_LOANS("MARGIN_LOANS"),  
  PROPERTY_LOANS("PROPERTY_LOANS"),  
  TRADE_FINANCE("TRADE_FINANCE"),  
  OVERDRAFTS("OVERDRAFTS"),  
  BUSINESS_LOANS("BUSINESS_LOANS"),
  RESIDENTIAL_MORTGAGES("RESIDENTIAL_MORTGAGES");

...

}

以下是我的核心类:

    public enum Type{

        CHARGES("Charges"),
        CREDITS("Credits"),
        ALL_LOANS("AllLoans"),
        MORTGAGE("Mortgage"),
        OTHER("Other"),
        PREPAIDCARD("PrePaidCard"),
        SAVINGS("Savings");

...

}

我有一个如下的 valueMapping(是错误的):

public interface TypeMapper {
    @ValueMappings( {
            @ValueMapping(source = "CHARGES", target= "CRED_AND_CHRG_CARDS"),
            @ValueMapping(source = "CREDITS", target= "CRED_AND_CHRG_CARDS"),
            @ValueMapping(source = "ALL_LOANS", target = "PERS_LOANS"),
            @ValueMapping(source = "ALL_LOANS", target = "MARGIN_LOANS"),
            @ValueMapping(source = "ALL_LOANS", target = "BUSINESS_LOANS"),
            @ValueMapping(source = "ALL_LOANS", target = "PROPERTY_LOANS"),
            @ValueMapping(source = "MORTGAGE", target= "RESIDENTIAL_MORTGAGES"),
            @ValueMapping(source = "OTHER", target= "OVERDRAFTS"),
            @ValueMapping(source = "PREPAIDCARD", target = MappingConstants.NULL),
            @ValueMapping(source = "SAVINGS", target = "TRANS_AND_SAVINGS_ACCOUNTS")
    } )

...

}

它不接受将 PERS_LOANS("PERS_LOANS"), MARGIN_LOANS("MARGIN_LOANS"), PROPERTY_LOANS("PROPERTY_LOANS") 映射到 ALL_LOANS。我想知道是否有一种方法可以将这三个值映射到一个字段中。也许可以在 @AfterMapping 中完成,有什么建议吗?我想将所有未映射的值映射到我的核心类中的 OTHER


<details>
<summary>英文:</summary>

I need to map from an open banking class to my core class. I am working with `mapstruct`. I need to map multiple enum fields to one on my core class. But i think mapstruct doesn&#39;t support mapping multiple fields. Any tricky way or a solution for that?
Thanks in advance!
Here is my enum values from open banking:

```java  
public enum Category {

  TRANS_AND_SAVINGS_ACCOUNTS(&quot;TRANS_AND_SAVINGS_ACCOUNTS&quot;),  
  REGULATED_TRUST_ACCOUNTS(&quot;REGULATED_TRUST_ACCOUNTS&quot;),  
  CRED_AND_CHRG_CARDS(&quot;CRED_AND_CHRG_CARDS&quot;),  
  PERS_LOANS(&quot;PERS_LOANS&quot;),  
  MARGIN_LOANS(&quot;MARGIN_LOANS&quot;),  
  PROPERTY_LOANS(&quot;PROPERTY_LOANS&quot;),  
  TRADE_FINANCE(&quot;TRADE_FINANCE&quot;),  
  OVERDRAFTS(&quot;OVERDRAFTS&quot;),  
  BUSINESS_LOANS(&quot;BUSINESS_LOANS&quot;),
  RESIDENTIAL_MORTGAGES(&quot;RESIDENTIAL_MORTGAGES&quot;);

...

}

And here is my core class:

    public enum Type{

        CHARGES( &quot;Charges&quot; ),
        CREDITS( &quot;Credits&quot; ),
        ALL_LOANS( &quot;AllLoans&quot; ),
        MORTGAGE( &quot;Mortgage&quot; ),
        OTHER( &quot;Other&quot; ),
        PREPAIDCARD( &quot;PrePaidCard&quot; ),
        SAVINGS( &quot;Savings&quot; );

...

}

And i have a valueMapping as following(which is wrong):

public interface TypeMapper {
    @ValueMappings( {
            @ValueMapping(source = &quot;CHARGES&quot;, target= &quot;CRED_AND_CHRG_CARDS&quot;),
            @ValueMapping(source = &quot;CREDITS&quot;, target= &quot;CRED_AND_CHRG_CARDS&quot;),
            @ValueMapping(source = &quot;ALL_LOANS&quot;, target = &quot;PERS_LOANS&quot;),
            @ValueMapping(source = &quot;ALL_LOANS&quot;, target = &quot;MARGIN_LOANS&quot;),
            @ValueMapping(source = &quot;ALL_LOANS&quot;, target = &quot;BUSINESS_LOANS&quot;),
            @ValueMapping(source = &quot;ALL_LOANS&quot;, target = &quot;PROPERTY_LOANS&quot;),
            @ValueMapping(source = &quot;MORTGAGE&quot;, target= &quot;RESIDENTIAL_MORTGAGES&quot;),
            @ValueMapping(source = &quot;OTHER&quot;, target= &quot;OVERDRAFTS&quot;),
            @ValueMapping(source = &quot;PREPAIDCARD&quot;, target = MappingConstants.NULL),
            @ValueMapping(source = &quot;SAVINGS&quot;, target = &quot;TRANS_AND_SAVINGS_ACCOUNTS&quot;)
    } )

...

}

It does not accept to map PERS_LOANS(&quot;PERS_LOANS&quot;), MARGIN_LOANS(&quot;MARGIN_LOANS&quot;), PROPERTY_LOANS(&quot;PROPERTY_LOANS&quot;) to ALL_LOANS. I wonder if there's a way to map all 3 to one field. Maybe can it be covered in @AfterMapping, any suggestions? And i want to map all the unmapped values to OTHER in my core class.

答案1

得分: 0

你必须自己实现它,因为 MapStruct 不支持该功能。尝试使用 @qualifiedByName 注解与您自己的方法

例如:

public interface TypeMapper{
    @Mapping(target= "CRED_AND_CHRG_CARDS", qualifiedByName = "creditAndChrgCardsMapping")
    public Type CategoryToType(Category category);
    
    default String qualifiedByName (String charges, String credits){
        // 实现您的业务逻辑,例如
        return charges + credits;
    } 
}
英文:

You have to implement it by yourself, because MapStruct doesn't support that. Try to use @qualifiedByName annotation with your own method

For example:

public interface TypeMapper{
    @Mapping(target= &quot;CRED_AND_CHRG_CARDS&quot;, qualifiedByName = &quot;creditAndChrgCardsMapping&quot;)
    public Type CategoryToType(Category category);
    
    default String qualifiedByName (String charges, String credits){
    //Implement your business logic for example
    return charges + credits;
    } 
 }

答案2

得分: 0

在您的问题中,您在说您无法将 PERS_LOANSMARGIN_LOANS 等映射到 ALL_LOANS。然而,您的值映射是颠倒的。您正在将 ALL_LOANS 映射到 PERS_LOANS

不可能将一个枚举(源)值映射到多个枚举(目标)值。然而,可以将多个枚举(源)值映射到同一个枚举(目标)值。

英文:

In your question you are saying that you can't map PERS_LOANS, MARGIN_LOANS, etc. to ALL_LOANS. However, your value mappings are reversed. You are mapping ALL_LOANS to PERS_LOANS.

It is not possible to map one enum (source) value to multiple enum (target) values. However, it is possible to map multiple enum (source) values to the same enum (target) value.

huangapple
  • 本文由 发表于 2020年9月1日 05:05:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63678175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定