Pagination with spring date

Asked

Viewed 1,239 times

0

I have a list of clients per seller and am trying to create a pagination to show 10 and 10 clients in my modal in PHP. If anyone can help me thank you, because I’m still not good at spring date.

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);

}
  • Hello Bruno, I got it! And it was very simple, as I had never made a pagination like this I was almost crazy kkk but it worked out all right. A friend of this Site posted this link download it because it has a very simplified example and you will understand right away. That’s what he posted "In one of my projects I used this solution, all its documentation is on github: https://github.com/jpenren/thymeleaf-spring-data-dialect"

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 as follows:

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 Clienterepository; One must assign values in the variables used in creating the pageRequest object at the time of construction, I left the comment for you to put the values according to your need.

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.