国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

JAVA代碼實現MongoDB動態條件之分頁查詢

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

一、使用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;}

以上就是JAVA代碼實現MongoDB動態條件之分頁查詢的詳細內容,更多關于JAVA 實現MongoDB分頁查詢的資料請關注好吧啦網其它相關文章!

標簽: Java
相關文章:
主站蜘蛛池模板: 国产成人夜间影院在线观看 | 草草影院私人免费入口 | 美美女下面被cao爽 美女131爽爽爽做爰中文视频 | 在线播放一区二区精品产 | 日本毛片在线观看 | 99久久国产 | 日本免费a级片 | 亚洲欧美精品一区天堂久久 | 久久中精品中文 | 中国老妇色xxxxx | 亚洲在线中文字幕 | 草草视频在线播放 | 国产亚洲精品久久久久久久久激情 | 香蕉久久久久久狠狠色 | 国产成在线观看免费视频成本人 | 亚洲视频免费在线 | 亚洲国产一区二区三区在线观看 | 日本aaaa特级毛片 | 永久免费91桃色福利 | 精品 日韩 国产 欧美在线观看 | 曰本女人色黄网站 | 精品国产日韩久久亚洲 | 成年人在线视频免费观看 | 免费国产a国产片高清不卡 免费国产不卡午夜福在线 免费国产不卡午夜福在线观看 | 怡红院日本一道日本久久 | 免费逼片 | 黄毛片一级毛片 | 一区二区三区在线观看免费 | 免费观看一级欧美在线视频 | 香蕉超级碰碰碰97视频蜜芽 | 一级美国片免费看 | 亚洲三级大片 | a级毛片免费播放 | 波多野结衣中文一区二区免费 | 精品视频一二三区 | 999成人网| 日韩欧美一区二区三区久久 | 久久视频精品36线视频在线观看 | 久草在线首页 | 日韩激情无码免费毛片 | 青娱乐色|