Paging with spring data

0

I have a list of clients per vendor and I'm trying to create a paging to show 10 and 10 clients on my modal in PHP. If anyone can help me thank you, because I'm still not good at spring data.

Below is my repository:

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import br.com.alpha.core.modelo.entidades.Cliente;

public interface ClienteRepositorio extends JpaRepository<Cliente, Integer> {

    List<Cliente> findByNomeStartingWithAndVendedorEquals(String nome, Integer vendedor);

}
    
asked by anonymous 12.07.2017 / 14:36

2 answers

1

You can do the following:

public interface ClienteRepositorio extends JpaRepository<Cliente, Integer> {

    Page<Cliente> findByNomeStartingWithAndVendedorEquals(String nome, Integer vendedor,  Pageable pageRequest);

}

In the method that calls the query you do this:

public Page<Cliente> buscarCliente(String nome, Integer vendedor,  Pageable pageRequest) {


/*
* page se refere a página de paginação que a consulta retornará o valor (Int)
*linesPerPage = quantidade de objetos que irá retornar em cada página
* direction = Se vai ordenar de forma crescente ou não ASC ou DESC
*orderBy = trata-se do campo utilizado na ordenação
*
*/

PageRequest pageRequest = new PageRequest(page, linesPerPage, Direction.valueOf(direction), orderBy);

Page<Cliente> clientes = clienteRepository.findByNomeStartingWithAndVendedorEquals(String nome, Integer vendedor,  pageRequest);
}

Remember that in the service class you must inject the class ClientRepository; You should assign values in the variables used in the creation of the pageRequest object at the time of construction, I left the comment so that you put the values according to your need.

    
26.05.2018 / 14:00
0

Hello, you can search the use of Spring's PageRequest, with it you already call the query passing this object as a return (you will pass the offset, the total) and it returns you a page already made.

You can see more about PageRequest here: link

    
20.10.2017 / 13:36