映射嵌套元素 - Mapstruct

Fut*_*eek 4 java nested-lists object-object-mapping spring-boot mapstruct

我正在尝试使用 MapStruct 将以下源类映射到目标类。

目标班级:

public class Response {
    private List<Customer> customer = new ArrayList<Customer>();
}

public class Customer {
    private String customerId;
    private List<Product> products = new ArrayList<Product>();
}

public class CustProduct {
    private String CustProductId;
    private String CustPdtName;
    private List<productDetail> CustProductDetails = new ArrayList<productDetail>();
}
Run Code Online (Sandbox Code Playgroud)

源类:

public class UserList {
    protected List<User> user;
}

public class User {
    protected String userId;
    protected List<String> productRefId;  //List of products for that particular user
}

public class ProductList {
    protected List<Product> product;
}
public class Product {
   protected String productId;       //Reference to productRefId
   protected String productName;
   protected List<Details> productDetails;
}
   
Run Code Online (Sandbox Code Playgroud)

映射器接口:

 List<Customer> mapUser(List<User> user);

    @Mappings({
            @Mapping(target = "customerId", source = "userId”),
            @Mapping(target = "products", ignore = true)
    })
    Customer mapUser(User user);

    @Mappings({
        @Mapping(target = "CustProductId", source = "productId"),
        @Mapping(target = "CustPdtName", source = "productName"),
        @Mapping(target = "CustProductDetails", source = "productDetails")
})
CustProduct mapUser(Product product);
Run Code Online (Sandbox Code Playgroud)

我的问题是,我想将CustProductCustomer连接起来, 为此,我尝试了 AfterMapping,例如:

default void findProducts(User user, @MappingTarget Customer customer) {
            List<String> productIds = user.getproductRefId();
            List<CustProduct> custProducts = new ArrayList<>();
            for(int i=0; i<productIds.size();i++){
                    CustProduct custProduct = new CustProduct();
                    custProduct.setCustProductId(productIds.get(i));
                    //Here I want set productName and productDetails to custProduct Object(Iterating through ProductList and get from Product)
                    custProducts.add(custProduct);
                }
            }
            customer.setCustProducts(custProducts);
        }
    
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮忙填写上面的评论部分吗?或者还有其他选项可以映射这些对象吗?

编辑:我尝试了以下解决方案,但接口实现类本身发生了变化。

Ank*_*han 5

您需要使用@Context批注将 ProductList 对象带入上下文。

将映射器方法更改为以下定义并在调用时传递 ProductList 对象mapUser

@Mappings({
            @Mapping(target = "customerId", source = "paxJourneyType.paxJourneyID”),
            @Mapping(target = "products", ignore = true)
    })
    Customer mapUser(User user, @Context ProductList productList);
Run Code Online (Sandbox Code Playgroud)

然后您可以在@AfterMapping方法中使用相同的 ProductList 对象:

default void findProducts(User user, @Context ProductList productList @MappingTarget Customer customer) {
            List<String> productIds = user.getproductRefId();
            List<CustProduct> custProducts = new ArrayList<>();
            for(int i=0; i<productIds.size();i++){
                    CustProduct custProduct = new CustProduct();
                    custProduct.setCustProductId(productIds.get(i));
                    Product product = getProduct(ProductList productList,productIds.get(i));
                    custProduct.setCustPdtName(product.getProductName);
                    custProducts.add(custProduct);
                }
            }
            customer.setCustProducts(custProducts);
        }

private Product getProduct(ProductList productList,String productId){
    //Iterate through ProductList and get from Product
}
Run Code Online (Sandbox Code Playgroud)


Maf*_*for 3

你可以不用@AfterMappingMapStruct 来做到这一点,但你需要一点帮助:

@Mapper
public interface CustMapper {

    @Mapping(target = "customerId", source = "userId")
    @Mapping(target = "products", source = "productRefIds")
    Customer map(User user, @Context Map<String, Product> productsMap);

    List<CustProduct> map(List<String> productRefIds, @Context Map<String, Product> productsMap);

    default CustProduct map(String productId, @Context Map<String, Product> productsMap) {
        return map(productsMap.get(productId));
    }

    @Mapping(target = "custProductId", source = "productId")
    @Mapping(target = "custProductName", source = "productName")
    @Mapping(target = "custProductDetails", source = "productDetails")
    CustProduct map(Product product);

    CustProductDetail map(ProductDetail productDetail);
}
Run Code Online (Sandbox Code Playgroud)

productRefIds或者,您可以手动迭代:

@Mapper
public interface CustMapper {

    @Mapping(target = "customerId", source = "userId")
    @Mapping(target = "products", source = "productRefIds")
    Customer map(User user, @Context Map<String, Product> productsMap);

    default List<CustProduct> map(List<String> productRefIds, @Context Map<String, Product> productsMap) {
        return productRefIds.stream().map(productsMap::get).map(this::map).collect(Collectors.toList());
    }

    @Mapping(target = "custProductId", source = "productId")
    @Mapping(target = "custProductName", source = "productName")
    @Mapping(target = "custProductDetails", source = "productDetails")
    CustProduct map(Product product);

    CustProductDetail map(ProductDetail productDetail);
}
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,您都需要以某种方式处理productIdproductsMap.

不使用的优点@AfterMapping是目标类可以是不可变的。