2 回答
TA贡献1963条经验 获得超6个赞
我应该传递已经在服务中转换的标题吗?
你绝对不应该这样做。这是转换器的工作,而不是服务的
尝试以下方法:
1)枚举类添加转换方法
enum Title {
...
public static Title fromString(String title) {
if (title != null) {
for (Title t : Title.values()) {
if (t.toString().equals(title)) {
return t;
}
}
}
return null;
}
}
2) Mapper 接口增加2个转换方法(仅限Java 8+)
@Mapper(componentModel = "spring")
public interface SignUpUserMapper {
SignUpUserDto signUpUserToSignUpUserDto(SignUpUser signUpUser);
SignUpUser signUpUserDtoToSignUpUser(SignUpUserDto signUpUserDto);
default String fromEnum(Title title) {
return title == null ? null : title.toString();
}
default Title toEnum(String title) {
return title == null ? null : Title.fromString(title);
}
}
TA贡献1803条经验 获得超6个赞
像这样使用第二个选项:
@Mapper(componentModel = "spring", imports = Title.class)
public interface SignUpUserMapper {
SignUpUserMapper INSTANCE = Mappers.getMapper(SignUpUserMapper.class);
@Mapping(target = "title", expression = "java(signUpUser.getTitle().toString())")
public SignUpUserDto signUpUserToSignUpUserDto(SignUpUser signUpUser);
@Mapping(target = "title", source = "java(Title.valueOf(signUpUserDto.getTitle().toUpperCase()))")
public SignUpUser signUpUserDtoToSignUpUser(SignUpUserDto signUpUserDto);
}
添加回答
举报