Spring Custom Query with pageable

前端 未结 6 748
自闭症患者
自闭症患者 2020-12-30 07:29

I want to to implement pagination in spring application.I know using repository we can implement pagination but we can not write our own query for data retrieve there are li

相关标签:
6条回答
  • 2020-12-30 07:56

    Use MongoRepository. Extend MongoRepository as

    public interface FooRepository extends MongoRepository<Foo,String> {    
        @Query(value="{'name': ?0}");
        Page<Foo> findByMethod(String name, Pageable pageable);
    }
    

    Then, use it as

    Page fooPage = FooRepository.findByMethod('John', new PageRequest(0,20));
    
    0 讨论(0)
  • 2020-12-30 08:02

    As you figured out, MongoTemplate doesn't support the complete page abstraction. Like KneeLess said you can use the @Query-Annotation to do some custom queries.

    In case this isn't enough for you, can use utilize the Spring Repository PageableExecutionUtils in combination with your MongoTemplate.

    For example like this:

    @Override
    public Page<XXX> findSophisticatedXXX(/* params, ... */ @NotNull Pageable pageable) {
    
        Query query = query(
                where("...")
                // ... sophisticated query ...
        ).with(pageable);
    
        List<XXX> list = mongoOperations.find(query, XXX.class);
        return PageableExecutionUtils.getPage(list, pageable,
                  () -> mongoOperations.count((Query.of(query).limit(-1).skip(-1), XXX.class));
    }
    

    Spring Repositories are doing the same. As you can see here, they fire two queries as well.

    0 讨论(0)
  • 2020-12-30 08:03

    Just putting it out in case someone needs it.

    SpringData has a method for custom query:

    final Pageable pageableRequest = new PageRequest(0, 2);
    Query query = new Query();
    query.with(pageableRequest);
    
    0 讨论(0)
  • 2020-12-30 08:03
    Pageable pageableBase = PageRequest.of(0, request.getSize());
    List <User> users = userRepository.findAllSignUpComplete(true, pageableBase);
    

    public interface UserRepository extends PagingAndSortingRepository<User, String> {...

    0 讨论(0)
  • 2020-12-30 08:05

    By extending Spring Data PagingAndSortingRepository interface you can get some common methods such as save, find, findAll and delete and also you can add your own custom queries:

    public interface Repository extends PagingAndSortingRepository<Book, ID extends Serializable> {
    
        // Common method
        Page<Book> findAll(Pageable pageable);
    
        // Custom query based on Spring Data naming convention
        Page<Book> findByNameOrDescription(String name, String description, Pageable pageable);
    
    }
    
    0 讨论(0)
  • 2020-12-30 08:16

    I am providing a code snippet on how we implement pagination using springboot with jpa simply using PagingAndSortingRepository which contains inbuild method for pagination.

    public interface PersonRepository extends PagingAndSortingRepository<Person, Integer> {
    
    }
    
      @Service
      public class PersonServiceImpl implements PersonService{
      
      @Autowired
      private PersonRepository personRepository;
    
      public Page<Person> listAll(int pageNumber){
       if(pageNumber>0){
      Pageable pageWithTenElements = PageRequest.of(pageNumber-1,10);
      //first param decide page and second param decide no of record
    
       return personRepository.findAll(pageWithTenElements);}
       else 
       return null;
      }
    
      }
    
      @RestController
      public class AppController {
      
      @Autowired
      private PersonService personService;
    
       @GetMapping("/page/{pageNumber}")
       public ResponseEntity<List<Person>> getPersons(@PathVariable("pageNumber") pageNumber){
       List<Person> persons = personService.listAll(pageNumber).getContent();
       ResponseEntity<SkillsTierResponse> response = 
                               new ResponseEntity<List<Person>>(persons,HttpStatus.OK);
            
            return response;
       }
    
      }
    
    0 讨论(0)
提交回复
热议问题