一、简介
SpringData MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的find的 *** 作,分页查询是返回到匹配文档的游标,可以随意修改查询限制、跳跃、和排序顺序的功能。
我们在查询时find()方法接受Query类型有:
(1)org.springframework.data.mongodb.core.query
(2)org.springframework.data.mongodb.core.query.BasicQuery
Query类提供方法有limit、skip、sort查询限制、跳跃、和排序顺序的功能,BasicQuery继承了Query类。
二、基本分页
Query类提供方法有limit、skip、sort查询限制、跳跃、和排序顺序的功能,我们实现Query查询分页。
第一步:实现分页工具类
package com.qt.robot.mongoEntity; import java.util.List; public class Page{ // 结果集 private List datas; // 查询记录数 private int rowCount; // 每页多少条数据 private int pageSize = 10; // 第几页 private int pageNo = 1; // 跳过几条数 private int skip = 0; public int getTotalPages() { return (rowCount + pageSize - 1) / pageSize; } public List getDatas() { return datas; } public void setDatas(List datas) { this.datas = datas; } public int getRowCount() { return rowCount; } public void setRowCount(int rowCount) { this.rowCount = rowCount; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getSkip() { skip = (pageNo - 1) * pageSize; return skip; } public void setSkip(int skip) { this.skip = skip; } public int getPageNo() { return pageNo; } public void setPageNo(int pageNo) { this.pageNo = pageNo; } }
第二步:实现分页
public PagegetVoiceInteractionStatistics(int pageNo, int pageSize, String robotId, String robotTypeId) { Page page = new Page (); page.setPageNo(pageNo); page.setPageSize(pageSize); Query query = new Query(); Criteria criteria = new Criteria(); int count = 0; // 查询总数 // 只分页查询 if (StringUtils.isBlank(robotId) && StringUtils.isBlank(robotTypeId)) { count = (int) mongoTemplate.count(query, collectionName); page.setRowCount(count); } // 根据robotId(机器人id)查询 if (StringUtils.isNoneBlank(robotId)) { criteria = Criteria.where("robotId").is(robotId); query = new Query(criteria); count = (int) mongoTemplate.count(query, collectionName); page.setRowCount(count); } // 根据robotTypeId(机器人设备id)查询 if (StringUtils.isNoneBlank(robotTypeId)) { criteria = Criteria.where("robotTypeId").is(robotTypeId); query = new Query(criteria); count = (int) mongoTemplate.count(query, collectionName); page.setRowCount(count); } // 根据robotId、robotTypeId查询 if (StringUtils.isNoneBlank(robotId) && StringUtils.isNoneBlank(robotTypeId)) { criteria = Criteria.where("robotTypeId").is(robotTypeId).and("robotId").is(robotId); query = new Query(criteria); count = (int) mongoTemplate.count(query, collectionName); page.setRowCount(count); } query.skip(page.getSkip()).limit(page.getPageSize()); List datas = mongoTemplate.find(query, VoiceInteractionStatisticsEntity.class, collectionName); page.setDatas(datas); return page; }
第三步:应用查询
public ListdetailStatistics(int pageNo, int pageSize, String robotId, String robotTypeId) { List detailStatisticsList = new ArrayList<>(); Date date = new Date(); date.setHours(0); date.setMinutes(0); date.setSeconds(0); Date date1 = new Date(); Calendar calc = Calendar.getInstance(); calc.setTime(date); calc.add(calc.DATE, -1); Date minDate = calc.getTime(); Criteria today = null; Criteria yesterday = null; Criteria total = null; Query queryByToday = null; Query queryByYesterday = null; Query queryByTotal = null; // 根据条件查询机器人id Page page = getVoiceInteractionStatistics(pageNo, pageSize, robotId, robotTypeId); if (page != null) { List list = page.getDatas(); for (VoiceInteractionStatisticsEntity voiceInteractionStatisticsEntity : list) { // 查询今日消息数 today = Criteria.where("time").gte(date).lt(date1).and("robotId") .is(voiceInteractionStatisticsEntity.getRobotId()); queryByToday = new Query(today); int todayTotal = (int) mongoTemplate.count(queryByToday, collectionName); // 查询昨日消息数 yesterday = Criteria.where("time").gte(minDate).lt(date).and("robotId") .is(voiceInteractionStatisticsEntity.getRobotId()); queryByYesterday = new Query(yesterday); int yesterdayTotal = (int) mongoTemplate.count(queryByYesterday, collectionName); // 查询访问总量 total = Criteria.where("robotId").is(voiceInteractionStatisticsEntity.getRobotId()); queryByTotal = new Query(total); int totals = (int) mongoTemplate.count(queryByTotal, collectionName); String robotId1 = voiceInteractionStatisticsEntity.getRobotId(); String robotTypeId1 = voiceInteractionStatisticsEntity.getRobotTypeId(); String robotTypeName = voiceInteractionStatisticsEntity.getRobotTypeName(); VoiceInteractionStatistics voiceInteractionStatistics = new VoiceInteractionStatistics (robotId1, robotTypeId1, robotTypeName, todayTotal, yesterdayTotal, totals); detailStatisticsList.add(voiceInteractionStatistics); } } return detailStatisticsList; }
python学习网,大量的免费MongoDB入门教程,欢迎在线学习!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)