Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Sort sorting by JPA in SpringBoot

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to achieve Sort sorting in JPA in SpringBoot. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Environment description

Spring 4.2 Spring Boot 1.5.11 Java 8

Pre-instruction

Definition of ECardEntity.java:

Import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.Table;import com.jd.ai.fasion.util.BaseEntity;import lombok.Data;import lombok.EqualsAndHashCode;@Entity@Table (name= "t_ebusiness_card") @ Data@EqualsAndHashCode (callSuper=true) public class ECardEntity extends BaseEntity {private static final long serialVersionUID = 6580526495176090890L; @ Column private String name; @ Column (name= "zip_url") private String zipUrl; @ Column (name= "thumb_url") private String thumbUrl @ Column (name= "seq_num") private int seqNum;}

The seqNum here is the sort field, sorted based on ascending order.

Definition of Repository:

@ Repositorypublic interface EBusinessCardRepository extends JpaRepository {/ definition of method} method 1: sort based on special parameters

Create a paging object:

Pageable pageable = new PageRequest (pageNum, size)

Define the appropriate method in Repository:

Page findByOrderBySeqNumAsc (Pageable pageable)

The default method name formed by the concatenation of fields is used here, which automatically parses to form the corresponding method.

Method 2: sort based on custom @ Query

The object definition of Pageable is the same as in method 1.

Define the corresponding JPL statement in Repository:

@ Query ("select e from ECardEntity e ORDER BY e.seqNum ASC") Page findInOrders (Pageable pageable); method 3: based on the Sort field in Pageable

Declaration of the Pageable object:

Sort sort = new Sort (Direction.ASC, "seqNum"); Pageable pageable = new PageRequest (pageNum, size, sort)

Here, the Pageable object is created by using the Sort field as the entry parameter of the constructor.

There is no need to declare any new methods in Repository, just use the findAll (Pageable pageable) method inherited from JpaRepository.

Call the method in the specific Repository in Service as follows:

Page eCardEntities = this.eCardRepo.findAll (pageable); this is the end of the article on "how to achieve Sort sorting in SpringBoot". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report