小编Ric*_*ler的帖子

是否有使用 mapstruct 的 spring pageable/sort 自动映射

我喜欢使用 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处理它吗?

那么有没有一种简单的方法可以将可分页从请求转换为正确的可分页(使用正确的排序)?

谢谢你的答案。

java spring-boot mapstruct

3
推荐指数
1
解决办法
5590
查看次数

标签 统计

java ×1

mapstruct ×1

spring-boot ×1