【发布时间】:2019-02-26 06:33:19
【问题描述】:
例如,我有一个带有字段的映射类,该字段未在映射类中显示。
一个类,哪个我想映射:
@Entity
@Table(name = "t_connection")
@Getter @Setter
@EqualsAndHashCode
public class ConnectionEntity {
@NotNull
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
protected UUID id;
...
}
一个类,我想在其中进行映射:
@ApiModel
@Getter
@Setter
@NoArgsConstructor
public class ConnectionDto {
@ApiModelProperty
private LocalDateTime createAt;
...
// Other fields without id field
}
我的映射器如下所示:
@Mapper(componentModel = "spring",
unmappedTargetPolicy = ReportingPolicy.ERROR,
unmappedSourcePolicy = ReportingPolicy.ERROR)
public interface CallMapper {
@IterableMapping(qualifiedByName = "map")
List<ConnectionDto> map(List<ConnectionEntity> connectionEntities);
ConnectionDto map(ConnectionEntity connectionEntity);
}
我想知道,当没有映射特定字段时,禁用unmappedSourcePolicy 不是一个选项。有什么建议吗?
【问题讨论】:
-
这似乎是不可能的,有一种相当愚蠢的方法就是手写
map()。default ConnectionDto map(ConnectionEntity connectionEntity) { ConnectionDto dto = new ConnectionDto(); dto.set ... (connectionEntity.get... ); return dto; } -
有可能。感谢@sjaak。看到那个答案:stackoverflow.com/a/54875713/11112760