org.springframework.data.mongodb.core.query.Query.with()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(10.5k)|赞(0)|评价(0)|浏览(416)

本文整理了Java中org.springframework.data.mongodb.core.query.Query.with方法的一些代码示例,展示了Query.with的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Query.with方法的具体详情如下:
包路径:org.springframework.data.mongodb.core.query.Query
类名称:Query
方法名:with

Query.with介绍

[英]Sets the given pagination information on the Query instance. Will transparently set skip and limit as well as applying the Sort instance defined with the Pageable.
[中]设置查询实例上的给定分页信息。将透明地设置skip和limit,并应用使用Pageable定义的排序实例。

代码示例

代码示例来源:origin: spring-projects/spring-data-mongodb

@Override
protected Query complete(Criteria criteria, Sort sort) {
  Query query = (criteria == null ? new Query() : new Query(criteria)).with(sort);
  if (LOG.isDebugEnabled()) {
    LOG.debug("Created query " + query);
  }
  return query;
}

代码示例来源:origin: spring-projects/spring-batch

mongoQuery.with(pageRequest);
query.with(pageRequest);

代码示例来源:origin: spring-projects/spring-data-mongodb

@Override
public Flux<T> findAll(Sort sort) {
  Assert.notNull(sort, "Sort must not be null!");
  return findAll(new Query().with(sort));
}

代码示例来源:origin: spring-projects/spring-data-mongodb

@Override
public List<T> findAll(Sort sort) {
  Assert.notNull(sort, "Sort must not be null!");
  return findAll(new Query().with(sort));
}

代码示例来源:origin: spring-projects/spring-data-mongodb

@Override
  @SuppressWarnings({ "unchecked", "rawtypes" })
  public Object execute(Query query) {
    int pageSize = pageable.getPageSize();
    // Apply Pageable but tweak limit to peek into next page
    Query modifiedQuery = query.with(pageable).limit(pageSize + 1);
    List result = find.matching(modifiedQuery).all();
    boolean hasNext = result.size() > pageSize;
    return new SliceImpl<Object>(hasNext ? result.subList(0, pageSize) : result, pageable, hasNext);
  }
}

代码示例来源:origin: spring-projects/spring-data-mongodb

@Override
public Page<T> findAll(Pageable pageable) {
  Assert.notNull(pageable, "Pageable must not be null!");
  Long count = count();
  List<T> list = findAll(new Query().with(pageable));
  return new PageImpl<T>(list, pageable, count);
}

代码示例来源:origin: spring-projects/spring-data-mongodb

@Override
public <S extends T> List<S> findAll(Example<S> example, Sort sort) {
  Assert.notNull(example, "Sample must not be null!");
  Assert.notNull(sort, "Sort must not be null!");
  Query q = new Query(new Criteria().alike(example)).with(sort);
  return mongoOperations.find(q, example.getProbeType(), entityInformation.getCollectionName());
}

代码示例来源:origin: spring-projects/spring-data-mongodb

@Override
public <S extends T> Flux<S> findAll(Example<S> example, Sort sort) {
  Assert.notNull(example, "Sample must not be null!");
  Assert.notNull(sort, "Sort must not be null!");
  Query query = new Query(new Criteria().alike(example)).with(sort);
  return mongoOperations.find(query, example.getProbeType(), entityInformation.getCollectionName());
}

代码示例来源:origin: spring-projects/spring-data-mongodb

/**
 * Sets the given pagination information on the {@link Query} instance. Will transparently set {@code skip} and
 * {@code limit} as well as applying the {@link Sort} instance defined with the {@link Pageable}.
 *
 * @param pageable
 * @return
 */
public Query with(Pageable pageable) {
  if (pageable.isUnpaged()) {
    return this;
  }
  this.limit = pageable.getPageSize();
  this.skip = pageable.getOffset();
  return with(pageable.getSort());
}

代码示例来源:origin: spring-projects/spring-data-mongodb

