spring中的分页查询

一.Spring中的分页对象

1.@PageableDefault

可放置在参数中,为Pageable对象指定默认值和排序方式

@GetMapping("/")
public String index(
    // size: 每一页显示的数据数量
    // sort: 排序的依据
    // direction: 排序的方式
    @PageableDefault(size = 5,sort = {"updateTime"},direction = Sort.Direction.DESC) 		Pageable pageable,
    Model model
    ){
    	// 返回前端的分页对象Page
        Page<Blog> listBlog = blogService.listBlog(pageable);

        toIndex(model,listBlog);

        return "index";
    }

2.Pageable接口

多用来和SpringDataJPA结合,进行分页查询,返回结果就是一个Page分页对象

前端根据搜索条件进行分页查询,后端用一个对象来封装前端的查询条件,进行分页条件查询

封装的前端查询条件对象

// 博客查询的封装对象
public class BlogQuery {
	
    private String title;  // 根据博客标题查询
    private Long typeId;  // 根据博客的分类id查询
    private boolean recommend;  // 根据是否推荐查询
    // ......
}

controller层

@PostMapping("/blogs/search")
public String search(
    // 指定默认的分页方式
    @PageableDefault(size = 10,sort = {"updateTime"},direction = Sort.Direction.DESC) 		Pageable pageable,
    BlogQuery blog,  // 前端的查询条件对象
    Model model
    ){
        model.addAttribute("page",blogService.listBlog(pageable, blog));
        // 定义一个片段,实现局部的渲染
        return "admin/blogs :: blogList";
    }

service层

@Override
public Page<Blog> listBlog(Pageable pageable, BlogQuery blog) {

    Specification<Blog> blogSpecification = new Specification<Blog>() {
        @Override
        public Predicate toPredicate(Root root, CriteriaQuery cq, CriteriaBuilder cb) {

            ArrayList<Predicate> predicateList = new ArrayList<>();
            // 判断是否有标题的条件传输过来(标题不等于空或者null的时候才会拼接条件)
            if (!"".equals(blog.getTitle()) && blog.getTitle() != null ){
                predicateList.add(cb.like(root.<String>get("title"), "%" +                               blog.getTitle() + "%"));
            }

            // 根据Blog对象中的Type对象的id值进行查询
            // 因为id是Long类型的,所以不存在空格字符串的情况
            if (blog.getTypeId() != null){
                predicateList.add(cb.equal(root<Type>get("type").get("id"),
                blog.getTypeId()));
            }

            // 判断是否是推荐的文章,是true的情况就进行查询
            if (blog.isRecommend()){
                predicateList.add(cb.equal(root.<Boolean>get("recommend"),
                blog.isRecommend()));
            }

            // 把条件列表转换成数组
            Predicate[] predicates = predicateList.toArray(
                new Predicate[predicateList.size()]
            );
            // 通过条件数据进行查询
            cq.where(predicates);

            return null;
        }
    };
	
    // 进行分页条件查询
    Page<Blog> all = repository.findAll(blogSpecification, pageable);

    return all;
}

前端使用Page分页对象获取数据分页数据

// 获取博客的总的条数
<h2 class="ui orange header" th:text="${page.totalElements}">14</h2>

// 获取分页对象中的每一个blog对象
<div class="ui padded vertical segment" th:each="blog : ${page.content}">

// 如果页数大于1页的话,就进行显示
<div class="ui bottom attached segment" th:if="${page.totalPages}>1">    

// 点击上一页的时候当前页数-1     只有存在上一页的情况下,上一页的按钮才会显示
<a href="#" th:href="@{/(page=${page.number}-1)}" th:unless="${page.first}">上一页</a>

// 点击下一页的时候当前页数+1     只有存在下一页的情况下,下一页的按钮才会显示
<a href="#" th:href="@{/(page=${page.number}+1)}" th:unless="${page.last}">下一页</a>

3.new PageImpl<>构造分页对象

PageImpl extends Chunk implements Page

PageImpl 是 Page接口的一个实现类

/*
	参数一: 通过分页查询查询到的对象
	参数二: 分页对象
	参数三: 对象(不是分页对象)的总的数量
*/
new PageImpl<>(arrayList, pageable, count);

示例

private Page<CommentAndBlog> returnResult (Page<Comment> commentList,Pageable pageable){
    ArrayList<CommentAndBlog> arrayList = new ArrayList<>();
    for (Comment comment : commentList) {
        CommentAndBlog commentAndBlog = new CommentAndBlog();
        commentAndBlog.setNickname(comment.getNickname());
        commentAndBlog.setContent(comment.getContent());
        commentAndBlog.setCreateTime(comment.getCreateTime());
        // 封装评论的id(为删除或者编辑id的时候使用)
        commentAndBlog.setId(comment.getId());

        // 封装评论所对应的文章
        Long blogId = comment.getBlog().getId();
        // 根据文章id查找文章的标题
        String blogTitle = blogService.getBlog(blogId).getTitle();
        commentAndBlog.setTitle(blogTitle);

        // 把构造好的对象添加到集合中
        arrayList.add(commentAndBlog);
    }
	
    // 获取查询对象的总的数量(非分页查询对象)
    long count = commentRepository.count();

    // 通过PageImpl<> 构造分页对象
    /*
         * 参数一: 查询到的对象
         * 参数二: 分页对象
         * 参数三: 数据的总的数量
         * */
    Page<CommentAndBlog> commentAndBlogs = new PageImpl<>(arrayList, pageable, count);
    return commentAndBlogs;
}

4.PageRequest.of()构造分页查询对象

参数一: 从第几页开始查询,当前为从第一页开始查询(写作0)

参数二: 每页显示的条数

参数三: 排序方式

PageRequest.of(0, size, sort);

@Override
public List<Blog> listRecommendBlogTop(Integer size) {
	// 构造排序对象
    Sort sort = new Sort(Sort.Direction.DESC, "updateTime");
    Pageable pageable = PageRequest.of(0, size, sort);
	
    // 根据分页对象,进行分页查询
    return repository.findTop(pageable);
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Spring Cloud分页查询,你可以使用Spring Data JPA来简化操作。下面是一个简单的示例: 1. 首先,在你的实体类添加分页查询所需的注解和属性。例如: ```java @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private int age; // 其他属性... // 省略构造函数、getter和setter方法 } ``` 2. 创建一个Repository接口,继承自`JpaRepository`或其子接口。例如: ```java public interface UserRepository extends JpaRepository<User, Long> { Page<User> findAll(Pageable pageable); } ``` 3. 在你的Service或Controller使用`Pageable`进行分页查询。例如: ```java @Service public class UserService { @Autowired private UserRepository userRepository; public Page<User> getUsers(int page, int size) { Pageable pageable = PageRequest.of(page, size); return userRepository.findAll(pageable); } } ``` 4. 最后,在Controller调用Service方法并返回分页结果。例如: ```java @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/users") public Page<User> getUsers(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) { return userService.getUsers(page, size); } } ``` 以上示例演示了如何在Spring Cloud实现分页查询,当你访问`/users`接口时,会返回指定页码和每页数量的用户数据。你可以根据自己的需求进行进一步的定制和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值