org.springframework.data.domain.Sort类的使用及代码示例

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

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

Sort介绍

[英]Sort option for queries. You have to provide at least a list of properties to sort for that must not include null or empty strings. The direction defaults to Sort#DEFAULT_DIRECTION.
[中]查询的排序选项。您必须至少提供一个属性列表进行排序,这些属性不能包含null或空字符串。方向默认为排序#默认_方向。

代码示例

代码示例来源: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-data-mongodb

/**
 * Adds a {@link Sort} to the {@link Query} instance.
 *
 * @param sort
 * @return
 */
public Query with(Sort sort) {
  Assert.notNull(sort, "Sort must not be null!");
  if (sort.isUnsorted()) {
    return this;
  }
  sort.stream().filter(Order::isIgnoreCase).findFirst().ifPresent(it -> {
    throw new IllegalArgumentException(String.format("Given sort contained an Order for %s with ignore case! "
        + "MongoDB does not support sorting ignoring case currently!", it.getProperty()));
  });
  this.sort = this.sort.and(sort);
  return this;
}

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

@Override
public Optional<T> findOne(@Nullable Specification<T> spec) {
  try {
    return Optional.of(getQuery(spec, Sort.unsorted()).getSingleResult());
  } catch (NoResultException e) {
    return Optional.empty();
  }
}

代码示例来源:origin: com.epam.reportportal/commons-dao

@Override
public Optional<Launch> findLastLaunch(String projectId, String mode) {
  Query query = query(where(PROJECT_ID_REFERENCE).is(projectId)).addCriteria(where(STATUS).ne(IN_PROGRESS))
      .addCriteria(where(MODE).is(mode))
      .limit(1)
      .with(new Sort(DESC, START_TIME));
  List<Launch> launches = mongoTemplate.find(query, Launch.class);
  return !launches.isEmpty() ? Optional.of(launches.get(0)) : Optional.empty();
}

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

public Sort getMappedSort(Sort sort, CassandraPersistentEntity<?> entity) {
  Assert.notNull(sort, "Sort must not be null");
  Assert.notNull(entity, "CassandraPersistentEntity must not be null");
  if (!sort.iterator().hasNext()) {
    return sort;
  }
  List<Order> mappedOrders = new ArrayList<>();
  for (Order order : sort) {
    ColumnName columnName = ColumnName.from(order.getProperty());
    Field field = createPropertyField(entity, columnName);
    Order mappedOrder = getCqlIdentifier(columnName, field)
        .map(cqlIdentifier -> new Order(order.getDirection(), cqlIdentifier.toCql())).orElse(order);
    mappedOrders.add(mappedOrder);
  }
  return Sort.by(mappedOrders);
}

代码示例来源:origin: pramoth/specification-with-projection

@Override
public <R> Optional<R> findOne(Specification<T> spec, Class<R> projectionType) {
  TypedQuery<T> query = getQuery(spec, Sort.unsorted());
  try {
    T result = query.getSingleResult();
    return Optional.ofNullable(projectionFactory.createProjection(projectionType, result));
  } catch (NoResultException e) {
    return Optional.empty();
  }
}

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

/**
 * Creates the actual query object.
 *
 * @return
 */
public T createQuery() {
  return createQuery(parameters.map(ParameterAccessor::getSort) //
      .orElse(Sort.unsorted()));
}

代码示例来源:origin: org.eclipse.hawkbit/hawkbit-repository-jpa

@Override
public Optional<Action> findOldestActiveActionByTarget(final String controllerId) {
  if (!actionRepository.activeActionExistsForControllerId(controllerId)) {
    return Optional.empty();
  }
  // used in favorite to findFirstByTargetAndActiveOrderByIdAsc due to
  // DATAJPA-841 issue.
  return actionRepository.findFirstByTargetControllerIdAndActive(new Sort(Direction.ASC, "id"), controllerId,
      true);
}

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

