springBoot + easyui + spring data JPA implement paging

  After countless attempts, I finally realized the function of paging, and experienced the pain of being a programmer. 
It is easy to implement paging at the easyui front desk! ! The packaging is great. . Here only introduce the big pit encountered in the background! !

Spring data JPA has perfect support for paging and sorting queries. When implementing paging, you need to inherit PagingAndSortingRepository.

A method is defined in XxxRepository.java. For example:
Page<Department> findById(Integer id, Pageable pageable);
  Pageableis an interface defined in the Spring Data library, the interface is an abstraction of all the paging related information. Through this interface, we can get all the information related to paging (such as pageNumber, pageSize, etc.), so that Jpa can get a Sql statement with paging information through the pageable parameter.
  PageIt is also an interface provided by Spring Data. This interface represents a part of the data collection and its related next part data, total data and other related information. Through this interface, we can get the overall information of the data (total data, total pages ...) and Information about current data (collection of current data, current number of pages, etc.)

   Spring Data Jpa will help us to extend the Sql statement through naming conventions, it will also help us deal with parameters of type Pageable, convert pageable parameters into conditions in SQL statements, and at the same time, it will also help us deal with the return value of type Page When the return value type is found to be Page, Spring Data Jpa will put the overall information of the data, the information of the current data, and the information of paging into the return value. In this way, we can conveniently carry out personalized paging query.
The method in service:
public Page<T> findAllT(Pageable pageable){
    return this.xxxRepository.findAll(pageable);
}
public  Page<T> findById(Integer id,Pageable pageable){
    return this.xxxRepository.findById(id,pageable);
}```
#####controller中的方法(用的springBoot技术,controller上有@RestController注解):

// All pages display
@RequestMapping (value = "/ displayAll", method = RequestMethod.GET)
public Map

The problem:

1、

Error message

The error shown is because there is no length and the returned model is not compatible with the required model.
2. After
  the method is written in the background (the code at this time is: map.put ("rows", list);), the foreground is not displayed. Run the corresponding URL alone to get a partial screenshot of the JSON data as follows:

Partial screenshot
As shown in the figure above, it can be seen that the paging method in the background is correct, and why can't you get JSON data?

The reason is that the model that returns JSON data does not match the model required by easyui datagrid (datagrid requires an array of models, or an object containing rows and total). See below

The data model required by easyui paging
The model of the JSON data we obtained is as follows:

 {
"total":15,
"rows":{
"content":[
{"id":1,"Name":"XXXXX","school":{"id":1,"schoolName":"XXXX","departments":[{"id":1,"departmentName":"XXXXX","school":1},","school":3},{"id":15,"departmentName":"jjjj","school":3}]}},{"id":9,"departmentName":"xxx","school":1},{"id":10,"departmentName":"xxx","school":2},{"id":13,"departmentName":"xxx","":1}
],
"last":false,
"totalPages":2,
"totalElements":15,
"number":0,
"size":10,
"sort":null,
"first":true,
"numberOfElements":10
}
}

  The rows we get are not an array ([]), it is an object ({}), and its content property is the model we need. So modify map.put ("rows", list.getContent ()) in the controller; finally displayed successfully.
If there is something wrong, please point it out! ! !

Published 10 original articles · Like 16 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/xl132598798/article/details/61614634