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