
一、简介
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;
/**
* 分页
* @author 作者:yaohongan
* @create 创建时间:2018年10月9日上午9:53:15
*
*/
publicclassPage<T> {
// 结果集
privateList<T> datas;
// 查询记录数
privateint rowCount;
// 每页多少条数据
privateint pageSize = 10;
// 第几页
privateint pageNo = 1;
// 跳过几条数
privateint skip = 0;
/**
* 总页数
* @return
*/
publicint getTotalPages() {
return(rowCount + pageSize - 1) / pageSize;
}
publicList<T> getDatas() {
returndatas;
}
publicvoid setDatas(List<T> datas) {
this.datas = datas;
}
publicint getRowCount() {
returnrowCount;
}
publicvoid setRowCount(int rowCount) {
this.rowCount = rowCount;
}
publicint getPageSize() {
returnpageSize;
}
publicvoid setPageSize(int pageSize) {
this.pageSize = pageSize;
}
publicint getSkip() {
skip = (pageNo - 1) * pageSize;
returnskip;
}
publicvoid setSkip(int skip) {
this.skip = skip;
}
publicint getPageNo() {
returnpageNo;
}
publicvoid setPageNo(int pageNo) {
this.pageNo = pageNo;
}
}
第二步:实现分页
/**
*
* @author 作者:yaohongan
* @create 创建时间:2018年10月9日下午3:17:03
* @desc 分页根据条件查询机器人id
*/
publicPage<VoiceInteractionStatisticsEntity> getVoiceInteractionStatistics(int pageNo, int pageSize,
String robotId, String robotTypeId) {
Page<VoiceInteractionStatisticsEntity> page = newPage<VoiceInteractionStatisticsEntity>();
page.setPageNo(pageNo);
page.setPageSize(pageSize);
Query query = newQuery();
Criteria criteria = newCriteria();
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 = newQuery(criteria);
count= (int) mongoTemplate.count(query, collectionName);
page.setRowCount(count);
}
// 根据robotTypeId(机器人设备id)查询
if(StringUtils.isNoneBlank(robotTypeId)) {
criteria = Criteria.where("robotTypeId").is(robotTypeId);
query = newQuery(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 = newQuery(criteria);
count= (int) mongoTemplate.count(query, collectionName);
page.setRowCount(count);
}
query.skip(page.getSkip()).limit(page.getPageSize());
List<VoiceInteractionStatisticsEntity> datas = mongoTemplate.find(query,
VoiceInteractionStatisticsEntity.class, collectionName);
page.setDatas(datas);
returnpage;
}
第三步:应用查询
/**
*
* @author 作者:yaohongan
* @create 创建时间:2018年10月9日上午10:00:50
* @desc 实时统计语音交互明细
*/
publicList<VoiceInteractionStatistics> detailStatistics(int pageNo, int pageSize, String robotId,
String robotTypeId) {
List<VoiceInteractionStatistics> detailStatisticsList = newArrayList<>();
Datedate= newDate();
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
Datedate1 = newDate();
Calendar calc = Calendar.getInstance();
calc.setTime(date);
calc.add(calc.DATE, -1);
DateminDate = calc.getTime();
Criteria today = null;
Criteria yesterday = null;
Criteria total = null;
Query queryByToday = null;
Query queryByYesterday = null;
Query queryByTotal = null;
// 根据条件查询机器人id
Page<VoiceInteractionStatisticsEntity> page = getVoiceInteractionStatistics(pageNo, pageSize, robotId,
robotTypeId);
if(page != null) {
List<VoiceInteractionStatisticsEntity> list = page.getDatas();
for(VoiceInteractionStatisticsEntity voiceInteractionStatisticsEntity : list) {
// 查询今日消息数
today = Criteria.where("time").gte(date).lt(date1).and("robotId")
.is(voiceInteractionStatisticsEntity.getRobotId());
queryByToday = newQuery(today);
int todayTotal = (int) mongoTemplate.count(queryByToday, collectionName);
// 查询昨日消息数
yesterday = Criteria.where("time").gte(minDate).lt(date).and("robotId")
.is(voiceInteractionStatisticsEntity.getRobotId());
queryByYesterday = newQuery(yesterday);
int yesterdayTotal = (int) mongoTemplate.count(queryByYesterday, collectionName);
// 查询访问总量
total = Criteria.where("robotId").is(voiceInteractionStatisticsEntity.getRobotId());
queryByTotal = newQuery(total);
int totals = (int) mongoTemplate.count(queryByTotal, collectionName);
String robotId1 = voiceInteractionStatisticsEntity.getRobotId();
String robotTypeId1 = voiceInteractionStatisticsEntity.getRobotTypeId();
String robotTypeName = voiceInteractionStatisticsEntity.getRobotTypeName();
VoiceInteractionStatistics voiceInteractionStatistics = newVoiceInteractionStatistics
(robotId1, robotTypeId1, robotTypeName, todayTotal, yesterdayTotal, totals);
detailStatisticsList.add(voiceInteractionStatistics);
}
}
returndetailStatisticsList;
}
网友评论