java关联mongodb结合jessite实现分页
程序员文章站
2022-07-08 10:51:25
...
@RequiresPermissions("message:kafkaMessage:view") @RequestMapping(value = "listData") @ResponseBody public Page<KafkaMessage> listData(KafkaMessage kafkaMessage, HttpServletRequest request, HttpServletResponse response) { List<KafkaMessage> kafkaMessageList = new ArrayList<>(); Gson gson = new Gson(); //连接到 mongodb 服务 MongoClient mongoClient = new MongoClient(mongoUrl, Integer.parseInt(port)); DB database = mongoClient.getDB("MongoTest"); DBCollection kafka = database.getCollection("kafkaMessage"); BasicDBObject query = new BasicDBObject(); if(kafkaMessage.getIsError() != null && kafkaMessage.getIsError() != ""){ if(kafkaMessage.getIsError().equals("1")){ query.put("responseCode", new BasicDBObject("$ne", "0")); }else{ query.put("responseCode", "0"); } } if (kafkaMessage.getServiceId() != null && kafkaMessage.getServiceId() != "") { query.put("serviceId", kafkaMessage.getServiceId()); } if (kafkaMessage.getRequestIp() != null && kafkaMessage.getRequestIp() != "") { query.put("requestIp", kafkaMessage.getRequestIp()); } if (kafkaMessage.getResponseCode() != null && kafkaMessage.getResponseCode() != "") { query.put("responseCode", kafkaMessage.getResponseCode()); } //按照请求时间倒序 Page<KafkaMessage> page = new Page<KafkaMessage>(request, response); page.setPageSize(20); //查询分页的数据显示 DBCursor cursor = kafka.find(query).skip((page.getPageNo() - 1) * page.getPageSize()).limit(page.getPageSize()).sort(new BasicDBObject("requestDate", -1)); //查询总数 DBCursor count = kafka.find(query).sort(new BasicDBObject("requestDate", -1)); while (cursor.hasNext()) { DBObject obj = cursor.next(); //反转 KafkaMessage k = gson.fromJson(obj.toString(), KafkaMessage.class); kafkaMessageList.add(k); } page.setList(kafkaMessageList); page.setCount(count.count()); return page; }