我有两种类型的数据要映射:
注册用户Dto:
public class SignUpUserDto {
private String firstName;
private String lastName;
private String username;
private String email;
private String password;
private String title;
}
注册用户:
@Entity
public class SignUpUser {
private Long id;
private String firstName;
private String lastName;
private String username;
private String email;
private String password;
private Title title;
}
标题:
public enum Title {
JUNIOR("junior"),
MIDDLE("middle"),
SENIOR("senior"),
MANAGER("manager");
private final String title;
Title(final String title) {
this.title = title;
}
public String toString() {
return this.title;
}
}
对于DTO title 成员是一个String。
对于实体title 成员是Title。
映射器应该是什么样子的?
我应该传递已经在服务中转换的标题吗?
@Mapper(componentModel = "spring")
public interface SignUpUserMapper {
SignUpUserMapper INSTANCE = Mappers.getMapper(SignUpUserMapper.class);
@Mapping(target = "title", expression = "title")
public SignUpUserDto signUpUserToSignUpUserDto(SignUpUser signUpUser, String title);
@Mapping(target = "title", source = "title")
public SignUpUser signUpUserDtoToSignUpUser(SignUpUserDto signUpUserDto, Title title);
}
或者我应该在Mapper中进行转换?
@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(new Title(signUpUserDto.getTitle()))")
public SignUpUser signUpUserDtoToSignUpUser(SignUpUserDto signUpUserDto);
}
神不在的星期二
慕码人8056858
相关分类