@Override
public <S extends T> Page<S> findAll(final Example<S> example, Pageable pageable) {
  Assert.notNull(example, "Sample must not be null!");
  Assert.notNull(pageable, "Pageable must not be null!");
  Query q = new Query(new Criteria().alike(example)).with(pageable);
  List<S> list = mongoOperations.find(q, example.getProbeType(), entityInformation.getCollectionName());
  return PageableExecutionUtils.getPage(list, pageable,
      () -> mongoOperations.count(q, example.getProbeType(), entityInformation.getCollectionName()));
}

代码示例来源:origin: spring-projects/spring-data-mongodb

@Override
  public Object execute(Query query) {
    int overallLimit = query.getLimit();
    TerminatingFind<?> matching = operation.matching(query);
    // Apply raw pagination
    query.with(pageable);
    // Adjust limit if page would exceed the overall limit
    if (overallLimit != 0 && pageable.getOffset() + pageable.getPageSize() > overallLimit) {
      query.limit((int) (overallLimit - pageable.getOffset()));
    }
    return PageableExecutionUtils.getPage(matching.all(), pageable, () -> {
      long count = matching.count();
      return overallLimit != 0 ? Math.min(count, overallLimit) : count;
    });
  }
}

代码示例来源:origin: spring-projects/spring-batch

@Test
public void testQueryObjectWithIgnoredPageSize() throws Exception {
  reader = new MongoItemReader<>();
  reader.setTemplate(template);
  
  Query query = new Query()
      .with(Sort.by(new Order(Sort.Direction.ASC, "_id")))
      .with(PageRequest.of(0, 50));
  reader.setQuery(query);
  reader.setTargetType(String.class);
  
  reader.afterPropertiesSet();
  
  ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class);
  when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<>());
  
  assertFalse(reader.doPageRead().hasNext());
  
  Query actualQuery = queryContainer.getValue();
  assertFalse(reader.doPageRead().hasNext());
  assertEquals(10, actualQuery.getLimit());
  assertEquals(0, actualQuery.getSkip());
}

代码示例来源:origin: spring-projects/spring-batch

@Test
public void testQueryObjectWithPageSize() throws Exception {
  reader = new MongoItemReader<>();
  reader.setTemplate(template);
  
  Query query = new Query()
      .with(Sort.by(new Order(Sort.Direction.ASC, "_id")))
      .with(PageRequest.of(30, 50));
  reader.setQuery(query);
  reader.setTargetType(String.class);
  reader.setPageSize(100);
  
  reader.afterPropertiesSet();
  
  ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class);
  when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<>());
  
  assertFalse(reader.doPageRead().hasNext());
  
  Query actualQuery = queryContainer.getValue();
  assertFalse(reader.doPageRead().hasNext());
  assertEquals(100, actualQuery.getLimit());
  assertEquals(0, actualQuery.getSkip());
}

代码示例来源:origin: spring-projects/spring-batch

@Test
public void testQueryObject() throws Exception {
  reader = new MongoItemReader<>();
  reader.setTemplate(template);
  
  Query query = new Query()
      .with(Sort.by(new Order(Sort.Direction.ASC, "_id")));
  reader.setQuery(query);
  reader.setTargetType(String.class);
  
  reader.afterPropertiesSet();
  
  ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class);
  when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<>());
  
  assertFalse(reader.doPageRead().hasNext());
  
  Query actualQuery = queryContainer.getValue();
  assertFalse(reader.doPageRead().hasNext());
  assertEquals(10, actualQuery.getLimit());
  assertEquals(0, actualQuery.getSkip());
}

代码示例来源:origin: spring-projects/spring-batch

@Test
  public void testQueryObjectWithCollection() throws Exception {
    reader = new MongoItemReader<>();
    reader.setTemplate(template);
    
    Query query = new Query()
        .with(Sort.by(new Order(Sort.Direction.ASC, "_id")));
    reader.setQuery(query);
    reader.setTargetType(String.class);
    reader.setCollection("collection");
    
    reader.afterPropertiesSet();
    
    ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class);
    ArgumentCaptor<String> stringContainer = ArgumentCaptor.forClass(String.class);
    when(template.find(queryContainer.capture(), eq(String.class), stringContainer.capture())).thenReturn(new ArrayList<>());
    
    assertFalse(reader.doPageRead().hasNext());
    
    Query actualQuery = queryContainer.getValue();
    assertFalse(reader.doPageRead().hasNext());
    assertEquals(10, actualQuery.getLimit());
    assertEquals(0, actualQuery.getSkip());
    assertEquals("collection", stringContainer.getValue());
  }
}

