controller层
//评论分页
@RequestMapping("queryPingLun")
@ResponseBody
public JSONObject queryPingLun(String goodId, Integer page, Integer rows, CommentsBean commentsBean) {
JSONObject chaxun;
if(redisTemplate.hasKey(rows+page)){
chaxun = (JSONObject) redisTemplate.opsForValue().get(rows + page);
System.out.println("走缓存");
}else{
chaxun=goodsService.queryPingLun(goodId,page,rows,commentsBean);
redisTemplate.opsForValue().set(rows+page,chaxun);
System.out.println("走数据库");
}
return chaxun;
}
serviceImpl层逻辑
//评论分页
@Override
public JSONObject queryPingLun(String goodId, Integer page, Integer rows, CommentsBean commentsBean) {
Query query = new Query();
//根据id查询每个用户的评论
query.addCriteria(Criteria.where("goodsId").is(goodId));
//查询数量
long count = mongoTemplate.count(query, CommentsBean.class);
//起始条数
int start = (page-1)*rows;
//分页
query.skip(start).limit(rows);
//查询数据
List<CommentsBean> find = mongoTemplate.find(query, CommentsBean.class);
jsonObject.put("total", count);
jsonObject.put("rows", find);
return jsonObject;
}