如何将 MapStruct 用于不同的数据类型?

Rob*_*ert 4 java spring-boot mapstruct

我有两种类型的数据要映射:

注册用户Dto:

public class SignUpUserDto {
    private String firstName;
    private String lastName;
    private String username;
    private String email;
    private String password;
    private String title;
}
Run Code Online (Sandbox Code Playgroud)

注册用户:

@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;
}
Run Code Online (Sandbox Code Playgroud)

标题:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)
  • 对于DTO标题成员是String

  • 对于实体title 成员是Title

映射器应该是什么样子的?

我应该传递已在Service 中转换的标题吗?

@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);
}
Run Code Online (Sandbox Code Playgroud)

或者我应该在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);
}
Run Code Online (Sandbox Code Playgroud)

Nik*_*nko 5

我应该传递已经在服务中转换的标题吗?

你绝对不应该这样做。这是转换器的工作,而不是服务的工作

尝试以下方法:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 补充一点:我认为到字符串的转换是由 mapstruct 自动完成的,因此您不必为此添加方法。从字符串的转换可以通过支持映射来完成,这可能更有效。 (2认同)