Spring Data JPA data using tabs and Sorting

file

I. Introduction

If the disposable loading thousands of list data, displayed on the pages of the very time-consuming, the user experience is not good. So when dealing with large data query results show, paging query is essential. Paging query must be accompanied by certain ordering rules, otherwise the state paged data is difficult to control, causing the user may see the same data in different pages. So, the main content of this article is to tell you about how to use Spring Data JPA paging and sorting.

Second, the entity definitions

We use a simple entity definition: Article (Article)

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Entity
@Table(name="article")
public class Article {

    @Id
    @GeneratedValue
    private Long id;

    @Column(nullable = false,length = 32)
    private String author;

    @Column(nullable = false, unique = true,length = 32)
    private String title;

    @Column(length = 512)
    private String content;

    private Date createTime;
}
  • This class represents @Entity is an entity class, JPA receiving control and management, a correspondence table in the database
  • @Table specify the class name correspondence table in the database. If the class name and the name of the database table in line with the hump and underline the rules, you can omit this annotation. The class name correspondence table FlowType flow_type.
  • @Id specify this table's primary key field
  • @GeneratedValue (strategy = GenerationType.IDENTITY) specifies the primary key generation method, typically increment primary key, then it is employed in the generation mode GenerationType.IDENTITY
  • @Column annotation for a field corresponding to a table. nullable = false indicates that the database can not be empty, unique = true indicates that the database can not be duplicate field value, length = 32 represents the maximum degree of database field 32.
  • @ Data, @ AllArgsConstructor, @ NoArgsConstructor, @ Builder is a plug-in annotation lombok, used to help us generate the template code entity class set, get method, constructor, and so on.

Three, Repository definitions

Define an interface ArticleRepository inheritance PagingAndSortingRepository. PagingAndSortingRepository Interface includes not only the basic functions of CURD, also supports sorting, paging function definition interface.

public interface ArticleRepository extends PagingAndSortingRepository<Article,Long> {
     //查询article表的所有数据,传入Pageable分页参数,不需要自己写SQL
    Page<Article> findAll(Pageable pageable);
    //根据author字段查询article表数据,传入Pageable分页参数,不需要自己写SQL
    Page<Article> findByAuthor(String author, Pageable pageable);
    //根据author字段和title字段,查询article表数据,传入Pageable分页参数,不需要自己写SQL
    Slice<Article> findByAuthorAndTitle(String author, String title, Pageable pageable);
}

Fourth, implement paging

 Pageable Spring is the interface definition, passing for paging parameters, we look at how to use it. First ArticleRepository injected into the class that you need to operate persistence layer, typically a @Service annotated classes, then use the following code within the service method of paging operations: Query the first page (0-based) data per 10 data.

Pageable pageable = PageRequest.of(0, 10);   //第一页
//Pageable pageable = PageRequest.of(0, 10);  //第二页
//Pageable pageable = PageRequest.of(0, 10);  // 第三页
//数据库操作获取查询结果
Page<Article> articlePage = articleRepository.findAll(pageable);
//将查询结果转换为List
List<Article> articleList = articlePage.getContent();

findAll method to target Page class response, if we want to get results List, you can use getContent () method. But I do not recommend this conversion, because the front page shows a list of not only data, but also need some information pages. For example: The first few pages, each page how many, how many total pages, a total of how many. The information Page (articlePage) inside the object can be obtained (hereinafter described there).

Fifth, implement sorting

Spring Data JPA provides an  Sortobject is to provide a sorting mechanism. Let's look at sort of way.


articleRepository.findAll(Sort.by("createTime"));

articleRepository.findAll(Sort.by("author").ascending()
                        .and(Sort.by("createTime").descending()));
  • The first method is a findAll createTime is sorted in ascending
  • The first method is a findAll author in ascending order, and then sorted in descending order of createTime

Paging and sorting together

Pageable pageable = PageRequest.of(0, 10,Sort.by("createTime"));

Six, Slice of Page

In ArticleRepository we see a method to return Slice and another method returns Page. They are Spring Data JPA response data interface, the interface is a sub-Page of Slice. They are used to save and return data.

6.1.Slice

Let's look at some of the important methods of Slice.

List <T>  getContent(); //获取切片的内容

Pageable  getPageable(); //当前切片的分页信息

boolean  hasContent(); //是否有查询结果?

boolean  isFirst();  //是否是第一个切片

boolean  isLast();  //是否是最后一个切片

Pageable nextPageable(); // 下一个切片的分页信息

Pageable previousPageable(); // 上一个切片的分页信息

6.2.Page

 Page Slice is a sub-interface, the following are some important ways.

//总页数
int getTotalPages();

//总数据条数
long getTotalElements();

So, when using the Slice? When to use Page?
A: It can be seen through the two functions defined in the interface, the Slice is not only concerned about the presence of a slice (tab), not to database calculates the total number of count, total number of pages. It is more suitable for large volume data list of mouse or finger sliding screen operation, do not care about how many pages there are in total, only concerned there is no next page. Page more suitable for the development of traditional applications in the table, need to know the total number of pages and the total number of pieces.

Look forward to your attention

Guess you like

Origin www.cnblogs.com/zimug/p/11809827.html