JAVA代码实现MongoDB动态条件之分页查询
作者:时间-海 发布时间:2022-05-21 12:44:11
标签:JAVA,MongoDB,动态条件,分页查询
一、使用QueryByExampleExecutor
1. 继承MongoRepository
public interface StudentRepository extends MongoRepository<Student, String> {
}
2. 代码实现
使用ExampleMatcher匹配器-----只支持字符串的模糊查询,其他类型是完全匹配
Example封装实体类和匹配器
使用QueryByExampleExecutor接口中的findAll方法
public Page<Student> getListWithExample(StudentReqVO studentReqVO) {
Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
Pageable pageable = PageRequest.of(studentReqVO.getPageNum(), studentReqVO.getPageSize(), sort);
Student student = new Student();
BeanUtils.copyProperties(studentReqVO, student);
//创建匹配器,即如何使用查询条件
ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式:模糊查询
.withIgnoreCase(true) //改变默认大小写忽略方式:忽略大小写
.withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains()) //采用“包含匹配”的方式查询
.withIgnorePaths("pageNum", "pageSize"); //忽略属性,不参与查询
//创建实例
Example<Student> example = Example.of(student, matcher);
Page<Student> students = studentRepository.findAll(example, pageable);
return students;
}
缺点:
不支持过滤条件分组。即不支持过滤条件用 or(或) 来连接,所有的过滤条件,都是简单一层的用 and(并且) 连接
不支持两个值的范围查询,如时间范围的查询
二、MongoTemplate结合Query
实现一:使用Criteria封装查询条件
public Page<Student> getListWithCriteria(StudentReqVO studentReqVO) {
Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
Pageable pageable = PageRequest.of(studentReqVO.getPageNum(), studentReqVO.getPageSize(), sort);
Query query = new Query();
//动态拼接查询条件
if (!StringUtils.isEmpty(studentReqVO.getName())){
Pattern pattern = Pattern.compile("^.*" + studentReqVO.getName() + ".*$", Pattern.CASE_INSENSITIVE);
query.addCriteria(Criteria.where("name").regex(pattern));
}
if (studentReqVO.getSex() != null){
query.addCriteria(Criteria.where("sex").is(studentReqVO.getSex()));
}
if (studentReqVO.getCreateTime() != null){
query.addCriteria(Criteria.where("createTime").lte(studentReqVO.getCreateTime()));
}
//计算总数
long total = mongoTemplate.count(query, Student.class);
//查询结果集
List<Student> studentList = mongoTemplate.find(query.with(pageable), Student.class);
Page<Student> studentPage = new PageImpl(studentList, pageable, total);
return studentPage;
}
实现二:使用Example和Criteria封装查询条件
public Page<Student> getListWithExampleAndCriteria(StudentReqVO studentReqVO) {
Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
Pageable pageable = PageRequest.of(studentReqVO.getPageNum(), studentReqVO.getPageSize(), sort);
Student student = new Student();
BeanUtils.copyProperties(studentReqVO, student);
//创建匹配器,即如何使用查询条件
ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式:模糊查询
.withIgnoreCase(true) //改变默认大小写忽略方式:忽略大小写
.withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains()) //标题采用“包含匹配”的方式查询
.withIgnorePaths("pageNum", "pageSize"); //忽略属性,不参与查询
//创建实例
Example<Student> example = Example.of(student, matcher);
Query query = new Query(Criteria.byExample(example));
if (studentReqVO.getCreateTime() != null){
query.addCriteria(Criteria.where("createTime").lte(studentReqVO.getCreateTime()));
}
//计算总数
long total = mongoTemplate.count(query, Student.class);
//查询结果集
List<Student> studentList = mongoTemplate.find(query.with(pageable), Student.class);
Page<Student> studentPage = new PageImpl(studentList, pageable, total);
return studentPage;
}
缺点:
不支持返回固定字段
三、MongoTemplate结合BasicQuery
BasicQuery是Query的子类
支持返回固定字段
public Page<Student> getListWithBasicQuery(StudentReqVO studentReqVO) {
Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
Pageable pageable = PageRequest.of(studentReqVO.getPageNum(), studentReqVO.getPageSize(), sort);
QueryBuilder queryBuilder = new QueryBuilder();
//动态拼接查询条件
if (!StringUtils.isEmpty(studentReqVO.getName())) {
Pattern pattern = Pattern.compile("^.*" + studentReqVO.getName() + ".*$", Pattern.CASE_INSENSITIVE);
queryBuilder.and("name").regex(pattern);
}
if (studentReqVO.getSex() != null) {
queryBuilder.and("sex").is(studentReqVO.getSex());
}
if (studentReqVO.getCreateTime() != null) {
queryBuilder.and("createTime").lessThanEquals(studentReqVO.getCreateTime());
}
Query query = new BasicQuery(queryBuilder.get().toString());
//计算总数
long total = mongoTemplate.count(query, Student.class);
//查询结果集条件
BasicDBObject fieldsObject = new BasicDBObject();
//id默认有值,可不指定
fieldsObject.append("id", 1) //1查询,返回数据中有值;0不查询,无值
.append("name", 1);
query = new BasicQuery(queryBuilder.get().toString(), fieldsObject.toJson());
//查询结果集
List<Student> studentList = mongoTemplate.find(query.with(pageable), Student.class);
Page<Student> studentPage = new PageImpl(studentList, pageable, total);
return studentPage;
}
四、MongoTemplate结合Aggregation
使用Aggregation聚合查询
支持返回固定字段
支持分组计算总数、求和、平均值、最大值、最小值等等
public Page<Student> getListWithAggregation(StudentReqVO studentReqVO) {
Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
Pageable pageable = PageRequest.of(studentReqVO.getPageNum(), studentReqVO.getPageSize(), sort);
Integer pageNum = studentReqVO.getPageNum();
Integer pageSize = studentReqVO.getPageSize();
List<AggregationOperation> operations = new ArrayList<>();
if (!StringUtils.isEmpty(studentReqVO.getName())) {
Pattern pattern = Pattern.compile("^.*" + studentReqVO.getName() + ".*$", Pattern.CASE_INSENSITIVE);
Criteria criteria = Criteria.where("name").regex(pattern);
operations.add(Aggregation.match(criteria));
}
if (null != studentReqVO.getSex()) {
operations.add(Aggregation.match(Criteria.where("sex").is(studentReqVO.getSex())));
}
long totalCount = 0;
//获取满足添加的总页数
if (null != operations && operations.size() > 0) {
Aggregation aggregationCount = Aggregation.newAggregation(operations); //operations为空,会报错
AggregationResults<Student> resultsCount = mongoTemplate.aggregate(aggregationCount, "student", Student.class);
totalCount = resultsCount.getMappedResults().size();
} else {
List<Student> list = mongoTemplate.findAll(Student.class);
totalCount = list.size();
}
operations.add(Aggregation.skip((long) pageNum * pageSize));
operations.add(Aggregation.limit(pageSize));
operations.add(Aggregation.sort(Sort.Direction.DESC, "createTime"));
Aggregation aggregation = Aggregation.newAggregation(operations);
AggregationResults<Student> results = mongoTemplate.aggregate(aggregation, "student", Student.class);
//查询结果集
Page<Student> studentPage = new PageImpl(results.getMappedResults(), pageable, totalCount);
return studentPage;
}
来源:https://www.cnblogs.com/wslook/p/9275861.html
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- 环境操作系统windows10JDKjdk1.8.0_192IDEEclipse IDE for Enterprise Java Devel
- starter起步依赖starter起步依赖是springboot一种非常重要的机制,它打包了某些场景下需要用到依赖,将其统一集成到star
- 这篇文章主要介绍了Springboot整合MybatisPlus的实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定
- 本文通过优化买票的重复流程来说明享元模式,为了加深对该模式的理解,会以String和基本数据类型的包装类对该模式的设计进一步说明。读者可以拉
- 简介本文主要讲解如何用java Selenium 控制鼠标在浏览器上的操作方法。主要列举的代码示例,无图显示。可以自己上代码执行操作看效果。
- 1. 为什么写这篇文章?事情是这样的,在 2021年6月10日早上我在CSDN上发布了文章《你真的懂Java怎么输出Hello World吗
- 函数式接口1.1 函数式接口概述函数式接口:有且仅有一个抽象方法的接口Java中的函数式编程体现就是Lambda表达式,所以函数式接口就是可
- 1、自定义消息转换器MessageConverter在WebMvcAutoConfiguration类中有一个方法configureMess
- 1. 你知道线程安全问题吗?线程安全问题:一般指在多线程模式下,多个线程对同一个共享数据进行操作时,第一个线程还没来得及更新共享数据,从而导
- 前言当用户向服务器发送了一次HTTP请求,该请求可能会经过多个信息资源处理以后才返回给用户,各个信息资源使用请求转发机制相互转发请求,但是用
- Executor接口基于以下方法可以完成增,删,改查以及事务处理等操作。事实上,mybatis中的所有数据库操作是通过调用这些方法实现的。p
- 在阅读这篇文章之前,大家可以先看下《Java多线程atomic包介绍及使用方法》,了解atomic包的相关内容。一、何谓Atomic?Ato
- 实现HandlerInterceptor接口或者继承HandlerInterceptor的子类,比如Spring 已经提供的实现了Handl
- 问题原因Springboot get请求是参数过长抛出异常:Request header is too large 的问题错误描述java.
- springboot上传文件大小的配置我这里记录两种,一种是设置在配置文件里只有两行代码,一种是加个Bean首先第一种:applicatio
- 在Android中,线程内部或者线程之间进行信息交互时经常会使用消息,这些基础的东西如果我们熟悉其内部的原理,将会使我们容易、更好地架构系统
- 目录引言简单遍历筛选符合某属性条件的List集合获取某属性返回新的List集合获取以某属性为key,其他属性或者对应对象为value的Map
- 最近需要对接支付宝的支付接口,官方文档写得内容有点分散,整理了一下发布出来,用作记录,同时也希望对不了解情况的人有所帮助,这里以电脑端的网页
- RocketMQ发送消息我们在使用RocketMQ发送消息时,一般都会使用DefaultMQProducer,类型的代码如下:Default
- Java float和double精度范围大小要想理解float和double的取值范围和计算精度,必须先了解小数是如何在计算机中存储的:举