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"}]
相关推荐
疯狂的程序猴2 小时前
生成加密 IPA 的工具在项目中的使用方式
后端
华仔啊2 小时前
Java 开发必看:什么时候用 for,什么时候用 Stream?
java·后端
程序员岳焱2 小时前
2025 IDEA运行报错:运行 xxxxApplication 时出错。命令行过长。 通过 JAR 清单或通过类路径文件缩短命令行,然后重新运行。
后端·intellij idea
Psycho_MrZhang2 小时前
Flask 设计思想总结
后端·python·flask
Java水解3 小时前
Dubbo跨机房调用实战:从原理到架构的完美解决方案
后端·dubbo
superman超哥3 小时前
仓颉语言中字符串常用方法的深度剖析与工程实践
开发语言·后端·python·c#·仓颉
AskHarries3 小时前
Claude CLI 使用指南(Step by Step)
后端·ai编程
q_19132846954 小时前
基于Springboot+Vue.js的工业人身安全监测系统
vue.js·spring boot·后端·mysql·计算机毕业设计·串口通讯
阿杰AJie4 小时前
安装 docker.io(不走外网 Docker 域名)
后端·docker