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

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

本文整理了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

  1. /**
  2. * Creates new {@link Sort} with potentially unsafe {@link Order} instances.
  3. *
  4. * @param properties must not be {@literal null}.
  5. * @return
  6. */
  7. public Sort withUnsafe(String... properties) {
  8. Assert.notEmpty(properties, "Properties must not be empty!");
  9. Assert.noNullElements(properties, "Properties must not contain null values!");
  10. List<Order> orders = new ArrayList<>(properties.length);
  11. for (String property : properties) {
  12. orders.add(new JpaOrder(getDirection(), property, getNullHandling(), isIgnoreCase(), this.unsafe));
  13. }
  14. return Sort.by(orders);
  15. }

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

  1. /**
  2. * Adds a {@link Sort} to the {@link Query} instance.
  3. *
  4. * @param sort
  5. * @return
  6. */
  7. public Query with(Sort sort) {
  8. Assert.notNull(sort, "Sort must not be null!");
  9. if (sort.isUnsorted()) {
  10. return this;
  11. }
  12. sort.stream().filter(Order::isIgnoreCase).findFirst().ifPresent(it -> {
  13. throw new IllegalArgumentException(String.format("Given sort contained an Order for %s with ignore case! "
  14. + "MongoDB does not support sorting ignoring case currently!", it.getProperty()));
  15. });
  16. this.sort = this.sort.and(sort);
  17. return this;
  18. }

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

  1. @Override
  2. public Optional<T> findOne(@Nullable Specification<T> spec) {
  3. try {
  4. return Optional.of(getQuery(spec, Sort.unsorted()).getSingleResult());
  5. } catch (NoResultException e) {
  6. return Optional.empty();
  7. }
  8. }

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

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

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

  1. public Sort getMappedSort(Sort sort, CassandraPersistentEntity<?> entity) {
  2. Assert.notNull(sort, "Sort must not be null");
  3. Assert.notNull(entity, "CassandraPersistentEntity must not be null");
  4. if (!sort.iterator().hasNext()) {
  5. return sort;
  6. }
  7. List<Order> mappedOrders = new ArrayList<>();
  8. for (Order order : sort) {
  9. ColumnName columnName = ColumnName.from(order.getProperty());
  10. Field field = createPropertyField(entity, columnName);
  11. Order mappedOrder = getCqlIdentifier(columnName, field)
  12. .map(cqlIdentifier -> new Order(order.getDirection(), cqlIdentifier.toCql())).orElse(order);
  13. mappedOrders.add(mappedOrder);
  14. }
  15. return Sort.by(mappedOrders);
  16. }

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

  1. @Override
  2. public <R> Optional<R> findOne(Specification<T> spec, Class<R> projectionType) {
  3. TypedQuery<T> query = getQuery(spec, Sort.unsorted());
  4. try {
  5. T result = query.getSingleResult();
  6. return Optional.ofNullable(projectionFactory.createProjection(projectionType, result));
  7. } catch (NoResultException e) {
  8. return Optional.empty();
  9. }
  10. }

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

  1. /**
  2. * Creates the actual query object.
  3. *
  4. * @return
  5. */
  6. public T createQuery() {
  7. return createQuery(parameters.map(ParameterAccessor::getSort) //
  8. .orElse(Sort.unsorted()));
  9. }

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

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

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

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

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

  1. @Override
  2. public List<T> findAllById(Iterable<ID> ids) {
  3. Assert.notNull(ids, "The given Iterable of Id's must not be null!");
  4. if (!ids.iterator().hasNext()) {
  5. return Collections.emptyList();
  6. }
  7. if (entityInformation.hasCompositeId()) {
  8. List<T> results = new ArrayList<T>();
  9. for (ID id : ids) {
  10. findById(id).ifPresent(results::add);
  11. }
  12. return results;
  13. }
  14. ByIdsSpecification<T> specification = new ByIdsSpecification<T>(entityInformation);
  15. TypedQuery<T> query = getQuery(specification, Sort.unsorted());
  16. return query.setParameter(specification.parameter, ids).getResultList();
  17. }

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

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

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

  1. @Override
  2. public Message<?> pollMessageFromGroup(Object groupId) {
  3. Assert.notNull(groupId, "'groupId' must not be null");
  4. Sort sort = Sort.by(MessageDocumentFields.LAST_MODIFIED_TIME, MessageDocumentFields.SEQUENCE);
  5. if (this.priorityEnabled) {
  6. sort = Sort.by(Sort.Direction.DESC, MessageDocumentFields.PRIORITY).and(sort);
  7. }
  8. Query query = groupIdQuery(groupId).with(sort);
  9. MessageDocument document = this.mongoTemplate.findAndRemove(query, MessageDocument.class, this.collectionName);
  10. Message<?> message = null;
  11. if (document != null) {
  12. message = document.getMessage();
  13. }
  14. return message;
  15. }

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

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

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

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

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

  1. /**
  2. * Translates {@link Sort} orders from Jackson-mapped field names to {@link PersistentProperty} names. Properties
  3. * that cannot be resolved are dropped.
  4. *
  5. * @param input must not be {@literal null}.
  6. * @param rootEntity must not be {@literal null}.
  7. * @return {@link Sort} with translated field names or {@literal null} if translation dropped all sort fields.
  8. */
  9. public Sort translateSort(Sort input, PersistentEntity<?, ?> rootEntity) {
  10. Assert.notNull(input, "Sort must not be null!");
  11. Assert.notNull(rootEntity, "PersistentEntity must not be null!");
  12. List<Order> filteredOrders = new ArrayList<Order>();
  13. for (Order order : input) {
  14. List<String> iteratorSource = new ArrayList<String>();
  15. Matcher matcher = SPLITTER.matcher("_" + order.getProperty());
  16. while (matcher.find()) {
  17. iteratorSource.add(matcher.group(1));
  18. }
  19. String mappedPropertyPath = getMappedPropertyPath(rootEntity, iteratorSource);
  20. if (mappedPropertyPath != null) {
  21. filteredOrders.add(order.withProperty(mappedPropertyPath));
  22. }
  23. }
  24. return filteredOrders.isEmpty() ? Sort.unsorted() : Sort.by(filteredOrders);
  25. }

代码示例来源:origin: org.springframework.cloud/spring-cloud-dataflow-server-core

  1. @Override
  2. public Iterable<D> findAll(Sort sort) {
  3. Assert.notNull(sort, "sort must not be null");
  4. Iterator<Sort.Order> iter = sort.iterator();
  5. String query = findAllQuery + "ORDER BY ";
  6. while (iter.hasNext()) {
  7. Sort.Order order = iter.next();
  8. query = query + order.getProperty() + " " + order.getDirection();
  9. if (iter.hasNext()) {
  10. query = query + ", ";
  11. }
  12. }
  13. return jdbcTemplate.query(query, rowMapper);
  14. }

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

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

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

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

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

  1. /**
  2. * Apply sorting for the given query.
  3. *
  4. * @param query the query
  5. * @param sort the sort
  6. * @return the string
  7. */
  8. public static String applySorting(String query, Sort sort) {
  9. Assert.hasText(query);
  10. if (null == sort || !sort.iterator().hasNext()) {
  11. return query;
  12. }
  13. throw new UnsupportedOperationException("Not implemented");
  14. }

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

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

相关文章