SpringBoot用JPA接口实现分页和排序

一些业务中,需要通过分页实现分批返回数据,或者需要对数据进行排序操作,可以使用Jpa组件的PagingAndSortingRepository接口实现。

简单业务实现(继续使用上一文章中的案例)

编写Repo类

java 复制代码
package com.test.repo;

import com.test.model.Stock;
import org.springframework.data.repository.PagingAndSortingRepository;

/**
 * @Author: 沐槿
 * @Description: 实现分页接口,通过PagingAndSortingRepository接口,大部分方法接口中已经实现
 * @Date: 2025/12/21 22:15
 * @Version: 1.0
 */
public interface StockPagingAndSortingRepo extends PagingAndSortingRepository<Stock, Integer> {
}

编写业务类

java 复制代码
package com.test.service;

import com.test.model.Stock;
import com.test.repo.StockPagingAndSortingRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 沐槿
 * @Description: TODO
 * @Date: 2025/12/21 22:14
 * @Version: 1.0
 */
@Service
public class StockPageAndSortingService {
    @Autowired
    private StockPagingAndSortingRepo stockPagingAndSortingRepo;
    //实现排序功能
    //Sort(org. springframework. data. domain. Sort. Direction, java. util. List<java. lang. String>)'
    // has private access in 'org. springframework. data. domain. Sort'
    //问题点:
    //springboot2.2.1(含)以上的版本Sort已经不能再实例化了,构造方法已经是私有的了!
    //可以改用Sort.by获得Sort对象
    //
    //解决方案:
    //原处理方式:Sort sort = new Sort(Sort.Direction.DESC, "id");
    //现处理方式:Sort sort = Sort.by(Sort.Direction.DESC, "id");
    public List<Stock> sortByName(){
    		  //Sort.by(Sort.Direction.ASC, "name");设置针对指定字段升序的排列方式。
        Sort sort = Sort.by(Sort.Direction.ASC, "name");
        return (List<Stock>) stockPagingAndSortingRepo.findAll(sort);
    }
    //实现分页功能
    public List<Stock> splitPage(){
    			//PageRequest.of(0, 2);设置起始位置,和返回的数据数
    			//PageRequest.of(0, 2,Sort);表示按照Sort方式排序后,再返回前两条数据
    			//从第0条开始,展示2条数据
        PageRequest pageRequest =PageRequest.of(0, 2);
        //返回Page<Stock>对象类型
        Page<Stock> list = stockPagingAndSortingRepo.findAll(pageRequest);
        //我们需要List数据,需要通过list.getContent()方法把数据类型转换。
        return (List<Stock>)list.getContent();
    }
}

编写控制器类

java 复制代码
package com.test.controller;


import com.test.model.Stock;
import com.test.service.StockPageAndSortingService;
import com.test.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author:     沐槿
 * @Description:  TODO  
 * @Date:    2025/12/15 14:37
 * @Version:    1.0
 */
@RestController
public class Controller {

    @Autowired
    private StockPageAndSortingService stockPageAndSortingService;

    @RequestMapping("/sortByName")
    public List<Stock> sortByName(){
        return stockPageAndSortingService.sortByName();
    }

    @RequestMapping("/splitPage")
    public List<Stock> splitPage(){
        return stockPageAndSortingService.splitPage();
    }
}

重启SpringBoot项目,在浏览器中输入地址

http://localhost:8080/sortByName

能看到

javascript 复制代码
[{"id":3,"name":"Book","num":15,"description":"Nice"},{"id":1,"name":"computer","num":10,"description":"Good"},{"id":2,"name":"Mac","num":8,"description":"Good"}]

在浏览器中输入地址

http://localhost:8080/splitPage

能看到

javascript 复制代码
[{"id":1,"name":"computer","num":10,"description":"Good"},{"id":2,"name":"Mac","num":8,"description":"Good"}]
相关推荐
想摆烂的不会研究的研究生29 分钟前
每日八股——Redis(1)
数据库·经验分享·redis·后端·缓存
唐宋元明清218831 分钟前
.NET 磁盘管理-技术方案选型
windows·c#·存储
毕设源码-郭学长1 小时前
【开题答辩全过程】以 基于SpringBoot技术的美妆销售系统为例,包含答辩的问题和答案
java·spring boot·后端
N***H4861 小时前
springcloud springboot nacos版本对应
spring boot·spring·spring cloud
桌面运维家1 小时前
vDisk VOI/IDV Windows本地运行异常怎么办?快速排查恢复
windows
追逐时光者1 小时前
精选 10 款 .NET 开源免费、功能强大的 Windows 效率软件
后端·.net
追逐时光者2 小时前
一款开源、免费的 WPF 自定义控件集
后端·.net
S***q3772 小时前
Spring Boot管理用户数据
java·spring boot·后端
BD_Marathon2 小时前
SpringBoot——辅助功能之切换web服务器
服务器·前端·spring boot
毕设源码-郭学长2 小时前
【开题答辩全过程】以 基于SpringBoot框架的民俗文化交流与交易平台的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端