我喜欢使用 mapstruct,但我找不到:是否有一个函数可以将 Pageable 中的 Sort 转换为映射的 dto pageable 到实体 pagable?
链接:
可分页:https ://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/domain/Pageable.html
排序:https : //docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/domain/Sort.html
故事:
有一个请求:
@GetMapping("find")
public List<DTO> findAll(final Pageable pageable) {
return mapper.map(repository.findAll(pagable));
}
Run Code Online (Sandbox Code Playgroud)
和存储库:
public interface Repository extends JpaRepository<Entity, Long> {
Page<Entity> findAll(Pageable pageable);
}
Run Code Online (Sandbox Code Playgroud)
如果我不想公开实体列名称,那么我会映射该列。例如:
@Mapper
public interface DTOMapper {
@Mappings({
@Mapping(source = "make", target = "manufacturer"),
})
DTO toDto(Entity entity);
}
Run Code Online (Sandbox Code Playgroud)
现在 API/返回值有manufacturer. 的使用manufacturer用于排序请求代替(包括在可分页)make是可能的:sort=manufacturer;asc。
但一定不是sort=make;asc,或者可以JpaRepository处理它吗?
那么有没有一种简单的方法可以将可分页从请求转换为正确的可分页(使用正确的排序)?
谢谢你的答案。