MapStruct实现User/Product关联映射至Customer-CustProduct嵌套对象
跨关联结构的MapStruct映射实现
现有代码结构梳理
目标侧类定义
public class Response { private List<Customer> customer = new ArrayList<Customer>(); } public class Customer { private String customerId; private List<CustProduct> products = new ArrayList<CustProduct>(); } public class CustProduct { private String CustProductId; private String CustPdtName; private List<ProductDetail> CustProductDetails = new ArrayList<ProductDetail>(); }
注:原有Customer类中products字段的泛型定义为Product,和后续要赋值的CustProduct类型不匹配,已修正为正确的泛型类型。
源侧类定义
public class UserList { protected List<User> user; } public class User { protected String userId; // 对应用户关联的产品ID列表 protected List<String> productRefId; } public class ProductList { protected List<Product> product; } public class Product { // 与productRefId对应的产品标识 protected String productId; protected String productName; protected List<ProductDetail> productDetails; }
已编写的Mapper基础逻辑
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 mapProduct(Product product);
核心卡点原因
你当前的@AfterMapping补全方法没有办法获取全量ProductList数据源,没法根据用户持有的productRefId匹配到对应的产品实体,导致产品信息补全逻辑断档。如果手动修改MapStruct生成的实现类,后续编译后代码会被覆盖,不符合长期维护要求。
适配的标准MapStruct方案
MapStruct原生支持在映射链路中传递额外参数,自动注入到@AfterMapping方法的入参列表中,全程不需要改动生成的实现类:
- 调整顶层映射入口,将全量
ProductList作为额外入参传入 - 重构
@AfterMapping方法,接收传入的全量产品数据源,做ID匹配后复用已有的mapProduct方法完成映射
// 顶层映射入口:同时传入用户列表、全量产品列表作为映射数据源 List<Customer> mapUserList(List<User> userList, ProductList allProductList); // 单用户映射方法,额外传入全量产品列表作为隐式参数 @Mappings({ @Mapping(target = "customerId", source = "user.userId"), @Mapping(target = "products", ignore = true) }) Customer mapUser(User user, ProductList allProductList); // 原有产品转CustProduct的映射方法保持不变 @Mappings({ @Mapping(target = "CustProductId", source = "productId"), @Mapping(target = "CustPdtName", source = "productName"), @Mapping(target = "CustProductDetails", source = "productDetails") }) CustProduct mapProduct(Product product); // AfterMapping补全方法,自动注入链路传递的所有参数 @AfterMapping default void fillMatchedProducts(User user, ProductList allProductList, @MappingTarget Customer customer) { // 预先将全量产品按productId转为Map,降低匹配的时间复杂度 Map<String, Product> productIdIndex = allProductList.getProduct().stream() .collect(Collectors.toMap(Product::getProductId, Function.identity())); List<String> userRefProductIds = user.getProductRefId(); // 空值兜底处理 if (userRefProductIds == null || userRefProductIds.isEmpty()) { customer.setProducts(new ArrayList<>()); return; } // 根据用户持有的产品ID匹配对应产品,完成字段映射 List<CustProduct> matchedProducts = userRefProductIds.stream() .filter(productIdIndex::containsKey) .map(productId -> mapProduct(productIdIndex.get(productId))) .collect(Collectors.toList()); customer.setProducts(matchedProducts); }
原注释位置的补全逻辑说明
在你之前写的for循环逻辑中,注释位置的实现需要满足:
- 提前将全量
ProductList中的实体按productId存入Map结构,避免双层循环的性能损耗 - 循环内根据当前遍历的
productRefId从Map中取出匹配的Product实体 - 调用已编写的
mapProduct方法将Product转换为CustProduct对象,加入结果列表
内容的提问来源于stack exchange,提问作者Futuregeek
相关产品推荐
相关产品推荐

