色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

JAVA代碼實現(xiàn)MongoDB動態(tài)條件之分頁查詢

瀏覽:8日期:2022-08-29 09:38:59

一、使用QueryByExampleExecutor

1. 繼承MongoRepository

public interface StudentRepository extends MongoRepository<Student, String> { }

2. 代碼實現(xiàn)

使用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); //創(chuàng)建匹配器,即如何使用查詢條件 ExampleMatcher matcher = ExampleMatcher.matching() //構(gòu)建對象 .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改變默認字符串匹配方式:模糊查詢 .withIgnoreCase(true) //改變默認大小寫忽略方式:忽略大小寫 .withMatcher('name', ExampleMatcher.GenericPropertyMatchers.contains()) //采用“包含匹配”的方式查詢 .withIgnorePaths('pageNum', 'pageSize'); //忽略屬性,不參與查詢 //創(chuàng)建實例 Example<Student> example = Example.of(student, matcher); Page<Student> students = studentRepository.findAll(example, pageable); return students;}

缺點:

不支持過濾條件分組。即不支持過濾條件用 or(或) 來連接,所有的過濾條件,都是簡單一層的用 and(并且) 連接 不支持兩個值的范圍查詢,如時間范圍的查詢

二、MongoTemplate結(jié)合Query

實現(xiàn)一:使用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(); //動態(tài)拼接查詢條件 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())); } //計算總數(shù) long total = mongoTemplate.count(query, Student.class); //查詢結(jié)果集 List<Student> studentList = mongoTemplate.find(query.with(pageable), Student.class); Page<Student> studentPage = new PageImpl(studentList, pageable, total); return studentPage;}

實現(xiàn)二:使用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); //創(chuàng)建匹配器,即如何使用查詢條件 ExampleMatcher matcher = ExampleMatcher.matching() //構(gòu)建對象 .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改變默認字符串匹配方式:模糊查詢 .withIgnoreCase(true) //改變默認大小寫忽略方式:忽略大小寫 .withMatcher('name', ExampleMatcher.GenericPropertyMatchers.contains()) //標題采用“包含匹配”的方式查詢 .withIgnorePaths('pageNum', 'pageSize'); //忽略屬性,不參與查詢 //創(chuàng)建實例 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())); } //計算總數(shù) long total = mongoTemplate.count(query, Student.class); //查詢結(jié)果集 List<Student> studentList = mongoTemplate.find(query.with(pageable), Student.class); Page<Student> studentPage = new PageImpl(studentList, pageable, total); return studentPage;}

缺點:

不支持返回固定字段

三、MongoTemplate結(jié)合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(); //動態(tài)拼接查詢條件 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()); //計算總數(shù) long total = mongoTemplate.count(query, Student.class); //查詢結(jié)果集條件 BasicDBObject fieldsObject = new BasicDBObject(); //id默認有值,可不指定 fieldsObject.append('id', 1) //1查詢,返回數(shù)據(jù)中有值;0不查詢,無值.append('name', 1); query = new BasicQuery(queryBuilder.get().toString(), fieldsObject.toJson()); //查詢結(jié)果集 List<Student> studentList = mongoTemplate.find(query.with(pageable), Student.class); Page<Student> studentPage = new PageImpl(studentList, pageable, total); return studentPage;}

四、MongoTemplate結(jié)合Aggregation

使用Aggregation聚合查詢 支持返回固定字段 支持分組計算總數(shù)、求和、平均值、最大值、最小值等等

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; //獲取滿足添加的總頁數(shù) 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); //查詢結(jié)果集 Page<Student> studentPage = new PageImpl(results.getMappedResults(), pageable, totalCount); return studentPage;}

以上就是JAVA代碼實現(xiàn)MongoDB動態(tài)條件之分頁查詢的詳細內(nèi)容,更多關(guān)于JAVA 實現(xiàn)MongoDB分頁查詢的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標簽: Java
相關(guān)文章:
主站蜘蛛池模板: 国产高清美女一级a毛片久久 | www.色中色 | 国产女人一区二区 | 国产一级毛片在线 | 久久亚洲精品成人综合 | 就草草在线观看视频 | 国产美女做爰免费视频网址 | 久久国产精品免费看 | 国内自拍tv在线 | 亚洲精品一区二区三区四区 | 亚洲一区精品在线 | 黄色毛片一级 | 色资源二区在线视频 | 成人a毛片久久免费播放 | 久久久影院亚洲精品 | 成人久久18网站 | 国产成人a一区二区 | 欧美最爽乱淫视频播放黑人 | 国产欧美一区二区成人影院 | 国产日韩欧美一区二区三区综合 | 美女被免费视频网站a国产 美女被免费网站视频软件 美女被免费网站在线软件 美女被免费网站在线视频软件 | 久久.com| 国产成人三级经典中文 | 精品国产免费人成高清 | 国产a国产| 亚洲国产视频在线 | 久久精品国产三级不卡 | 久久香蕉精品成人 | 日韩一区二区三区不卡视频 | 国产成人精品免费视频大全办公室 | 国产菲菲视频在线观看 | 国产成人午夜精品免费视频 | 国产杨幂福利在线视频观看 | 美女很黄很黄免费 | 国产视频久久久久 | 国内精品久久久久久久影视麻豆 | 一级做a爰片久久毛片美女 一级做a爰片久久毛片免费看 | 中文成人在线视频 | 日韩综合| 欧美极度极度另类 | 成人毛片在线视频 |