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"}]
相关推荐
Victor3561 天前
https://editor.csdn.net/md/?articleId=139321571&spm=1011.2415.3001.9698
后端
Victor3561 天前
Hibernate(89)如何在压力测试中使用Hibernate?
后端
灰子学技术1 天前
go response.Body.close()导致连接异常处理
开发语言·后端·golang
风流倜傥唐伯虎1 天前
Spring Boot Jar包生产级启停脚本
java·运维·spring boot
Gogo8161 天前
BigInt 与 Number 的爱恨情仇,为何大佬都劝你“能用 Number 就别用 BigInt”?
后端
fuquxiaoguang1 天前
深入浅出:使用MDC构建SpringBoot全链路请求追踪系统
java·spring boot·后端·调用链分析
毕设源码_廖学姐1 天前
计算机毕业设计springboot招聘系统网站 基于SpringBoot的在线人才对接平台 SpringBoot驱动的智能求职与招聘服务网
spring boot·后端·课程设计
顾北121 天前
MCP服务端开发:图片搜索助力旅游计划
java·spring boot·dubbo
昀贝1 天前
IDEA启动SpringBoot项目时报错:命令行过长
java·spring boot·intellij-idea
野犬寒鸦1 天前
从零起步学习并发编程 || 第六章:ReentrantLock与synchronized 的辨析及运用
java·服务器·数据库·后端·学习·算法