我正在嘗試制作一個自動映射器,但我的EnumDTO 內部有問題。在我的@mapper課上,我希望除了Enum欄位之外的所有內容都被自動映射。但是,在除錯時,我看到它確實進入了映射器自動實作類,并且它沒有Enum像我用@mapping 指示的那樣忽略我
我的物體和 DTO 示例
@Entity
public class Document {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
private String type;
我的 DTO:
@Data
public class DocumentDTO {
private Integer id;
private DocumentsEnum type;
如何忽略該特定列舉欄位的映射?
我在@mapping 上嘗試了以下注釋:
@Mapper
public interface DocumentsMapper {
@Mapping(source = "type", target = "type", ignore = true)
@Mapping(source = "DocumentsEnum", target = "DocumentsEnum", ignore = true)
@Mapping(source = "MAIN", target = "MAIN", ignore = true) // <-- This is the value of Enum
@Mapping(source = "ppal", target = "ppal", ignore = true) // <-- This is the name of Enum
List<DocumentDTO> mapper(List<Document> document);
@AfterMapping
default void afterMapping(Document document, @MappingTarget DocumentDTO documentDTO) {
//some logic
}
uj5u.com熱心網友回復:
Mapping宣告忽略要忽略的欄位的正確方法是第一個,您可以洗掉其他欄位。
@Mapping(source = "type", target = "type", ignore = true)
這對您不起作用的原因是您在技術上將一個List物件映射到另一個List物件。您傳入的要映射的List物件可能沒有type欄位(而且絕對不是您關心的欄位),但它們持有的物件(Document和DocumentDTO)會有。
為了使它按照他們的方式作業,您需要兩個映射器:一個用于Lists ,一個用于Documentto DocumentDTO。由于type您要忽略的欄位位于后面的物件上,因此您應該將Mapping注釋放在該方法上:
@Mapper
public interface DocumentsMapper {
List<DocumentDTO> listMapper(List<Document> document);
@Mapping(source = "type", target = "type", ignore = true)
DocumentDTO mapper(Document document);
該mapstruct庫足夠智能,可以自動使用mapper方法來轉換方法中的每個List元素listMapper。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/536408.html
標籤:爪哇春天界面映射器