/**
 * Static factory method to create a {@link Query} using the provided {@link CriteriaDefinition}.
 *
 * @param criteriaDefinitions must not be {@literal null}.
 * @return the {@link Query} for {@link CriteriaDefinition}s.
 */
public static Query query(Iterable<? extends CriteriaDefinition> criteriaDefinitions) {
  Assert.notNull(criteriaDefinitions, "CriteriaDefinitions must not be null");
  List<CriteriaDefinition> collect = stream(criteriaDefinitions.spliterator(), false)
      .collect(Collectors.toList());
  return new Query(collect, Columns.empty(), Sort.unsorted(), Optional.empty(), Optional.empty(),
      Optional.empty(), false);
}

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

@Override
public List<T> findAllById(Iterable<ID> ids) {
  Assert.notNull(ids, "The given Iterable of Id's must not be null!");
  if (!ids.iterator().hasNext()) {
    return Collections.emptyList();
  }
  if (entityInformation.hasCompositeId()) {
    List<T> results = new ArrayList<T>();
    for (ID id : ids) {
      findById(id).ifPresent(results::add);
    }
    return results;
  }
  ByIdsSpecification<T> specification = new ByIdsSpecification<T>(entityInformation);
  TypedQuery<T> query = getQuery(specification, Sort.unsorted());
  return query.setParameter(specification.parameter, ids).getResultList();
}

代码示例来源: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(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-mongodb

@Override
public <S extends T> Flux<S> findAll(Example<S> example) {
  Assert.notNull(example, "Example must not be null!");
  return findAll(example, Sort.unsorted());
}

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

/**
 * Creates a new {@link Sort} for the given properties.
 *
 * @param properties must not be {@literal null}.
 * @return
 */
public static Sort by(String... properties) {
  Assert.notNull(properties, "Properties must not be null!");
  return properties.length == 0 ? Sort.unsorted() : new Sort(properties);
}

代码示例来源: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: org.springframework.cloud/spring-cloud-dataflow-server-core

@Override
public Iterable<D> findAll(Sort sort) {
  Assert.notNull(sort, "sort must not be null");
  Iterator<Sort.Order> iter = sort.iterator();
  String query = findAllQuery + "ORDER BY ";
  while (iter.hasNext()) {
    Sort.Order order = iter.next();
    query = query + order.getProperty() + " " + order.getDirection();
    if (iter.hasNext()) {
      query = query + ", ";
    }
  }
  return jdbcTemplate.query(query, rowMapper);
}

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

/**
 * Creates the actual query object applying the given {@link Sort} parameter. Use this method in case you haven't
 * provided a {@link ParameterAccessor} in the first place but want to apply dynamic sorting nevertheless.
 *
 * @param dynamicSort must not be {@literal null}.
 * @return
 */
public T createQuery(Sort dynamicSort) {
  Assert.notNull(dynamicSort, "DynamicSort must not be null!");
  return complete(createCriteria(tree), tree.getSort().and(dynamicSort));
}

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

DefaultSelectSpecSupport(String table) {
  Assert.hasText(table, "Table name must not be null!");
  this.table = table;
  this.projectedFields = Collections.emptyList();
  this.sort = Sort.unsorted();
  this.page = Pageable.unpaged();
}

代码示例来源:origin: com.orientechnologies/spring-data-orientdb-commons

/**
 * Apply sorting for the given query.
 *
 * @param query the query
 * @param sort the sort
 * @return the string
 */
public static String applySorting(String query, Sort sort) {
  Assert.hasText(query);
  
  if (null == sort || !sort.iterator().hasNext()) {
    return query;
  }
  
  throw new UnsupportedOperationException("Not implemented");
}

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

/**
 * Creates a new unsorted {@link PageRequest}.
 *
 * @param page zero-based page index.
 * @param size the size of the page to be returned.
 * @throws IllegalArgumentException for page requests other than the first page.
 */
public static CassandraPageRequest of(int page, int size) {
  Assert.isTrue(page == 0,
      "Cannot create a Cassandra page request for an indexed page other than the first page (0).");
  return of(page, size, Sort.unsorted());
}

相关文章