SpringBoot中JPA实现Sort排序的三种方式小结
作者:bladestone 时间:2022-02-12 23:35:12
引言: 在Spring Boot应用中,基于数据某个字段进行排序是一个非常常用的需求,这里将给出Sort的三种常用用法,基于分页的应用,大家可以各取所需,择机使用。
环境说明
Spring 4.2 Spring Boot 1.5.11 Java 8
前置说明
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;
}
这里的seqNum是排序字段,基于升序来排序。
Repository的定义:
@Repository
public interface EBusinessCardRepository extends JpaRepository<ECardEntity, Long> {
///方法的定义
}
方法1: 基于特殊参数的排序
建立分页对象:
Pageable pageable = new PageRequest(pageNum, size);
在Repository中定义相应的方法:
Page<ECardEntity> findByOrderBySeqNumAsc(Pageable pageable);
这里使用默认的字段拼接形成的方法名,从而自动解析形成对应的方法。
方法2: 基于自定义的@Query进行排序
Pageable的对象定义与方法1中相同。
在Repository中定义相应的JPL语句:
@Query("select e from ECardEntity e ORDER BY e.seqNum ASC")
Page<ECardEntity> findInOrders(Pageable pageable);
方法3: 基于Pageable中的Sort字段
Pageable对象的声明:
Sort sort = new Sort(Direction.ASC, "seqNum");
Pageable pageable = new PageRequest(pageNum, size, sort);
这里将Sort字段作为构造方法的入口参数,创建了Pageable对象。
在Repository无需声明任何新的方法,直接使用JpaRepository中继承而来的findAll(Pageable pageable)方法即可。
在Service中调用具体Repository中的方法如下:
Page<ECardEntity> eCardEntities = this.eCardRepo.findAll(pageable);
来源:https://blade.blog.csdn.net/article/details/80355899
标签:SpringBoot,JPA,Sort排序
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
实例解析使用Java实现基本的音频播放器的编写要点
2023-11-29 14:54:26
Jaxb2实现JavaBean与xml互转的方法详解
2023-11-25 11:51:38
java后台接收app上传的图片的示例代码
2022-11-03 00:04:15
Java如何跳过https的ssl证书验证详解
2023-08-24 11:34:56
![](https://img.aspxhome.com/file/2023/4/58094_0s.jpg)
java 工厂模式的实例详解
2023-11-28 08:48:57
Flutter路由传递参数及解析实现
2023-06-22 11:48:45
![](https://img.aspxhome.com/file/2023/5/90155_0s.gif)
Thymeleaf 3.0 自定义标签方言属性的实例讲解
2023-03-24 20:40:23
详解java构建者模式Builder
2021-11-16 18:49:32
java实现文件上传下载
2023-11-23 09:41:18
使用C++ Matlab中的lp2lp函数教程详解
2023-07-13 17:13:12
![](https://img.aspxhome.com/file/2023/0/103080_0s.png)
剖析Java中阻塞队列的实现原理及应用场景
2023-09-01 17:33:07
SpringBoot异常处理器的使用与添加员工功能实现流程介绍
2021-10-21 19:24:23
![](https://img.aspxhome.com/file/2023/8/60908_0s.png)
零基础写Java知乎爬虫之获取知乎编辑推荐内容
2023-11-29 04:33:39
![](https://img.aspxhome.com/file/2023/3/60783_0s.png)
Proxy实现AOP切面编程案例
2023-07-23 06:44:52
![](https://img.aspxhome.com/file/2023/6/61086_0s.jpg)
带你走进Maven的大门-最全Maven配置及集成idea工具总结
2022-12-06 08:41:40
![](https://img.aspxhome.com/file/2023/0/60680_0s.png)
详解Spring依赖注入的三种方式使用及优缺点
2023-06-09 18:29:00
![](https://img.aspxhome.com/file/2023/6/61046_0s.png)
java 中如何实现 List 集合去重
2023-10-07 07:54:06
![](https://img.aspxhome.com/file/2023/9/58549_0s.webp)
HashMap原理及put方法与get方法的调用过程
2023-10-06 03:53:02
![](https://img.aspxhome.com/file/2023/1/58601_0s.png)
详解MyBatis配置typeAliases的方法
2023-11-29 06:21:52
OpenCV实现人脸识别简单程序
2023-07-07 00:31:12