org.springframework.data.domain.Sort.by()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(10.0k)|赞(0)|评价(0)|浏览(406)

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

Sort.by介绍

[英]Creates a new Sort for the given Orders.
[中]为给定的订单创建新的排序。

代码示例

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

private Sort convertToSort(Map<String, Sort.Direction> sorts) {
    List<Sort.Order> sortValues = new ArrayList<>();

    for (Map.Entry<String, Sort.Direction> curSort : sorts.entrySet()) {
      sortValues.add(new Sort.Order(curSort.getValue(), curSort.getKey()));
    }

    return Sort.by(sortValues);
  }
}

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

private Sort convertToSort(Map<String, Sort.Direction> sorts) {
  List<Sort.Order> sortValues = new ArrayList<>();
  for (Map.Entry<String, Sort.Direction> curSort : sorts.entrySet()) {
    sortValues.add(new Sort.Order(curSort.getValue(), curSort.getKey()));
  }
  return Sort.by(sortValues);
}

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

/**
 * Factory method to create a new {@link SortOperation} for the given sort {@link Direction} and {@code fields}.
 *
 * @param direction must not be {@literal null}.
 * @param fields must not be {@literal null}.
 * @return
 */
public static SortOperation sort(Direction direction, String... fields) {
  return new SortOperation(Sort.by(direction, fields));
}

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

public SortOperation and(Direction direction, String... fields) {
  return and(Sort.by(direction, fields));
}

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

@Override
  public Cursor<Product> findAllUsingCursor() {

    // NOTE: Using Cursor requires to sort by an unique field
    return solrTemplate.queryForCursor("techproducts", new SimpleQuery("*:*").addSort(Sort.by("id")), Product.class);
  }
}

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

/**
 * Creates new {@link Sort} with potentially unsafe {@link Order} instances.
 *
 * @param properties must not be {@literal null}.
 * @return
 */
public Sort withUnsafe(String... properties) {
  Assert.notEmpty(properties, "Properties must not be empty!");
  Assert.noNullElements(properties, "Properties must not contain null values!");
  List<Order> orders = new ArrayList<>(properties.length);
  for (String property : properties) {
    orders.add(new JpaOrder(getDirection(), property, getNullHandling(), isIgnoreCase(), this.unsafe));
  }
  return Sort.by(orders);
}

代码示例来源: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-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 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: org.springframework.data/spring-data-mongodb

/**
 * Factory method to create a new {@link SortOperation} for the given sort {@link Direction} and {@code fields}.
 *
 * @param direction must not be {@literal null}.
 * @param fields must not be {@literal null}.
 * @return
 */
public static SortOperation sort(Direction direction, String... fields) {
  return new SortOperation(Sort.by(direction, fields));
}

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

public SortOperation and(Direction direction, String... fields) {
  return and(Sort.by(direction, fields));
}

代码示例来源: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: spring-projects/spring-integration

@Override
public Message<?> pollMessageFromGroup(Object groupId) {
  Assert.notNull(groupId, "'groupId' must not be null");
  Sort sort = Sort.by(MessageDocumentFields.LAST_MODIFIED_TIME, MessageDocumentFields.SEQUENCE);
  if (this.priorityEnabled) {
    sort = Sort.by(Sort.Direction.DESC, MessageDocumentFields.PRIORITY).and(sort);
  }
  Query query = groupIdQuery(groupId).with(sort);
  MessageDocument document = this.mongoTemplate.findAndRemove(query, MessageDocument.class, this.collectionName);
  Message<?> message = null;
  if (document != null) {
    message = document.getMessage();
  }
  return message;
}

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

/**
 * Translates {@link Sort} orders from Jackson-mapped field names to {@link PersistentProperty} names. Properties
 * that cannot be resolved are dropped.
 *
 * @param input must not be {@literal null}.
 * @param rootEntity must not be {@literal null}.
 * @return {@link Sort} with translated field names or {@literal null} if translation dropped all sort fields.
 */
public Sort translateSort(Sort input, PersistentEntity<?, ?> rootEntity) {
  Assert.notNull(input, "Sort must not be null!");
  Assert.notNull(rootEntity, "PersistentEntity must not be null!");
  List<Order> filteredOrders = new ArrayList<Order>();
  for (Order order : input) {
    List<String> iteratorSource = new ArrayList<String>();
    Matcher matcher = SPLITTER.matcher("_" + order.getProperty());
    while (matcher.find()) {
      iteratorSource.add(matcher.group(1));
    }
    String mappedPropertyPath = getMappedPropertyPath(rootEntity, iteratorSource);
    if (mappedPropertyPath != null) {
      filteredOrders.add(order.withProperty(mappedPropertyPath));
    }
  }
  return filteredOrders.isEmpty() ? Sort.unsorted() : Sort.by(filteredOrders);
}

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

@Override
public Message<?> pollMessageFromGroup(final Object groupId) {
  Assert.notNull(groupId, "'groupId' must not be null");
  Query query = whereGroupIdIs(groupId).with(Sort.by(GROUP_UPDATE_TIMESTAMP_KEY, SEQUENCE));
  MessageWrapper messageWrapper = this.template.findAndRemove(query, MessageWrapper.class, this.collectionName);
  Message<?> message = null;
  if (messageWrapper != null) {
    message = messageWrapper.getMessage();
  }
  updateGroup(groupId, lastModifiedUpdate());
  return message;
}

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

@Override
public Message<?> pollMessageFromGroup(final Object groupId) {
  Assert.notNull(groupId, "'groupId' must not be null");
  Sort sort = Sort.by(MessageDocumentFields.LAST_MODIFIED_TIME, MessageDocumentFields.SEQUENCE);
  Query query = groupIdQuery(groupId).with(sort);
  MessageDocument document = mongoTemplate.findAndRemove(query, MessageDocument.class, collectionName);
  Message<?> message = null;
  if (document != null) {
    message = document.getMessage();
    updateGroup(groupId, lastModifiedUpdate());
  }
  return message;
}

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

/**
 * Creates a new {@link PageRequest} with sort direction and properties applied for the first page.
 *
 * @param size the size of the page to be returned.
 * @param direction must not be {@literal null}.
 * @param properties must not be {@literal null}.
 */
public static CassandraPageRequest first(int size, Direction direction, String... properties) {
  return first(size, Sort.by(direction, properties));
}

代码示例来源:origin: apache/servicemix-bundles

private Sort convertToSort(Map<String, Sort.Direction> sorts) {
  List<Sort.Order> sortValues = new ArrayList<>();
  for (Map.Entry<String, Sort.Direction> curSort : sorts.entrySet()) {
    sortValues.add(new Sort.Order(curSort.getValue(), curSort.getKey()));
  }
  return Sort.by(sortValues);
}

相关文章