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

x33g5p2x  于2022-01-26 转载在 其他  
字(11.6k)|赞(0)|评价(0)|浏览(242)

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

Pageable.getOffset介绍

[英]Returns the offset to be taken according to the underlying page and page size.
[中]返回根据基础页面和页面大小获取的偏移量。

代码示例

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

/**
 * Configures the {@link Pageable} to use.
 *
 * @param pageable must not be {@literal null}
 * @return
 */
public NearQuery with(Pageable pageable) {
  Assert.notNull(pageable, "Pageable must not be 'null'.");
  if (pageable.isPaged()) {
    this.num = pageable.getOffset() + pageable.getPageSize();
    this.skip = pageable.getOffset();
  }
  return this;
}

代码示例来源:origin: apache/ignite

/**
 * Add a dynamic part of a query for the pagination support.
 *
 * @param sql Builder instance.
 * @param pageable Pageable instance.
 * @return Builder instance.
 */
public static StringBuilder addPaging(StringBuilder sql, Pageable pageable) {
  if (pageable.getSort() != null)
    addSorting(sql, pageable.getSort());
  sql.append(" LIMIT ").append(pageable.getPageSize()).append(" OFFSET ").append(pageable.getOffset());
  return sql;
}

代码示例来源:origin: apache/ignite

/**
 * Add a dynamic part of a query for the pagination support.
 *
 * @param sql Builder instance.
 * @param pageable Pageable instance.
 * @return Builder instance.
 */
public static StringBuilder addPaging(StringBuilder sql, Pageable pageable) {
  if (pageable.getSort() != null)
    addSorting(sql, pageable.getSort());
  sql.append(" LIMIT ").append(pageable.getPageSize()).append(" OFFSET ").append(pageable.getOffset());
  return sql;
}

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

/**
 * Reads the given {@link TypedQuery} into a {@link Page} applying the given {@link Pageable} and
 * {@link Specification}.
 *
 * @param query must not be {@literal null}.
 * @param domainClass must not be {@literal null}.
 * @param spec can be {@literal null}.
 * @param pageable can be {@literal null}.
 * @return
 */
protected <S extends T> Page<S> readPage(TypedQuery<S> query, final Class<S> domainClass, Pageable pageable,
    @Nullable Specification<S> spec) {
  if (pageable.isPaged()) {
    query.setFirstResult((int) pageable.getOffset());
    query.setMaxResults(pageable.getPageSize());
  }
  return PageableExecutionUtils.getPage(query.getResultList(), pageable,
      () -> executeCountQuery(getCountQuery(spec, domainClass)));
}

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

/**
 * Applies the given {@link Pageable} to the given {@link SpringDataMongodbQuery}.
 *
 * @param query
 * @param pageable
 * @return
 */
private SpringDataMongodbQuery<T> applyPagination(SpringDataMongodbQuery<T> query, Pageable pageable) {
  query = query.offset(pageable.getOffset()).limit(pageable.getPageSize());
  return applySorting(query, pageable.getSort());
}

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

/**
   * Binds the parameters to the given query and applies special parameter types (e.g. pagination).
   *
   * @param query must not be {@literal null}.
   * @param values values of method parameters to be assigned to the query parameters.
   */
  Query bindAndPrepare(Query query, Object[] values) {

    Assert.notNull(query, "Query must not be null!");

    ParametersParameterAccessor accessor = new ParametersParameterAccessor(parameters, values);

    Query result = bind(query, values);

    if (!useJpaForPaging || !parameters.hasPageableParameter() || accessor.getPageable().isUnpaged()) {
      return result;
    }

    result.setFirstResult((int) accessor.getPageable().getOffset());
    result.setMaxResults(accessor.getPageable().getPageSize());

    return result;
  }
}

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

/**
 * Applies the given {@link Pageable} to the given {@link JPQLQuery}.
 *
 * @param pageable
 * @param query must not be {@literal null}.
 * @return the Querydsl {@link JPQLQuery}.
 */
public <T> JPQLQuery<T> applyPagination(Pageable pageable, JPQLQuery<T> query) {
  if (pageable.isUnpaged()) {
    return query;
  }
  query.offset(pageable.getOffset());
  query.limit(pageable.getPageSize());
  return applySorting(pageable.getSort(), query);
}

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

@Override
public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) {
  Assert.notNull(pageable, "Pageable must not be null!");
  RedisOperationChain operationChain = createQuery(example);
  KeyValueQuery<RedisOperationChain> query = new KeyValueQuery<>(operationChain);
  Iterable<T> result = keyValueTemplate.find(
      query.orderBy(pageable.getSort()).skip(pageable.getOffset()).limit(pageable.getPageSize()),
      entityInformation.getJavaType());
  long count = operationChain.isEmpty() ? keyValueTemplate.count(entityInformation.getJavaType())
      : keyValueTemplate.count(query, entityInformation.getJavaType());
  List<S> list = new ArrayList<>();
  for (T t : result) {
    list.add((S) t);
  }
  return new PageImpl<>(list, pageable, count);
}

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

@Test
public void testDoReadFirstReadNoResults() throws Exception {
  ArgumentCaptor<PageRequest> pageRequestContainer = ArgumentCaptor.forClass(PageRequest.class);
  when(repository.findAll(pageRequestContainer.capture())).thenReturn(new PageImpl<>(new ArrayList<>()));
  assertNull(reader.doRead());
  Pageable pageRequest = pageRequestContainer.getValue();
  assertEquals(0, pageRequest.getOffset());
  assertEquals(0, pageRequest.getPageNumber());
  assertEquals(1, pageRequest.getPageSize());
  assertEquals("id: ASC", pageRequest.getSort().toString());
}

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

