基于MapStruct实现CustomerProfile与DO类的嵌套映射技术问询
嘿,我来帮你搞定这个MapStruct的嵌套映射问题!先得修正一下你给出的DO类里的拼写错误——CustomeDO应该是CustomerDO,CustomerIdentiferDO应该是CustomerIdentifierDO,不然编译的时候肯定会报错哒!下面一步步来实现:
解决MapStruct嵌套映射:CustomerProfile ↔ CustomerProfileDO
第一步:整理修正后的实体/DO类
先把所有类补全getter/setter(MapStruct需要访问字段或者有getter方法)并修正拼写:
实体类(Entity)
import java.util.List; // 假设Status类的定义(你没给出,这里补个基础版) class Status { private String code; private String description; // getter & setter public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } } class Identifier { private long id; private String type; private List<Status> statuses; // getter & setter public long getId() { return id; } public void setId(long id) { this.id = id; } public String getType() { return type; } public void setType(String type) { this.type = type; } public List<Status> getStatuses() { return statuses; } public void setStatuses(List<Status> statuses) { this.statuses = statuses; } } class Customer { private Identifier identifier; // getter & setter public Identifier getIdentifier() { return identifier; } public void setIdentifier(Identifier identifier) { this.identifier = identifier; } } class CustomerProfile { private Customer customer; // getter & setter public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } }
数据对象(DO)
import java.util.List; class CustomerIdentifierDO { private long id; // getter & setter public long getId() { return id; } public void setId(long id) { this.id = id; } } // 修正拼写:CustomeDO → CustomerDO class CustomerDO { // 修正拼写:CustomerIdentiferDO → CustomerIdentifierDO private CustomerIdentifierDO custID; // getter & setter public CustomerIdentifierDO getCustID() { return custID; } public void setCustID(CustomerIdentifierDO custID) { this.custID = custID; } } class CustomerProfileDO { private String category; private List<Status> custStatuses; private CustomerDO customer; // getter & setter public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } public List<Status> getCustStatuses() { return custStatuses; } public void setCustStatuses(List<Status> custStatuses) { this.custStatuses = custStatuses; } public CustomerDO getCustomer() { return customer; } public void setCustomer(CustomerDO customer) { this.customer = customer; } }
第二步:编写CustomerProfileMapper接口
接下来实现核心的映射逻辑,用MapStruct的注解处理嵌套层级和字段名不匹配的问题:
import org.mapstruct.Mapper; import org.mapstruct.Mapping; import org.mapstruct.Mappings; import org.mapstruct.factory.Mappers; // 如果是Spring环境,加上@ComponentModel = "spring"就能直接注入使用 @Mapper public interface CustomerProfileMapper { // 非Spring环境用这个单例实例获取Mapper CustomerProfileMapper INSTANCE = Mappers.getMapper(CustomerProfileMapper.class); // 核心映射方法:CustomerProfile → CustomerProfileDO @Mappings({ // 映射嵌套层级的ID:CustomerProfile.customer.identifier.id → CustomerProfileDO.customer.custID.id @Mapping(source = "customer.identifier.id", target = "customer.custID.id"), // 映射状态列表:把Identifier里的statuses映射到CustomerProfileDO的custStatuses @Mapping(source = "customer.identifier.statuses", target = "custStatuses"), // 如果需要给category设置默认值,加这行: // @Mapping(target = "category", defaultValue = "DEFAULT_CATEGORY") }) CustomerProfileDO toCustomerProfileDO(CustomerProfile customerProfile); // 可选:显式声明嵌套映射方法(MapStruct能自动推导,但显式写出来更清晰) @Mapping(source = "identifier.id", target = "custID.id") CustomerDO toCustomerDO(Customer customer); // 可选:Identifier到CustomerIdentifierDO的映射 CustomerIdentifierDO toCustomerIdentifierDO(Identifier identifier); }
关键逻辑说明
- 嵌套路径映射:因为实体和DO的字段层级、名称不匹配(比如
identifiervscustID),必须用source指定完整的实体字段路径,target指定DO的对应路径。 - 集合自动映射:MapStruct会自动处理
List<Status>的映射,只要Status类的字段匹配(或者你定义了Status的映射方法)。 - 默认值处理:如果CustomerProfile没有
category字段,但DO需要这个值,可以用defaultValue给它设置默认值。
使用示例
public class MappingDemo { public static void main(String[] args) { // 构造测试数据 Status status = new Status(); status.setCode("ACTIVE"); status.setDescription("客户状态正常"); Identifier identifier = new Identifier(); identifier.setId(1001L); identifier.setType("USER_ID"); identifier.setStatuses(List.of(status)); Customer customer = new Customer(); customer.setIdentifier(identifier); CustomerProfile profile = new CustomerProfile(); profile.setCustomer(customer); // 执行映射 CustomerProfileDO profileDO = CustomerProfileMapper.INSTANCE.toCustomerProfileDO(profile); // 验证结果 System.out.println(profileDO.getCustomer().getCustID().getId()); // 输出1001 System.out.println(profileDO.getCustStatuses().get(0).getCode()); // 输出ACTIVE } }
内容的提问来源于stack exchange,提问作者Naveen
相关产品推荐
相关产品推荐

