赞
踩
mongodb是常用的非关系型数据库,他经常用来存储文本数据,也就是JSON格式的数据。
不废话,直接上代码。注释写的很详细。(有问题留言秒回)
public Page<Product> listProducts(ProductCond cond) { //如前端没传,就默认第一页,前10条 if(cond.getSize()==null || cond.getCurrent() ==null) { cond.setSize(1); cond.setCurrent(10); } Query query = new Query(); query.addCriteria(Criteria.where("data_status").is(1L)); query.addCriteria(Criteria.where("tenant_id").is(LoginUserContext.getContext().getTenantId())); //手动拼接查询参数 if(StringUtils.isNotBlank(cond.getProductSoleCode())){ query.addCriteria(Criteria.where("product_sole_code").is(cond.getProductSoleCode())); } //.regex为模糊查询 if(StringUtils.isNotBlank(cond.getProductTemplateName())){ query.addCriteria(Criteria.where("product_template_name").regex(".*?" + cond.getProductTemplateName() + ".*")); } if(StringUtils.isNotBlank(cond.getProductTypeName())){ query.addCriteria(Criteria.where("product_type_name").is(cond.getProductTypeName())); } //设置时间区间 if(StringUtils.isNotBlank(cond.getStartCreateTime()) && StringUtils.isNotBlank(cond.getEndCreateTime())){ Date startTime = strToDateLong(cond.getStartCreateTime()); Date endTime = strToDateLong(cond.getEndCreateTime()); query.addCriteria(Criteria.where("create_time").gte(startTime).lte(endTime)); } //查全部的条数 long total = mongoTemplate.count(query,Product.class); //手动拼接分页参数+根据创建时间倒叙 query.skip((cond.getCurrent() - 1L) *cond.getSize()).limit(cond.getSize()).with(Sort.by(Sort.Direction.DESC, "create_time")); log.info("查询mongodb产品实例的条件为:{}", query); List<Product> userList = mongoTemplate.find(query,Product.class); //手动拼接分页结果 long pages = (long)Math.ceil(total/cond.getSize()) ; Page<Product> pageList = new Page<>(); pageList.setRecords(userList); pageList.setTotal(total); pageList.setPages(pages); pageList.setCurrent(cond.getCurrent()); pageList.setSize(cond.getSize()); return pageList; }
mongoTemplate可以进行区间查询,MongoRepository反正我是没找到他能区间查询的方法。所以查询推荐使用mongoTemplate进行区间查询。
查询结果:
参考以下内容:
https://blog.csdn.net/weixin_43059031/article/details/100079941
https://blog.csdn.net/a1120467800/article/details/109954145
https://blog.csdn.net/weixin_44216706/article/details/106496298
https://blog.csdn.net/cheryjava/article/details/120655671
https://blog.csdn.net/qq_38359685/article/details/113865582
https://blog.csdn.net/xjx891111/article/details/120371529
https://blog.csdn.net/qq_43718308/article/details/108128461
https://blog.csdn.net/edc0228/article/details/92645396
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。