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());
}
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());
}
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());
}
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());
}
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());
}
Aggregations