简体   繁体   中英

Pagination with spring data JPA

I hava to do a pagination request in this GET but I'm not getting it.

@GetMapping(value = {"uf/{uf}"})
    public List<Cidade> findByUF(@PathVariable String uf, Pageable page){
         return repositorioCidade.findByUF(uf);        
    }

Repository:

public interface RepositorioCidade extends JpaRepository<Cidade,Long>{
    @Query(nativeQuery= true, value="SELECT * FROM cidade WHERE uf = ?")
    List<Cidade> findByUF(String uf);
    @Query(nativeQuery= true, value="SELECT * FROM cidade WHERE cidade = ?")
    List<Cidade> findByNome(String nome);
    @Query(nativeQuery= true, value="SELECT COUNT(*) FROM cidade")
    List<Cidade> countRegistros(String cidade);
}

You can make use of method queries. In your repository add the following method query:

public interface RepositorioCidade extends JpaRepository<Cidade,Long> {

    Page<Cidade> findByUf(String uf, Pageable pageable);

}

And change your controller to this:

@GetMapping(value = {"uf/{uf}"})
public Page<Cidade> findByUF(@PathVariable String uf, Pageable pageable){
     return repositorioCidade.findByUf(uf, pageable);        
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM