//分页查询
@RequestMapping(“findCloudListPage”)
@ResponseBody
public JSONObject findCloudListPage(Integer page,Integer rows,HttpSession session,String id){
JSONObject jsonObject = new JSONObject();
Query query=new Query();
query.addCriteria(Criteria.where(“pid”).is(id));
//查总条数
long count=mongoTemplate.count(query,CloudDiskBean.class);
query.with(new Sort(new Order(Direction.DESC,“createTime”)));//排序
query.skip((page-1)*rows);//跳过前几页
query.limit(rows);
List find = mongoTemplate.find(query,CloudDiskBean.class);
jsonObject.put(“total”, count);
jsonObject.put(“rows”, find);
return jsonObject;
/*//获取连接
MongoClient client=new MongoClient();
//得到数据库
MongoDatabase database = client.getDatabase("itcastdb");
//得到集合封装对象
MongoCollection<Document> collection = database.getCollection("student");
BasicDBObject bson=new BasicDBObject("name", "铁扇公主");
collection.deleteOne(bson);//删除记录(符合条件的第一条记录)
//collection.deleteMany(bson);//删除符合条件的全部记录
*/
}
|
本文介绍了一种使用SpringMVC框架实现的分页查询方法,通过MongoDB的Java驱动进行数据操作。具体展示了如何设置查询条件、排序、跳过指定数量的记录并限制返回的记录数,同时获取总记录数,最终将查询结果封装为JSON对象返回。
1万+

被折叠的 条评论
为什么被折叠?



