欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2020年07月15日 15:05:15   作者:時(shí)間-海  
這篇文章主要介紹了JAVA如何實(shí)現(xiàn)MongoDB動(dòng)態(tài)條件之分頁查詢,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下

一、使用QueryByExampleExecutor

1. 繼承MongoRepository

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

2. 代碼實(shí)現(xiàn)

  • 使用ExampleMatcher匹配器-----只支持字符串的模糊查詢,其他類型是完全匹配
  • Example封裝實(shí)體類和匹配器
  • 使用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) //改變默認(rèn)字符串匹配方式:模糊查詢
      .withIgnoreCase(true) //改變默認(rèn)大小寫忽略方式:忽略大小寫
      .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains()) //采用“包含匹配”的方式查詢
      .withIgnorePaths("pageNum", "pageSize"); //忽略屬性,不參與查詢

  //創(chuàng)建實(shí)例
  Example<Student> example = Example.of(student, matcher);
  Page<Student> students = studentRepository.findAll(example, pageable);

  return students;
}

缺點(diǎn):

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

二、MongoTemplate結(jié)合Query

實(shí)現(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();

  //動(dòng)態(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()));
  }

  //計(jì)算總數(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;
}

實(shí)現(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) //改變默認(rèn)字符串匹配方式:模糊查詢
      .withIgnoreCase(true) //改變默認(rèn)大小寫忽略方式:忽略大小寫
      .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains()) //標(biāo)題采用“包含匹配”的方式查詢
      .withIgnorePaths("pageNum", "pageSize"); //忽略屬性,不參與查詢

  //創(chuàng)建實(shí)例
  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()));
  }

  //計(jì)算總數(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;
}

缺點(diǎn):

不支持返回固定字段

三、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();

  //動(dòng)態(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());
  //計(jì)算總數(shù)
  long total = mongoTemplate.count(query, Student.class);

  //查詢結(jié)果集條件
  BasicDBObject fieldsObject = new BasicDBObject();
  //id默認(rèn)有值,可不指定
  fieldsObject.append("id", 1)  //1查詢,返回?cái)?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聚合查詢
  • 支持返回固定字段
  • 支持分組計(jì)算總數(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為空,會(huì)報(bào)錯(cuò)
    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代碼實(shí)現(xiàn)MongoDB動(dòng)態(tài)條件之分頁查詢的詳細(xì)內(nèi)容,更多關(guān)于JAVA 實(shí)現(xiàn)MongoDB分頁查詢的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java中線程上下文類加載器超詳細(xì)講解使用

    Java中線程上下文類加載器超詳細(xì)講解使用

    這篇文章主要介紹了Java中線程上下文類加載器,類加載器負(fù)責(zé)讀取Java字節(jié)代碼,并轉(zhuǎn)換成java.lang.Class類的一個(gè)實(shí)例的代碼模塊。本文主要和大家聊聊JVM類加載器ClassLoader的使用,需要的可以了解一下
    2022-12-12
  • java實(shí)現(xiàn)區(qū)域內(nèi)屏幕截圖示例

    java實(shí)現(xiàn)區(qū)域內(nèi)屏幕截圖示例

    這篇文章主要介紹了java截圖示例,需要的朋友可以參考下
    2014-04-04
  • Java基礎(chǔ)-Java變量的聲明和作用域

    Java基礎(chǔ)-Java變量的聲明和作用域

    這篇文章主要介紹了Java變量的聲明和作用域,變量其實(shí)就是內(nèi)存中的一個(gè)存儲(chǔ)空間,用來存儲(chǔ)數(shù)據(jù),具體的相關(guān)內(nèi)容,需要的小伙伴可以參考下面文章內(nèi)容
    2022-01-01
  • java Swing實(shí)現(xiàn)選項(xiàng)卡功能(JTabbedPane)實(shí)例代碼

    java Swing實(shí)現(xiàn)選項(xiàng)卡功能(JTabbedPane)實(shí)例代碼

    這篇文章主要介紹了java Swing實(shí)現(xiàn)選項(xiàng)卡功能(JTabbedPane)實(shí)例代碼的相關(guān)資料,學(xué)習(xí)java 基礎(chǔ)的朋友可以參考下這個(gè)簡單示例,需要的朋友可以參考下
    2016-11-11
  • 詳解Flutter TabLayout 布局用法

    詳解Flutter TabLayout 布局用法

    Flutter是谷歌的移動(dòng)UI框架,可以快速在iOS和Android上構(gòu)建高質(zhì)量的原生用戶界面。這篇文章主要介紹了Flutter TabLayout 布局用法,需要的朋友可以參考下
    2019-07-07
  • Java基本類型和包裝類型的區(qū)別

    Java基本類型和包裝類型的區(qū)別

    這篇文章主要介紹了Java基本類型和包裝類型的區(qū)別,幫助大家更好的理解和學(xué)習(xí)Java,感興趣的朋友可以了解下
    2020-09-09
  • Spring Session實(shí)現(xiàn)分布式session的簡單示例

    Spring Session實(shí)現(xiàn)分布式session的簡單示例

    本篇文章主要介紹了Spring Session實(shí)現(xiàn)分布式session的簡單示例,具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-05-05
  • Java別名Alias是如何工作的

    Java別名Alias是如何工作的

    這篇文章主要介紹了Java別名Alias是如何工作的,別名的問題是,當(dāng)用戶寫入特定對象時(shí),其他幾個(gè)引用的所有者不希望該對象發(fā)生更改,下文相關(guān)介紹需要的小伙伴可以參考一下
    2022-04-04
  • SpringCloud集成Sleuth和Zipkin的思路講解

    SpringCloud集成Sleuth和Zipkin的思路講解

    Zipkin 是 Twitter 的一個(gè)開源項(xiàng)目,它基于 Google Dapper 實(shí)現(xiàn),它致力于收集服務(wù)的定時(shí)數(shù)據(jù),以及解決微服務(wù)架構(gòu)中的延遲問題,包括數(shù)據(jù)的收集、存儲(chǔ)、查找和展現(xiàn),這篇文章主要介紹了SpringCloud集成Sleuth和Zipkin,需要的朋友可以參考下
    2022-11-11
  • springboot實(shí)現(xiàn)微信掃碼登錄的項(xiàng)目實(shí)踐

    springboot實(shí)現(xiàn)微信掃碼登錄的項(xiàng)目實(shí)踐

    微信掃碼功能是目前第三方登錄常見功能,前不久有個(gè)項(xiàng)目剛好用上,本文主要介紹了springboot實(shí)現(xiàn)微信掃碼登錄的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-10-10

最新評論