MapStruct 从映射器中排除了很多字段
MapStruct exclude LOT of fields from mapper
假设我有一个复杂的 entity
,如下所示:
class A {
private String a;
private String b;
private String c;
/**
* so on */
private B bb;
private C cc;
}
和相应的 DTO
:
class ADTO {
private String a;
private String b;
private String c;
/**
* so on */
private BDTO bb;
private CDTO cc;
}
现在假设 C entity
(和 CDTO
)有很多变量,我希望 map 只包含他的 ID 字段。例如
@Mapper(componentModel = "spring")
public Interface AMapper {
BDTO bToDto(B b);
B bFromDto(BDTO bDto);
CDTO cToDto(C c); // for this I want to map just the id!!
C cFromDto(CDTO cDto); // even for this
}
我该怎么办??我不会写50次@Mapper(properties = "someField", ignore = true)
,还有别的方法吗??
谢谢
您可以为 C class 创建一个单独的映射器并使用 @Mapper
的 uses
字段添加到 AMapper
class
@Mapper(componentModel = "spring", uses={"CMapper.class"})
public Interface AMapper {
BDTO bToDto(B b);
B bFromDto(BDTO bDto);
// remove below
// CDTO cToDto(C c);
// C cFromDto(CDTO cDto);
}
public CMapper{
public C fromDTO(CDTO) {
// add mapping for ID only
}
public CDTO toDTO(C) {
// add mapping for ID only
}
}
假设我有一个复杂的 entity
,如下所示:
class A {
private String a;
private String b;
private String c;
/**
* so on */
private B bb;
private C cc;
}
和相应的 DTO
:
class ADTO {
private String a;
private String b;
private String c;
/**
* so on */
private BDTO bb;
private CDTO cc;
}
现在假设 C entity
(和 CDTO
)有很多变量,我希望 map 只包含他的 ID 字段。例如
@Mapper(componentModel = "spring")
public Interface AMapper {
BDTO bToDto(B b);
B bFromDto(BDTO bDto);
CDTO cToDto(C c); // for this I want to map just the id!!
C cFromDto(CDTO cDto); // even for this
}
我该怎么办??我不会写50次@Mapper(properties = "someField", ignore = true)
,还有别的方法吗??
谢谢
您可以为 C class 创建一个单独的映射器并使用 @Mapper
uses
字段添加到 AMapper
class
@Mapper(componentModel = "spring", uses={"CMapper.class"})
public Interface AMapper {
BDTO bToDto(B b);
B bFromDto(BDTO bDto);
// remove below
// CDTO cToDto(C c);
// C cFromDto(CDTO cDto);
}
public CMapper{
public C fromDTO(CDTO) {
// add mapping for ID only
}
public CDTO toDTO(C) {
// add mapping for ID only
}
}