Search in sources :

Example 31 with Pageable

use of org.springframework.data.domain.Pageable in project tutorials-java by Artister.

the class ApplicationTests method specificationTest.

/**
 * 1) 分页
 * 2) 排序
 * 3) 条件: age > 50
 */
@Test
public void specificationTest() {
    Sort.Order order = new Sort.Order(Sort.Direction.ASC, "id");
    Sort sort = new Sort(order);
    Specification<User> specification = new Specification<User>() {

        /**
         * @param root  查询的类型-User
         * @param criteriaQuery 添加查询条件
         * @param criteriaBuilder 构建Predicate
         * @return
         */
        @Override
        public Predicate toPredicate(// 查询的实体类-根对象
        Root<User> root, // 查询的语句
        CriteriaQuery<?> criteriaQuery, // 
        CriteriaBuilder criteriaBuilder) {
            // root (User (age))--->从root到age就是这个path
            Path path = root.get("age");
            // 就是条件
            return criteriaBuilder.gt(path, 50);
        }
    };
    Pageable pageable = new PageRequest(0, 5, sort);
    Page<User> Users = jpaRepository.findAll(specification, pageable);
    out.println("总页数" + Users.getTotalPages());
    out.println("总记录数" + Users.getTotalElements());
    out.println("当前第几页" + Users.getNumber() + 1);
    out.println("当前页面的集合" + Users.getContent());
    out.println("当前页面的记录数" + Users.getNumberOfElements());
}
Also used : User(org.ko.web.domain.User) Specification(org.springframework.data.jpa.domain.Specification) PageRequest(org.springframework.data.domain.PageRequest) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) Test(org.junit.Test)

Example 32 with Pageable

use of org.springframework.data.domain.Pageable in project tutorials-java by Artister.

the class ApplicationTests method sortTest.

@Test
public void sortTest() {
    Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");
    Sort sort = new Sort(order);
    /**
     * @see PageRequest
     * page 从0开始
     * size 大小
     */
    Pageable pageable = new PageRequest(0, 5, sort);
    Page<User> Users = pagingSortRepository.findAll(pageable);
    out.println("总页数" + Users.getTotalPages());
    out.println("总记录数" + Users.getTotalElements());
    out.println("当前第几页" + Users.getNumber() + 1);
    out.println("当前页面的集合" + Users.getContent());
    out.println("当前页面的记录数" + Users.getNumberOfElements());
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Pageable(org.springframework.data.domain.Pageable) User(org.ko.web.domain.User) Sort(org.springframework.data.domain.Sort) Test(org.junit.Test)

Example 33 with Pageable

use of org.springframework.data.domain.Pageable in project irida by phac-nml.

the class CRUDServiceImplTest method testSearch.

@Test
@SuppressWarnings("unchecked")
public void testSearch() {
    int page = 1;
    int size = 1;
    Direction order = Direction.ASC;
    Page<IdentifiableTestEntity> idPage = new PageImpl<>(Lists.newArrayList(new IdentifiableTestEntity(), new IdentifiableTestEntity()));
    when(crudRepository.findAll(any(Specification.class), any(Pageable.class))).thenReturn(idPage);
    Page<IdentifiableTestEntity> search = crudService.search(IdentifiableTestEntitySpecification.search(), page, size, order);
    assertEquals(2, search.getTotalElements());
    ArgumentCaptor<Pageable> pageArgument = ArgumentCaptor.forClass(Pageable.class);
    verify(crudRepository).findAll(any(Specification.class), pageArgument.capture());
    // ensure a created date sort property is set
    Pageable pagable = pageArgument.getValue();
    Order sort = pagable.getSort().iterator().next();
    assertEquals("createdDate", sort.getProperty());
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) Order(org.springframework.data.domain.Sort.Order) IdentifiableTestEntity(ca.corefacility.bioinformatics.irida.utils.model.IdentifiableTestEntity) Pageable(org.springframework.data.domain.Pageable) IdentifiableTestEntitySpecification(ca.corefacility.bioinformatics.irida.utils.model.IdentifiableTestEntitySpecification) Specification(org.springframework.data.jpa.domain.Specification) Direction(org.springframework.data.domain.Sort.Direction) Test(org.junit.Test)

Example 34 with Pageable

use of org.springframework.data.domain.Pageable in project irida by phac-nml.

the class CRUDServiceImplTest method testSearchSortEmptyString.

@Test
@SuppressWarnings("unchecked")
public void testSearchSortEmptyString() {
    int page = 1;
    int size = 1;
    Direction order = Direction.ASC;
    Page<IdentifiableTestEntity> idPage = new PageImpl<>(Lists.newArrayList(new IdentifiableTestEntity(), new IdentifiableTestEntity()));
    when(crudRepository.findAll(any(Specification.class), any(Pageable.class))).thenReturn(idPage);
    Page<IdentifiableTestEntity> search = crudService.search(IdentifiableTestEntitySpecification.search(), page, size, order, "");
    assertEquals(2, search.getTotalElements());
    ArgumentCaptor<Pageable> pageArgument = ArgumentCaptor.forClass(Pageable.class);
    verify(crudRepository).findAll(any(Specification.class), pageArgument.capture());
    // ensure a created date sort property is set
    Pageable pagable = pageArgument.getValue();
    Order sort = pagable.getSort().iterator().next();
    assertEquals("createdDate", sort.getProperty());
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) Order(org.springframework.data.domain.Sort.Order) IdentifiableTestEntity(ca.corefacility.bioinformatics.irida.utils.model.IdentifiableTestEntity) Pageable(org.springframework.data.domain.Pageable) IdentifiableTestEntitySpecification(ca.corefacility.bioinformatics.irida.utils.model.IdentifiableTestEntitySpecification) Specification(org.springframework.data.jpa.domain.Specification) Direction(org.springframework.data.domain.Sort.Direction) Test(org.junit.Test)

Example 35 with Pageable

use of org.springframework.data.domain.Pageable in project springBoot-learn-demo by nbfujx.

the class UserRepositoryTest method test3.

@Test
public void test3() throws Exception {
    Pageable pageable = new PageRequest(0, 10);
    Page<User> u1 = userRepository.findByNameAndAgeRange("kaka", 50, pageable);
    this.logger.info(u1.toString());
    Page<User> u2 = userRepository.findByNameAndAgeRange2("kaka", 0, 50, pageable);
    this.logger.info(u2.toString());
    Page<User> u3 = userRepository.findByNameAndAgeRange3("kaka", 0, 50, pageable);
    this.logger.info(u3.toString());
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Pageable(org.springframework.data.domain.Pageable) User(com.goku.demo.model.User) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

Pageable (org.springframework.data.domain.Pageable)185 PageRequest (org.springframework.data.domain.PageRequest)88 Sort (org.springframework.data.domain.Sort)81 Test (org.junit.Test)39 PageImpl (org.springframework.data.domain.PageImpl)28 ArrayList (java.util.ArrayList)18 Collectors (java.util.stream.Collectors)17 List (java.util.List)15 Page (org.springframework.data.domain.Page)15 Autowired (org.springframework.beans.factory.annotation.Autowired)14 GetMapping (org.springframework.web.bind.annotation.GetMapping)12 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)12 Test (org.junit.jupiter.api.Test)11 UUID (java.util.UUID)10 ApiOperation (io.swagger.annotations.ApiOperation)9 Calendar (java.util.Calendar)9 java.util (java.util)8 Lists (com.google.common.collect.Lists)7 Map (java.util.Map)7 Transactional (org.springframework.transaction.annotation.Transactional)6