【代码解析】代码解析之分页查询(3)

public PageBean<MovieComment> findPage(MovieComment movieComment, PageBean<MovieComment> pageBean){
		ExampleMatcher withMatcher = ExampleMatcher.matching().withMatcher("content", GenericPropertyMatchers.contains());
		//withMatcher = withMatcher.withIgnorePaths("isShow");
		Example<MovieComment> example = Example.of(movieComment, withMatcher);
		Sort sort = Sort.by(Direction.DESC, "createTime");
		Pageable pageable = PageRequest.of(pageBean.getCurrentPage()-1, pageBean.getPageSize(),sort);
		Page<MovieComment> findAll = movieCommentDao.findAll(example, pageable);
		pageBean.setContent(findAll.getContent());
		pageBean.setTotal(findAll.getTotalElements());
		pageBean.setTotalPage(findAll.getTotalPages());
		return pageBean;
	}

这是一段关于分页查找电影院评价信息列表的代码

相关代码解析如下:

1.

public PageBean<MovieComment> findPage(MovieComment movieCommentPageBean<MovieComment> pageBean){

这是一个公开方法,返回类型为PageBean<MovieComment>,它接受两个参数:一个MovieComment对象和一个PageBean<MovieComment>对象。

2.
        ExampleMatcher withMatcher = ExampleMatcher.matching().withMatcher("content", GenericPropertyMatchers.contains());

这里创建了一个 ExampleMatcher 对象,用于定义如何匹配MovieComment对象的属性。

具体来说,它设置了内容属性(content)的匹配方式为包含(contains)。

3.
        Example<MovieComment> example = Example.of(movieComment, withMatcher);

使用上面创建的 withMatcher ,根据传入的 movieComment 对象,创建一个Example<MovieComment> 对象。这个对象可以用于查询数据库。

Example.of():这是一个静态方法,用于创建Example对象。

4.
        Sort sort = Sort.by(Direction.DESC, "createTime");

这行代码是在创建一个Sort对象,用于定义数据库查询的排序规则。这里使用了Sort.by()方法,它接受两个参数:一个是排序方向(Direction.DESC),另一个是排序的字段名("createTime")。定义一个排序规则,按照 createTime 字段降序排列。

5.
        Pageable pageable = PageRequest.of(pageBean.getCurrentPage()-1, pageBean.getPageSize(),sort);

根据传入的pageBean ,创建一个分页请求对象,这里减1是因为分页通常是从0开始的,但数据库查询是从1开始的。

  • PageRequest.of():这是一个静态方法,用于创建Pageable对象。
  • pageBean.getPageSize():获取页面大小,即每页要返回的记录数。
  • sort:这是前面创建的排序规则对象。

6.
        Page<MovieComment> findAll = movieCommentDao.findAll(example, pageable);

使用上面创建的examplepageable对象,从movieCommentDao中执行分页查询。

7.
        pageBean.setContent(findAll.getContent());
        pageBean.setTotal(findAll.getTotalElements());
        pageBean.setTotalPage(findAll.getTotalPages());
        return pageBean;
    }

  1. pageBean.setContent(findAll.getContent());:这行代码将查询结果的内容设置到pageBean对象的content属性中。
  2. pageBean.setTotal(findAll.getTotalElements());:这行代码将查询结果的元素总数(即满足条件的记录数)设置到pageBean对象的total属性中。
  3. pageBean.setTotalPage(findAll.getTotalPages());:这行代码将查询结果的总页数设置到pageBean对象的totalPage属性中。

MovieComment 

pageBean 

package com.demo.movie.bean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.stereotype.Component;

/**
 * 分页信息封装类
 * @author Administrator
 *
 */
@Component
public class PageBean<T> {
	
	private int currentPage = 1;//当前页码
	
	private int pageSize = 10;//每页显示数量,默认十条
	
	private long total = 0;//总数量
	
	private int totalPage;//总页数
	
	private List<T> content;
	
	private int showPageSize = 5;//显示在页面可快速跳转的页码个数
	
	private List<Integer> currentShowPage = new ArrayList<Integer>();//当前显示在页面快速跳转的页码

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public List<T> getContent() {
		return content;
	}

	public void setContent(List<T> content) {
		this.content = content;
	}

	public int getShowPageSize() {
		return showPageSize;
	}

	public void setShowPageSize(int showPageSize) {
		this.showPageSize = showPageSize;
	}

	public List<Integer> getCurrentShowPage() {
		//首先当前页往前显示currentShowPage页
		for(int i = currentPage - 1;i > 0; i--){
			currentShowPage.add(i);
			if(i <= (currentPage - showPageSize)){
				break;
			}
		}
		//接下来当前页往后显示currentShowPage页
		for(int i = currentPage;i <= totalPage; i++){
			currentShowPage.add(i);
			if(i >= totalPage){
				break;
			}
			if(i >= (showPageSize + currentPage)){
				break;
			}
		}
		Collections.sort(currentShowPage);
		return currentShowPage;
	}

	public void setCurrentShowPage(List<Integer> currentShowPage) {
		this.currentShowPage = currentShowPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	
	
}

 

  • 16
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值