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   五官一体即忢  阅读(86)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
< 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

导航

统计

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