1

I create web application in spring boot using the postgress database.

I want to limit the number of records per page(now it's 30,000 records - it's loading a long time), so what should i do to limit it? I use thymeleaf.

Model:

@Entity(name="articles")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Articles {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long article_id;
private String title;
private String issn;
private String eissn;
private String title2;
private String issn2;
private String eissn2;
private Integer points;

@ManyToMany
@JoinTable(
        name = "articles_categories",
        joinColumns = @JoinColumn(name = "article_id"),
        inverseJoinColumns = @JoinColumn(name = "category_id"))
private List<Category> categories;
....

getters and setters

Repository:

public interface ArticlesRepository extends JpaRepository<Articles,Long> {
}

Controller:

@Controller
@RequestMapping("/articles")
public class ArticlesController {


private ArticleService articleService;

@Autowired
public void setArticleService(ArticleService articleService) {
    this.articleService = articleService;
}

@GetMapping 
public String getAll(Model model)
{
    model.addAttribute("articles", articleService.list());
    return "articles"; 
}

Service:

@Service
public class ArticleService {


@Autowired
private ArticlesRepository articlesRepository;

public ArticleService() {
}

public List<Articles> list(){
    return articlesRepository.findAll();
}}
spaceoddity11
  • 91
  • 1
  • 3
  • 10

1 Answers1

1

Use Pageable to limit the size of your articles.

public List<Articles> list(int page, int limit){
    Page<Articles> pageableArticales = articlesRepository.findAll(PageRequest.of(page, limit);
    return pageableArticales.getContent();
}

Note that repository.findAll(pageable) wraps the list of data on Page, which provides getNumber(), getSize(), getNumberOfElements(), getTotalPages(), getTotalElements, etc.

And consider exploring PageRequest and PagedResources as well.

Shekhar Rai
  • 2,008
  • 2
  • 22
  • 25
  • Check this question: https://stackoverflow.com/q/9314078/11733759 – lczapski Jan 22 '20 at 15:07
  • Should I change something in my thymeleaf template? I ask because when I set for example page=100 and limit=40 in my Controllers method,I received only 40 records without being able to go to the next page or sth like that to view more records. What should I do to be able to navigate pages with other records? – spaceoddity11 Jan 22 '20 at 15:18
  • You may consider seeing [hateos](https://www.baeldung.com/rest-api-pagination-in-spring) to navigate pages. Or you can use `getTotalElements()` / `getTotalPages()` as well. And `page` and `limit` you can pass through your *model*(`getAll(Model model)`) – Shekhar Rai Jan 22 '20 at 15:24
  • See [PagedResources](https://docs.spring.io/spring-hateoas/docs/0.25.3.BUILD-SNAPSHOT/api/org/springframework/hateoas/PagedResources.html) as well to use hateoas links.. – Shekhar Rai Jan 22 '20 at 15:31
  • What do you mean by 'And page and limit you can pass through your *model*(getAll(Model model))'? – spaceoddity11 Jan 22 '20 at 16:12
  • @spaceoddity11 my bad you should pass using @RequestParam like `@GetMapping public String getAll(@RequestParam int page, @RequestParam int size, Model model)` and your URL should be like `/articles?limit=40&page=100` – Shekhar Rai Jan 22 '20 at 16:20
  • Ok, but how can I navigate between the pages avoid writing in my browser exact number of page and limit on the page? – spaceoddity11 Jan 22 '20 at 16:29
  • `PagedResources` is what you should do - implementing `hateoas` you get links to navigate next/previous pages. You should put page(1) and size(40) by default and request to your controller - which gives you links as well. try exploring hateos. – Shekhar Rai Jan 22 '20 at 16:37
  • For example, if you click [here- stackoverflow questions](https://stackoverflow.com/questions), and if you scroll down to the end you can see a list of page buttons like 1,2,3......[lastPage] and 15, 30, 50 per page (limit). – Shekhar Rai Jan 22 '20 at 16:38