使用mapstruct映射Hierrachical Beans

时间:2018-04-04 17:40:29

标签: java mapstruct object-object-mapping

这是this问题的扩展。

 class Customer{
  // distinct properties
}
class RetailCustomer extends Customer{
  // distinct properties
}

class WholeSaleCustomer extends Customer{
 // distinct properties
}

class CustomerDO {
 // String custType ; // flag used to determine if Customer is wholeSale or Retail
 //few properties same as Customer/WholeSaleCustomer/RetailCustomer
 // few distinct properties
}

@Mapper
public interface CustomerMapper{

           default Customer toCustomer(CustomerDO customerDO) {
                 String custType = customerDO.getCustType();
                if("W".equalsIgnoreCase(custType)){
                   return toWholeSaleCustomer(customerDO);
                  }
                 else {
                  return toRetailCustomer(CustomerDO);
                   }
           }
        @Mappings({
              @Mapping(source="a", target="b"),
               @Mapping(source="c", target="d"),
               @Mapping(source="m", target="m")
            })
         WholeSaleCustomer toWholeSaleCustomer(CustomerDO customerDO);

       @Mappings({
              @Mapping(source="e", target="f"),
               @Mapping(source="g", target="h"),
               @Mapping(source="n", target="n")
            })
         RetailCustomer toRetailCustomer(CustomerDO customerDO);
}

我需要根据CustomerDO中的custType标志从CustomerDO映射到WholeSaleCustomer / RetailCustomer。但上面定义的映射器不起作用。它在编译时给出了以下错误

 CustomerMapper.java:[23,34] Ambiguous mapping methods found for mapping property "com.domain.CustomerDO customerDO" to com.role.Customer: com.role.Customer: toCustomer
r(com.domain.CustomerDO customerDO), com.role.WholeSaleCustomer toWholeSaleCustomer(com.domain.CustomerDO wsCustomer), com.role.RetailCustomer toRetailCustomer(com.domain.CustomerDO wsCustomer)

但是,如果我将toCustomer(CustomerDo customerDO)签名更改为toCustomer(Object customerDO)并删除toWholeSaleCustomer / toRetailCustomer,则可以正常工作。它只会映射两种类型中的任何一种。但我想要两个。我有类似服务Bean的案例。有几个儿童服务。我应该能够在需要时将它们全部映射

1 个答案:

答案 0 :(得分:0)

您要找的是Mapping method selection based on qualifiers

因此,如果您的客户对象如下:

class WholeSaleCustomer extends Customer {
 // distinct properties
}

class CustomerDO {
 // String custType ; // flag used to determine if Customer is wholeSale or Retail
 //few properties same as Customer/WholeSaleCustomer/RetailCustomer
 // few distinct properties
    private CustomerDO customerDO;
}

然后你必须告诉MapStruct它需要使用哪种方法来执行映射。所以你的映射器看起来像:

@Mapper
public interface CustomerMapper {

        @Named("baseCustomer")
        default Customer toCustomer(CustomerDO customerDO) {
            String custType = customerDO.getCustType();
            if("W".equalsIgnoreCase(custType)){
                return toWholeSaleCustomer(customerDO);
            }
            else {
                return toRetailCustomer(CustomerDO);
            }
        }

        @Mappings({
            @Mapping(source="customerDO", qualifiedByName = "baseCustomer"),
            @Mapping(source="c", target="d"),
            @Mapping(source="m", target="m")
        })
        WholeSaleCustomer toWholeSaleCustomer(CustomerDO customerDO);

        @Mappings({
            @Mapping(source="customerDO", qualifiedByName = "baseCustomer"),
            @Mapping(source="g", target="h"),
            @Mapping(source="n", target="n")
        })
        RetailCustomer toRetailCustomer(CustomerDO customerDO);
}

@Named应来自org.mapstruct.Named