例如,我有一个带有字段的映射类,该字段未在映射类中呈现。
我想要映射的类:
@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 将为您生成一个列表映射。如果您确实需要从外部列表,则可能不需要限定符。 .泛型+列表就够了
关于java - 如何禁用 MapStruct 中源映射中的字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54863315/