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 楼答案
我猜(因为您没有输入
buider
代码),问题是您的生成器类不包含父类字段。MapStruct在为mapper生成代码时做了一些假设。从documentation-如果您使用的是Lombok,那么可以通过使用
@SuperBuilder
as-看起来像——