【发布时间】:2021-04-26 00:15:12
【问题描述】:
在我的 SpringBoot 应用程序中,我有两个实体 User 和 Role
public class User {
private Long id;
private String email;
private String password;
private Set<Role> roles;
[...]
}
public class Role {
private Long id;
private String name;
private Set<User> users;
[...]
}
我的 DTO 看起来非常相似,直到我意识到这可能会导致递归问题,当 user 的字段 role 具有相同的 user 字段时,该字段具有相同的 @987654327 字段@等
因此我决定只交给我的 DTO 的 ids,所以它们看起来像这样
public class UserDto {
private Long id;
private String email;
private String password;
private List<Long> roleIds;
}
public class RoleDto {
private Long id;
private String name;
private List<Long> userIds;
}
我的映射器非常简单,过去看起来像这样
import org.mapstruct.Mapper;
@Mapper
public interface UserMapper {
User userDtoToUser(UserDto userDto);
UserDto userToUserDto(User user);
List<UserDto> userListToUserDtoList(List<User> users);
}
import org.mapstruct.Mapper;
@Mapper
public interface RoleMapper {
Role roleDtoToRole(RoleDto roleDto);
RoleDto roleToRoleDto(Role Role);
List<RoleDto> roleListToRoleDtoList(List<Role> roles);
}
我将如何更改它们以便它们将users 转换为/从userIds 和roles 转换为/从roleIds?
【问题讨论】:
标签: java spring-boot type-conversion mapstruct