如何在Spring Boot中对存储库实体进行分页排序

huangapple go评论69阅读模式
英文:

How to sorting repository entity with pagination in spring boot

问题

我正在使用Spring Boot编写一个REST API。在这里,我尝试使用'PagingAndSortingRepository'对列表进行排序。这是我尝试的方法:

在我的Controller类中的方法:

@GetMapping
public ResponseEntity<PagedModel<ComplainDTO>> getComplains(
        @RequestParam(defaultValue = "0", value = "page", required = false) int page,
        @RequestParam(value = "sortBy", required = false, defaultValue = "complainId") String sortBy,
        PagedResourcesAssembler assembler) { 
    return ResponseEntity.status(HttpStatus.OK)
        .body(assembler.toModel(complainService.getAllComplains(page, sortBy)));
}

在我的服务中的方法:

@Override
public Page<ComplainDTO> getAllComplains(int page, String sortBy) {
    Pageable pageable = PageRequest.of(page, 20, Sort.by(sortBy));
    Page<ComplainEntity> result = complainRepository.findAll(pageable);
    return result.map(complainEntity -> toDTO(complainEntity));
}

我正在从消费者那里获取输入,以根据给定的属性对列表进行排序,然而Sort的默认行为是升序的。那么,我如何实现在使用if-else之外的情况下,根据用户参数来实现升序或降序的功能呢?

英文:

I am writing an REST API with Spring Boot.Here,I am trying to sort the list using the 'PagingAndSortingRepository'.This is what I have tried
Method in my Controller class

@GetMapping public ResponseEntity
&lt;PagedModel&lt;ComplainDTO&gt;&gt; getComplains(@RequestParam(defaultValue = &quot;0&quot;,value = &quot;page&quot;,required =false)int page, @RequestParam(value=&quot;sortBy&quot; ,required = false,defaultValue = &quot;complainId&quot;) String sortBy, PagedResourcesAssembler assembler) { 
    return ResponseEntity.status(HttpStatus.OK)
     .body(assembler.toModel(complainService.getAllComplains(page,sortBy)));
  }

Method on my service

@Override
    public Page&lt;ComplainDTO&gt; getAllComplains(int page,String sortBy) {
        
        Pageable pageable = PageRequest.of(page,20, Sort.by(sortBy));
        Page&lt;ComplainEntity&gt; result =complainRepository.findAll(pageable);
        return result.map(complainEntity -&gt; toDTO(complainEntity));
    }

I am taking input from the consumer to sort the list based on given attribute however the default behavior of Sort is Ascending .So,how can I implement this taking user params whether it being ascending or descending aside from using if-else.

答案1

得分: 0

以下是翻译好的内容:

其中一个选项是要求用户使用@RequestParam提供排序顺序

@GetMapping
public ResponseEntity<PagedModel<ComplainDTO>> getComplains(
        @RequestParam(defaultValue = "0", value = "page", required = false) int page,
        @RequestParam(value = "sortBy", required = false, defaultValue = "complainId") String sortBy,
        @RequestParam(value = "orderBy", required = false, defaultValue = "ASC") String orderBy,
        PagedResourcesAssembler assembler) {

    return ResponseEntity.status(HttpStatus.OK)
            .body(assembler.toModel(complainService.getAllComplains(page, sortBy, orderBy)));
}

然后在服务中将排序顺序传递给Sort方法

@Override
public Page<ComplainDTO> getAllComplains(int page, String sortBy, String orderBy) {

    Pageable pageable = PageRequest.of(page, 20, Sort.by(Sort.Direction.valueOf(orderBy), sortBy));
    Page<ComplainEntity> result = complainRepository.findAll(pageable);
    return result.map(complainEntity -> toDTO(complainEntity));
}
英文:

The one option is asking the user to provide the sorting order using @RequestParam

 @GetMapping public ResponseEntity
&lt;PagedModel&lt;ComplainDTO&gt;&gt; getComplains(@RequestParam(defaultValue = &quot;0&quot;,value = &quot;page&quot;,required =false)int page, 
        @RequestParam(value=&quot;sortBy&quot; ,required = false,defaultValue = &quot;complainId&quot;) String sortBy, 
        @RequestParam(value=&quot;orderBy&quot; ,required = false,defaultValue = &quot;ASC&quot;) String orderBy, PagedResourcesAssembler assembler) { 

return ResponseEntity.status(HttpStatus.OK)
 .body(assembler.toModel(complainService.getAllComplains(page,sortBy,orderBy)));

}

And then in the service pass the sorting order to Sort method

@Override
public Page&lt;ComplainDTO&gt; getAllComplains(int page,String sortBy, String orderBy) {

    Pageable pageable = PageRequest.of(page,20, Sort.by(Sort.Direction.valueOf(orderBy),sortBy));
    Page&lt;ComplainEntity&gt; result =complainRepository.findAll(pageable);
    return result.map(complainEntity -&gt; toDTO(complainEntity));
}

huangapple
  • 本文由 发表于 2020年4月9日 13:41:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/61114659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定