gpt4 book ai didi

java - 如何在 mapstruct 中使用来自不同类的另一个映射

转载 作者:行者123 更新时间:2023-12-01 19:28:39 26 4
gpt4 key购买 nike

我想将模型对象映射到 dto 模型。我已经拥有其中一个对象的映射器。如何在另一个类中的另一个映射器中重用此映射器?

我有以下模型

    @Getter
@AllArgsConstructor
@ToString
public class History {

@JsonProperty("identifier")
private final Identifier identifier;

@JsonProperty("submitTime")
private final ZonedDateTime submitTime;

@JsonProperty("method")
private final String method;

@JsonProperty("reason")
private final String reason;

@JsonProperty("dataList")
private final List<Data> dataList;
}

@DynamoDBTable(tableName = "history")
@Data
@NoArgsConstructor
public class HistoryDynamo {
@DynamoDBRangeKey(attributeName = "submitTime")
@DynamoDBTypeConverted(converter = ZonedDateTimeType.Converter.class)
private ZonedDateTime submitTime;

@DynamoDBAttribute(attributeName = "identifier")
@NonNull
private Identifier identifier;

@DynamoDBAttribute(attributeName = "method")
private String method;

@DynamoDBAttribute(attributeName = "reason")
private String reason;

@DynamoDBAttribute(attributeName = "dataList")
private List<Data> dataList;
}

@Data
@DynamoDBDocument
@NoArgsConstructor
public class Identifier implements Serializable {

@DynamoDBAttribute(attributeName = "number")
private String number;

@DynamoDBAttribute(attributeName = "cityCode")
@NonNull
private String cityCode;

@DynamoDBAttribute(attributeName = "countryCode")
@NonNull
private String countryCode;

@DynamoDBTypeConverted(converter = LocalDateType.Converter.class)
private LocalDate mydate;
}

@Data
@EqualsAndHashCode
@NoArgsConstructor
@RequiredArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Identifier implements Serializable {

@NonNull
@lombok.NonNull
@NotNull
private String number;

@NonNull
@lombok.NonNull
@NotNull
private City city;

@NonNull
@lombok.NonNull
@NotNull
private Country country;

@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'Z'")
@DateTimeFormat(pattern = "yyyy-MM-dd'Z'")
@NonNull
@lombok.NonNull
@NotNull
private LocalDate mydate;
}

这是我的映射

    @Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.WARN, injectionStrategy = InjectionStrategy.CONSTRUCTOR, nullValueMappingStrategy = NullValueMappingStrategy.RETURN_NULL)
public interface IdentifierMapper {

IdentifierMapper MAPPER = Mappers.getMapper(IdentifierMapper.class);


@Mappings({@Mapping(source = "identifier.number", target = "number"),
@Mapping(source = "identifier.city.code", target = "cityCode"),
@Mapping(source = "identifier.country.code", target = "countryCode"),
@Mapping(source = "identifier.mydate", target = "mydate")})
@Named("toIdentifierDynamo")
myproject.entity.dynamo.Identifier toIdentifierDynamo(myproject.model.Identifier identifier);
}

@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.WARN, injectionStrategy = InjectionStrategy.CONSTRUCTOR,
nullValueMappingStrategy = NullValueMappingStrategy.RETURN_NULL, uses = {IdentifierMapper.class})
public interface HistoryMapper {

HistoryMapper MAPPER = Mappers.getMapper(HistoryMapper.class);

@Mappings({@Mapping(source = "identifier", target = "identifier", qualifiedByName = "toIdentifierDynamo"),
@Mapping(source = "method", target = "method"),
@Mapping(source = "reason", target = "reason"),
@Mapping(source = "timestamp", target = "timestamp")})
HistoryDynamo toHistoryDynamo(History history);
}

我想将 History 映射到 HistoryDynamo 并重用 IdentifierMapper 来映射 HistoryDynamo 中的对象之一。如何在 toHistoryDynamo 中使用 toIdentifierDynamo?

最佳答案

  • 首先,您不必在 Spring 中创建实例。你可以只需 Autowiring 您的映射器即可。
  • 其次,您不必提供 @Mapping 注释每个字段(如果它具有相同的名称)。 Mapstruct 会为你做这件事。
  • 您的问题可以使用 MapStruct 映射器的 uses 参数来解决HistoryMapper 可以在 @Mapper 注释参数 uses = IdentifierMapper.class 中使用。它将 Autowiring IdentifierMapper历史映射器。默认情况下,它将通过字段执行。你可以改变它也在参数中: injectionStrategy = InjectionStrategy.CONSTRUCTOR 可能就足够了具有相同名称的字段(标识符)和MapStruct应该实现应该使用 IdentifierMapper

关于java - 如何在 mapstruct 中使用来自不同类的另一个映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60523230/

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