有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java映射嵌套列表

我有一个包含嵌套对象列表的对象

@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;
}

Dto

@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);
}

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


共 (1) 个答案

  1. # 1 楼答案

    为了对某些元素执行自定义映射,只需要定义一个映射方法,其余的由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之间的映射应该如何进行