gpt4 book ai didi

java - 映射嵌套列表

转载 作者:行者123 更新时间:2023-12-01 16:30:33 24 4
gpt4 key购买 nike

我有一个带有嵌套对象列表的对象

@Getter
@Setter
@NoArgsConstructor
public class Notification {
private Long id
private Long statusId;
private List <External> external;
}


@Getter
@Setter
@NoArgsConstructor
public class External{
private Long externalId;
private LocalDate date;
}

D至

@Getter
@Setter
@NoArgsConstructor
public class NotificationPayload {
private Long id;
private Long statusId;
private List <ExternalReferencePayload> external;
}


@Getter
@Setter
@NoArgsConstructor
public class ExternalReferencePayload {
private Long externalReferenceId;
}

映射器

@Mapper(componentModel = "spring")
public interface NotificationMapper{

public Notification dtoToNotification(NotificationPayload payload);
}

我搜索映射嵌套列表的方法

最佳答案

为了对某些元素执行自定义映射,只需要定义一个映射方法,MapStruct 将处理其余的事情。在您的示例中:

@Mapper(componentModel = "spring")
public interface NotificationMapper{

public Notification dtoToNotification(NotificationPayload payload);

@Mapping(target = "externalId", source = "externalReferenceId")
public External dtoExternal(ExternalReferencePayload payload);
}

这样,嵌套列表将使用 dtoExternal 映射方法来执行映射。使用 @Mapping,您可以控制 externalIdexternalReferenceId 之间的映射方式

关于java - 映射嵌套列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62049980/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com