java的mongodb分页封装

package com.sxsoft.utils;

import com.sxsoft.utils.PageHelper;
import lombok.Data;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.regex.Pattern;

/**
 * @program: sxsoft_operation_admin
 * @ClassName MongoUtil
 * @description:mogodb分页封装
 * @author: 黄涛
 * @create: 2023-02-03 17:13
 * @Version 1.0
 **/
@Data
@Component
public class MongoUtil<T> {
    public Integer pageSize;
    public Integer currentPage;


    public void start( Integer currentPage, Integer pageSize, Query query) {
        pageSize = pageSize == 0 ? 10 : pageSize;
        query.limit(pageSize);
        query.skip((currentPage - 1) * pageSize);
        this.pageSize = pageSize;
        this.currentPage = currentPage;
    }

    public PageHelper pageHelper(long total, List<T> list) {
        return new PageHelper(this.currentPage, total, this.pageSize, list);
    }

    public PageHelper pageHelper(List<T> list) {
        return new PageHelper(this.currentPage, this.pageSize, list);
    }

    public PageHelper pageHelper(long currentPage, long total, long pageSize, List<T> list) {
        return new PageHelper(currentPage, total, pageSize, list);
    }

    public PageHelper pageHelper(long currentPage, long pageSize, List<T> list) {
        return new PageHelper(currentPage, pageSize, list);
    }


    /**
     * 用于模糊查询忽略大小写
     *
     * @param string
     * @return
     */
    public Pattern getPattern(String string) {
        Pattern pattern = Pattern.compile("^.*" + string + ".*$", Pattern.CASE_INSENSITIVE);
        return pattern;
    }
}
package com.sxsoft.utils;

/**
 * @program: sxsoft_operation_admin
 * @ClassName PageResult
 * @description:分页封装
 * @author: 黄涛
 * @create: 2023-02-02 16:40
 * @Version 1.0
 **/

import lombok.Data;

import java.util.List;

@Data
public class PageHelper<T> {
    private long currentPage;
    private long total;
    private long pageSize;
    private long totalRecord;
    private long countpage;
    private List<T> list;


    public PageHelper(long pageNum, long total, long pageSize, List<T> list) {
        this.currentPage = pageNum;
        this.total = total;
        this.totalRecord = list.size();
        this.pageSize = pageSize;
        this.list = list;

        // 计算总页数
        if (this.totalRecord % this.pageSize == 0) {
            this.countpage = this.totalRecord / this.pageSize;
        } else {
            this.countpage = this.totalRecord / this.pageSize + 1;
        }
    }

    public PageHelper(long pageNum, long pageSize, List<T> list) {
        this.currentPage = pageNum;
        this.pageSize = pageSize;
        this.list = list;
    }

    public PageHelper() {

    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }
}
 //计算总数,必须在分页前面
            long total = mongoTemplate.count(query, BidderTrade.class);
            mongoUtil.start(pageIndex,pageSize,query);
 List<BidderTrade> projectList = mongoTemplate.find(query, BidderTrade.class);
 PageHelper<BidderTrade> pageList =  mongoUtil.pageHelper(total, projectList);

 

posted on 2023-03-10 08:41  五官一体即忢  阅读(82)  评论(0编辑  收藏  举报

导航