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"}]
相关推荐
一鹿高歌几秒前
🔥内存炸了!背刺我的竟然是Redisson!!
后端
lizhongxuan1 分钟前
AI 的底层思考
后端
Penge66622 分钟前
解密 Kafka 与 RocketMQ 消费模型的核心之战
后端
Nyarlathotep011325 分钟前
SpringBoot Starter的用法以及原理
java·spring boot
小码哥_常27 分钟前
Spring Boot遇上Maven依赖冲突:打怪升级全攻略
后端
用户73440281934227 分钟前
Spring Boot 集成 Redis 并调用 Lua 脚本详解
后端
小码哥_常29 分钟前
Spring Boot多模块项目:Parent、BOM、Starter的分工大揭秘
后端
SimonKing1 小时前
GitHub 10万星的OpenCode,正在悄悄改变我们的工作流
java·后端·程序员
Moment1 小时前
OpenClaw 从能聊到能干差的是这 50 个 Skills 😍😍😍
前端·后端·开源
怕浪猫2 小时前
第20章:Web服务实战——构建RESTful API
后端·go·编程语言