@Test
@SuppressWarnings("serial")
public void testDoReadFirstReadExhausted() throws Exception {
  ArgumentCaptor<PageRequest> pageRequestContainer = ArgumentCaptor.forClass(PageRequest.class);
  final Object result = new Object();
  when(repository.findAll(pageRequestContainer.capture())).thenReturn(new PageImpl<>(new ArrayList<Object>() {{
    add(new Object());
  }})).thenReturn(new PageImpl<>(new ArrayList<Object>(){{
    add(result);
  }})).thenReturn(new PageImpl<>(new ArrayList<>()));
  assertFalse(reader.doRead() == result);
  assertEquals(result, reader.doRead());
  assertNull(reader.doRead());
  Pageable pageRequest = pageRequestContainer.getValue();
  assertEquals(2, pageRequest.getOffset());
  assertEquals(2, pageRequest.getPageNumber());
  assertEquals(1, pageRequest.getPageSize());
  assertEquals("id: ASC", pageRequest.getSort().toString());
}

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

@Test
@SuppressWarnings("serial")
public void testJumpToItem() throws Exception {
  reader.setPageSize(100);
  ArgumentCaptor<PageRequest> pageRequestContainer = ArgumentCaptor.forClass(PageRequest.class);
  when(repository.findAll(pageRequestContainer.capture())).thenReturn(new PageImpl<>(new ArrayList<Object>() {{
    add(new Object());
  }}));
  reader.jumpToItem(485);
  Pageable pageRequest = pageRequestContainer.getValue();
  assertEquals(400, pageRequest.getOffset());
  assertEquals(4, pageRequest.getPageNumber());
  assertEquals(100, pageRequest.getPageSize());
  assertEquals("id: ASC", pageRequest.getSort().toString());
}

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

@Test
@SuppressWarnings("serial")
public void testDoReadFirstReadSecondPage() throws Exception {
  ArgumentCaptor<PageRequest> pageRequestContainer = ArgumentCaptor.forClass(PageRequest.class);
  final Object result = new Object();
  when(repository.findAll(pageRequestContainer.capture())).thenReturn(new PageImpl<>(new ArrayList<Object>() {{
    add(new Object());
  }})).thenReturn(new PageImpl<>(new ArrayList<Object>(){{
    add(result);
  }}));
  assertFalse(reader.doRead() == result);
  assertEquals(result, reader.doRead());
  Pageable pageRequest = pageRequestContainer.getValue();
  assertEquals(1, pageRequest.getOffset());
  assertEquals(1, pageRequest.getPageNumber());
  assertEquals(1, pageRequest.getPageSize());
  assertEquals("id: ASC", pageRequest.getSort().toString());
}

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

@Test
@SuppressWarnings("serial")
public void testDoReadFirstReadResults() throws Exception {
  ArgumentCaptor<PageRequest> pageRequestContainer = ArgumentCaptor.forClass(PageRequest.class);
  final Object result = new Object();
  when(repository.findAll(pageRequestContainer.capture())).thenReturn(new PageImpl<>(new ArrayList<Object>(){{
    add(result);
  }}));
  assertEquals(result, reader.doRead());
  Pageable pageRequest = pageRequestContainer.getValue();
  assertEquals(0, pageRequest.getOffset());
  assertEquals(0, pageRequest.getPageNumber());
  assertEquals(1, pageRequest.getPageSize());
  assertEquals("id: ASC", pageRequest.getSort().toString());
}

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

@Test
public void testDifferentTypes() throws Exception {
  TestRepository differentRepository = mock(TestRepository.class);
  RepositoryItemReader<String> reader = new RepositoryItemReader<>();
  sorts = new HashMap<>();
  sorts.put("id", Direction.ASC);
  reader.setRepository(differentRepository);
  reader.setPageSize(1);
  reader.setSort(sorts);
  reader.setMethodName("findFirstNames");
  ArgumentCaptor<PageRequest> pageRequestContainer = ArgumentCaptor.forClass(PageRequest.class);
  when(differentRepository.findFirstNames(pageRequestContainer.capture())).thenReturn(new PageImpl<>(new ArrayList<String>(){{
    add("result");
  }}));
  assertEquals("result", reader.doRead());
  Pageable pageRequest = pageRequestContainer.getValue();
  assertEquals(0, pageRequest.getOffset());
  assertEquals(0, pageRequest.getPageNumber());
  assertEquals(1, pageRequest.getPageSize());
  assertEquals("id: ASC", pageRequest.getSort().toString());
}

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

long offset = pageable.getOffset();
if (offset > Integer.MAX_VALUE) {
  throw new IllegalArgumentException(String.format("Offset must not be more than %s", Integer.MAX_VALUE));

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

/**
 * Configures the {@link Pageable} to use.
 *
 * @param pageable must not be {@literal null}
 * @return
 */
public NearQuery with(Pageable pageable) {
  Assert.notNull(pageable, "Pageable must not be 'null'.");
  if (pageable.isPaged()) {
    this.num = pageable.getOffset() + pageable.getPageSize();
    this.skip = pageable.getOffset();
  }
  return this;
}

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

/**
 * Applies the given {@link Pageable} to the given {@link SpringDataMongodbQuery}.
 *
 * @param query
 * @param pageable
 * @return
 */
private SpringDataMongodbQuery<T> applyPagination(SpringDataMongodbQuery<T> query, Pageable pageable) {
  query = query.offset(pageable.getOffset()).limit(pageable.getPageSize());
  return applySorting(query, pageable.getSort());
}

相关文章