代码示例来源:origin: spring-projects/spring-data-mongodb

private ReactiveMongoQueryExecution getExecutionToWrap(MongoParameterAccessor accessor, FindWithQuery<?> operation) {
  if (isDeleteQuery()) {
    return new DeleteExecution(operations, method);
  } else if (method.isGeoNearQuery()) {
    return new GeoNearExecution(operations, accessor, method.getReturnType());
  } else if (isTailable(method)) {
    return (q, t, c) -> operation.matching(q.with(accessor.getPageable())).tail();
  } else if (method.isCollectionQuery()) {
    return (q, t, c) -> operation.matching(q.with(accessor.getPageable())).all();
  } else if (isCountQuery()) {
    return (q, t, c) -> operation.matching(q).count();
  } else if (isExistsQuery()) {
    return (q, t, c) -> operation.matching(q).exists();
  } else {
    return (q, t, c) -> {
      TerminatingFind<?> find = operation.matching(q);
      if (isCountQuery()) {
        return find.count();
      }
      return isLimiting() ? find.first() : find.one();
    };
  }
}

代码示例来源:origin: spring-projects/spring-batch

@Test
public void testAfterPropertiesSetForQueryObject() throws Exception{
  reader = new MongoItemReader<>();
  
  reader.setTemplate(template);
  reader.setTargetType(String.class);
  
  Query query1 = new Query().with(Sort.by(new Order(Sort.Direction.ASC, "_id")));
  reader.setQuery(query1);
  reader.afterPropertiesSet();
}

代码示例来源:origin: spring-projects/spring-data-mongodb

private MongoQueryExecution getExecution(ConvertingParameterAccessor accessor, FindWithQuery<?> operation) {
  if (isDeleteQuery()) {
    return new DeleteExecution(operations, method);
  } else if (method.isGeoNearQuery() && method.isPageQuery()) {
    return new PagingGeoNearExecution(operation, method, accessor, this);
  } else if (method.isGeoNearQuery()) {
    return new GeoNearExecution(operation, method, accessor);
  } else if (method.isSliceQuery()) {
    return new SlicedExecution(operation, accessor.getPageable());
  } else if (method.isStreamQuery()) {
    return q -> operation.matching(q).stream();
  } else if (method.isCollectionQuery()) {
    return q -> operation.matching(q.with(accessor.getPageable()).with(accessor.getSort())).all();
  } else if (method.isPageQuery()) {
    return new PagedExecution(operation, accessor.getPageable());
  } else if (isCountQuery()) {
    return q -> operation.matching(q).count();
  } else if (isExistsQuery()) {
    return q -> operation.matching(q).exists();
  } else {
    return q -> {
      TerminatingFind<?> find = operation.matching(q);
      return isLimiting() ? find.firstValue() : find.oneValue();
    };
  }
}

代码示例来源:origin: spring-projects/spring-integration

private static Query groupOrderQuery(Object groupId) {
  Sort sort = Sort.by(Sort.Direction.DESC, MessageDocumentFields.LAST_MODIFIED_TIME,
      MessageDocumentFields.SEQUENCE);
  return groupIdQuery(groupId).with(sort);
}

代码示例来源:origin: org.springframework.data/spring-data-mongodb

@Override
  @SuppressWarnings({ "unchecked", "rawtypes" })
  public Object execute(Query query) {
    int pageSize = pageable.getPageSize();
    // Apply Pageable but tweak limit to peek into next page
    Query modifiedQuery = query.with(pageable).limit(pageSize + 1);
    List result = find.matching(modifiedQuery).all();
    boolean hasNext = result.size() > pageSize;
    return new SliceImpl<Object>(hasNext ? result.subList(0, pageSize) : result, pageable, hasNext);
  }
}

相关文章