MongoTemplate分页参数工具类

我们在使用mongodb的java代码的时候,分页获取数据是很常见的代码,但并没有mongo的java的工具类提供出来。这里封装一个比较简单分页工具类供大家参考。

public class MongoPageHelper {

    private static final int FIRST_PAGE_NUM = 1;

    private static final String ID = "_id";

    private final MongoTemplate mongoTemplate;


    public MongoPageHelper(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }

    /**
     * 分页查询,直接返回集合类型的结果
     */
    public  PageResult pageQuery(Query query, Class entityClass, Integer pageSize, Integer pageNum,String collectionName) {
        return pageQuery(query, entityClass, pageSize, pageNum, Function.identity(), null,collectionName);

    }

    /**
     * 分页查询
     * @param query  mongo Query 对象,构造你自己的查询条件
     * @param entityClass  mongo collection 定义的entity class ,确定集合
     * @param pageSize  每页大小
     * @param pageNum  当前页
     * @param mapper   映射器 collection 定义的entityClass,如要转化为另一个对象
     * @param lastId  上一页的主键id
     * @param    mongo映射实体类
     * @param    响应实体类
     * @return     分页参数
     */
    public  PageResult pageQuery(Query query, Class entityClass, Integer pageSize,
                                           Integer pageNum, Function mapper, String lastId,String collectionName) {
        //分页逻辑
        long total = mongoTemplate.count(query, entityClass, collectionName);
        final Integer pages = (int) Math.ceil(total / (double) pageSize);
        if (pageNum <= 0) {
            pageNum = FIRST_PAGE_NUM;
        }

        if (!StringUtils.isEmpty(lastId)) {
            Criteria criteria = new Criteria();
            if (pageNum != FIRST_PAGE_NUM) {
                criteria.and(ID).gt(new ObjectId(lastId));
            }
            query.addCriteria(criteria);
            query.limit(pageSize);
        }else {
            int skip = pageSize * (pageNum - 1);
            query.skip(skip).limit(pageSize);
        }
        List entityList = mongoTemplate.find(query, entityClass, collectionName);

       final PageResult pageResult = new PageResult<>();

        pageResult.setTotal(total);
        pageResult.setPages(pages);
        pageResult.setPageSize(pageSize);
        pageResult.setPageNum(pageNum);
        pageResult.setList(entityList.stream().map(mapper).collect(Collectors.toList()));
        return pageResult;
    }

}

下面演示工具类的使用

1、先查看一下原数据
MongoTemplate分页参数工具类_第1张图片

2、代码分页工具类演示

 @Autowired
    private MongoTemplate mongoTemplate;

@GetMapping("/testMongoPage")
    public String testMongoPage() {

        MongoPageHelper mongoPageHelper = new MongoPageHelper(mongoTemplate);
        Query query = new Query();
        Sort sort = Sort.by(Sort.Direction.DESC, "valueInt");
        query.with(sort);
        Criteria criteria = new Criteria();
        criteria.and("valueInt").lt(14);
        query.addCriteria(criteria);
        PageResult pageResult = mongoPageHelper.pageQuery(query, MongoTestEntity.class,
                3, 1, "mongoTest");



        PageResult pageResult2 = mongoPageHelper.pageQuery(query, MongoTestEntity.class,
                3, 2, "mongoTest");

        PageResult pageResult3 = mongoPageHelper.pageQuery(query, MongoTestEntity.class,
                3, 2,MongoTestEntity::getValueStr , null, "mongoTest");

        System.out.println(pageResult);
        System.out.println("------------------------------");
        System.out.println(pageResult2);
        System.out.println("===============================");
        System.out.println(pageResult3);
        return “success”;
    }

3、查看结果
在这里插入图片描述

你可能感兴趣的:(mongo)