如何在 MapStruct 中映射扩展类

lab*_*uka 6 java mapstruct

关于mapStruct的问题。我有这样的情况:我从基本实体扩展类,但不确定如何映射它。这是我的案例。

基础实体:

public class BaseEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "id")
    private Long id;
}
Run Code Online (Sandbox Code Playgroud)

基数为:

public class BaseDto {

    private Long id;
}
Run Code Online (Sandbox Code Playgroud)

用户实体:

public class User extends BaseEntity {
    private String name;
    private String lastName;
    private String username;
    private String password;
    private String profilePicturePath;
}
Run Code Online (Sandbox Code Playgroud)

用户D至:

public class UserDto extends BaseDto {
    private String name;
    private String lastName;
    private String username;
    private String password;
    private String profilePicturePath;
}
Run Code Online (Sandbox Code Playgroud)

映射器是这样的:

@Mapper(uses = {BaseMapper.class})
public interface UserMapper {

    User userDtoToUser(UserDto userDto);

    UserDto userToUserDto(User user);
}
Run Code Online (Sandbox Code Playgroud)

基础映射器:

@Mapper
public interface BaseMapper {

    BaseEntity dtoToEntity(BaseDto baseDto);

    BaseDto entityToDto(BaseEntity baseEntity);
}
Run Code Online (Sandbox Code Playgroud)

问题是我没有映射 ID 属性。

感谢您的时间。

编辑:

没有显示错误,在映射器实现(生成的代码)中没有该 ID 的映射:

 @Override
    public User userDtoToUser(UserDto userDto) {
        if ( userDto == null ) {
            return null;
        }

        UserBuilder user = User.builder();

        user.name( userDto.getName() );
        user.lastName( userDto.getLastName() );
        user.username( userDto.getUsername() );
        user.password( userDto.getPassword() );
        user.profilePicturePath( userDto.getProfilePicturePath() );

        return user.build();
    }
Run Code Online (Sandbox Code Playgroud)

Pan*_*kaj 5

我猜测(因为您没有放置buider代码)问题是您的构建器类不包含父类字段。MapStruct 在为映射器生成代码时做出一些假设。来自文档-

BuilderProvider 的默认实现假设如下:

  1. 该类型具有返回构建器的无参数公共静态构建器创建方法。例如,Person 有一个返回 PersonBuilder 的公共静态方法。
  2. 构建器类型有一个无参数公共方法(构建方法),它返回正在构建的类型。在我们的示例中,PersonBuilder 有一个返回 Person 的方法。
  3. 如果有多个构建方法,MapStruct 将查找名为 build 的方法,如果存在该方法,则将使用该方法,否则将创建编译错误。

如果您使用 Lombok,您可以通过使用@SuperBuilderas 来解决这个问题 -

@SuperBuilder
@Getter
@ToString
public class UserDto extends BaseDto {
  private String name;
  private String lastName;
  private String username;
  private String password;
  private String profilePicturePath;
}

@Getter
@SuperBuilder
class BaseDto {
  private Long id;
}

@SuperBuilder
@Getter
@ToString
public class User extends BaseEntity {
  private String name;
  private String lastName;
  private String username;
  private String password;
  private String profilePicturePath;
}

@Setter
@Getter
@SuperBuilder
class BaseEntity {
  private Long id;
}
Run Code Online (Sandbox Code Playgroud)

生成的可能看起来像 -

@Override
public User userDtoToUser(UserDto userDto) {
    if ( userDto == null ) {
        return null;
    }

    UserBuilder<?, ?> user = User.builder();

    user.id( userDto.getId() );
    user.name( userDto.getName() );
    user.lastName( userDto.getLastName() );
    user.username( userDto.getUsername() );
    user.password( userDto.getPassword() );
    user.profilePicturePath( userDto.getProfilePicturePath() );

    return user.build();
}
Run Code Online (Sandbox Code Playgroud)