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

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

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

编写Repo类

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> {
}

编写业务类

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();
    }
}

编写控制器类

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
能看到

[{"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
能看到

[{"id":1,"name":"computer","num":10,"description":"Good"},{"id":2,"name":"Mac","num":8,"description":"Good"}]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值