![](/img/trans.png)
[英]Can I mapping with mapstruct a class that extends from ArrayList<Object>?
[英]How can I use another mapping from different class in mapstruct
我想將模型對象映射到 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?
@Mapping
為每個字段提供@Mapping
注釋。 Mapstruct 將為您完成。HistoryMapper
uses
參數來完成,該參數可以在@Mapper
注釋參數uses = IdentifierMapper.class
。 它將自動連接IdentifierMapper
到HistoryMapper
。 默認情況下,它將通過字段執行。 您也可以在參數中更改它: injectionStrategy = InjectionStrategy.CONSTRUCTOR
並且可能就足夠了,因為您具有相同的字段名稱(標識符)並且 MapStruct 應該意識到應該使用IdentifierMapper
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.