随笔 - 323  文章 - 0  评论 - 3  阅读 - 17万

Spring Data JPA : 查-分页排序

1.分页查询 

pageNumber是从0开始, pageNumber=0,pageSize=3 就是获取前3条 参考创建分页Pageable变量

创建Pageable对象,再查询

复制代码
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class FruitServiceImpl implements FruitService {

    @Autowired
    private FruitRepository fruitRepository;

    // 分页获取
    @Override
    public List<Fruit> findAllPageable(Integer pageNumber, Integer pageSize) {

        Pageable pageable = PageRequest.of(pageNumber, pageSize); // 创建分页对象

        Page<Fruit> fruits = fruitRepository.findAll(pageable);
        Long total = fruits.getTotalElements(); // 符合条件的总记录条数
        List<Fruit> fruitList = fruits.getContent();// 这一页的所有记录
        return fruitList;
    }
}
复制代码

2.先排序 再分页 查询 : 创建Sort对象,再用Sort对象创建 Pageable对象,再查询 参考Spring Data JPA 多属性排序

  •  根据一个字段排序   
  •  根据多个字段排序,排序方式一样 
  •  根据多个字段排序,排序方式不一样
复制代码
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Service;

@Service
public class FruitServiceImpl implements FruitService {

    @Autowired
    private FruitRepository fruitRepository;

    @Override
    public List<Fruit> findAllPageableBySort(Integer pageNumber, Integer pageSize) {

        Sort sort = Sort.by(Direction.DESC, "name");// 排序方式  根据name降序排列
        Pageable pageable = PageRequest.of(pageNumber, pageSize, sort);// 分页对象
    
        Page<Fruit> fruits = fruitRepository.findAll(pageable);// 查询

        Long total = fruits.getTotalElements(); // 符合条件的总记录条数
        List<Fruit> fruitList = fruits.getContent();// 这一页的所有记录

        return fruitList;
    }

    // 根据多个条件先排序 再分页获取
    @Override
    public List<Fruit> findAllPageableByMultiSort(Integer pageNumber, Integer pageSize) {

        // 根据多个条件排序 先根据name降序排列,再根据color的升序排列
        List<Order> orders = new ArrayList<Sort.Order>();
        orders.add(new Order(Direction.DESC, "name"));
        orders.add(new Order(Direction.ASC, "color"));
        Sort sort = Sort.by(orders);
        Pageable pageable = PageRequest.of(pageNumber, pageSize, sort);// 分页对象

        Page<Fruit> fruits = fruitRepository.findAll(pageable);// 查询
        
        Long total = fruits.getTotalElements(); // 符合条件的总记录条数
        List<Fruit> fruitList = fruits.getContent();// 这一页的所有记录

        return fruitList;
    }
}
复制代码

 

posted on   dreamstar  阅读(170)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示