有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在MapStruct中映射扩展类

关于mapStruct的问题。在这种情况下,我从基本实体扩展类,但不确定如何映射它。这是我的案子

基本实体:

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

BaseDto:

public class BaseDto {

    private Long id;
}

用户实体:

public class User extends BaseEntity {
    private String name;
    private String lastName;
    private String username;
    private String password;
    private String profilePicturePath;
}

用户地址:

public class UserDto extends BaseDto {
    private String name;
    private String lastName;
    private String username;
    private String password;
    private String profilePicturePath;
}

mapper是这样的:

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

    User userDtoToUser(UserDto userDto);

    UserDto userToUserDto(User user);
}

基线映射器:

@Mapper
public interface BaseMapper {

    BaseEntity dtoToEntity(BaseDto baseDto);

    BaseDto entityToDto(BaseEntity baseEntity);
}

问题是我没有得到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();
    }

共 (1) 个答案

  1. # 1 楼答案

    我猜(因为您没有输入buider代码),问题是您的生成器类不包含父类字段。MapStruct在为mapper生成代码时做了一些假设。从documentation-

    The default implementation of the BuilderProvider assumes the following:

    1. 该类型具有无参数的公共静态生成器创建方法 返回一个生成器。例如,一个人有一个公共静态 方法返回PersonBuilder
    2. 生成器类型有一个无参数的公共方法(build method) 返回我们示例中生成的类型PersonBuilder有 方法返回人
    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;
    }
    

    看起来像——

    @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();
    }