Springboot + MongoDB 大容量数据分页及条件查询(千万数据量级别)

69 篇文章 1 订阅
2 篇文章 0 订阅

1. 新建查询类 QueryData

package com.example.basedata.bean;
 
import java.util.HashMap;
import java.util.List;
 
public class QueryData {
 
    private String tableName;
 
    private Integer pageNum;
 
    private Integer pageSize;
 
    private HashMap<String, String> conditions;
 
    public Integer getPageNum() {
        return pageNum;
    }
 
    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }
 
    public Integer getPageSize() {
        return pageSize;
    }
 
    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }
 
    public HashMap<String, String> getConditions() {
        return conditions;
    }
 
    public void setConditions(HashMap<String, String> conditions) {
        this.conditions = conditions;
    }
 
    public String getTableName() {
        return tableName;
    }
 
    public void setTableName(String tableName) {
        this.tableName = tableName;
    }
 
    @Override
    public String toString() {
        return "QueryData{" +
                "tableName='" + tableName + '\'' +
                ", pageNum=" + pageNum +
                ", pageSize=" + pageSize +
                ", conditions=" + conditions +
                '}';
    }
}

2. Control 层

package com.example.basedata.controller;
 
import com.alibaba.fastjson.JSONObject;
import com.example.basedata.bean.*;
import com.example.basedata.service.CommonDataServe;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.util.List;
 
@RestController
@RequestMapping("/data")
@CrossOrigin(value = "*",  maxAge = 3600)
public class CommonDataControl {
 
    @Resource
    private CommonDataServe serve;
 
//  @ApiOperation(value="多条件分页查询", notes="多条件分页查询指定表")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public JSONObject queryData(@RequestBody QueryData queryData) {
        JSONObject output = new JSONObject();
        serve.getData(queryData, output);
        return output;
    }
}

3. Serve 层

package com.example.basedata.service;
 
import com.alibaba.fastjson.JSONObject;
import com.example.basedata.bean.*;
import com.example.basedata.dao.impl.CommonDataImpl;
import com.example.basedata.dao.impl.TableInfoImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
 
@Service
public class CommonDataServe {
 
    @Resource
    private CommonDataImpl commonDataImpl;
 
    public JSONObject getData(QueryData queryData, JSONObject result) {
        String tableName = queryData.getTableName();
        int pageNum = queryData.getPageNum();
        int pageSize = queryData.getPageSize();
        HashMap<String, String> conditions = queryData.getConditions();
 
        List list1 = commonDataImpl.getData(pageNum, pageSize, tableName, conditions);
        result.put("status", 200);
        result.put("msg", "success");
        result.put("data", list1);
        return result;
    }
}

4. CommonDataImpl 类

package com.example.basedata.dao.impl;
 
import com.example.basedata.bean.DeleteFields;
import com.example.basedata.bean.Extract;
import com.mongodb.MongoNamespace;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.RenameCollectionOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;
 
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CountDownLatch;
 
@Repository
public class CommonDataImpl {
 
    @Resource
    private MongoTemplate template;
 
    @Resource
    private AsyncTestImpl asyncTestImpl;
 
    public List getData(int pageNum, int pageSize, String tableName, HashMap<String, String> conditions) {
        //创建查询对象
        Query query = new Query();
        //设置起始数
        query.skip((pageNum - 1) * pageSize);
        //设置查询条数
        query.limit(pageSize);
        if (conditions != null) {
            for (String field: conditions.keySet()) {
                query.addCriteria(new Criteria(field).is(conditions.get(field)));
            }
        }
        return template.find(query, Object.class, tableName);
    }
}

5. 程序测试

测试的目标库表数据量为1000多万

 

 成功返回,且花费时间仅为220ms,接下来加上查询条件

成功返回,且花费时间仅为24ms

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值