例如,我有一个字段的映射类,没有在映射类中显示。
我要映射的一个类:
@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
不是一个选项。有什么建议?
如果我理解你..你想控制你不想映射的源属性?
在那种情况下尝试:
@BeanMapping#ignoreUnmappedSourceProperties
所以:
@Mapper(componentModel = "spring",
unmappedTargetPolicy = ReportingPolicy.ERROR,
unmappedSourcePolicy = ReportingPolicy.ERROR)
public interface CallMapper {
@IterableMapping(qualifiedByName = "map")
List<ConnectionDto> map(List<ConnectionEntity> connectionEntities);
@BeanMapping( ignoreUnmappedSourceProperties={"id"} )
ConnectionDto map(ConnectionEntity connectionEntity);
}
您不需要指定列表映射,除非您需要从外部进行此操作.MapStruct将为您生成一个..如果您确实需要从外部列表,您可能不需要限定符..通用+列表就够了