Java Code Examples for org.springframework.data.domain.PageRequest#of()

The following examples show how to use org.springframework.data.domain.PageRequest#of() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpecTest.java    From code with Apache License 2.0 6 votes vote down vote up
/**
 * 分页查询
 */
@Test
public void testPage() {
    Specification<Customer> specification = new Specification<Customer>() {
        @Override
        public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            return cb.like(root.get("custName").as(String.class), "阿里巴%");
        }
    };
    /*
     * 构造分页参数
     * 		Pageable : 接口
     * 			PageRequest实现了Pageable接口,调用构造方法的形式构造
     * 				第一个参数:页码(从0开始)
     * 				第二个参数:每页查询条数
     * Pageable pageable = new PageRequest(0, 5);
     */
    Pageable pageable = PageRequest.of(0, 5);
    
    Page<Customer> page = customerDao.findAll(specification, pageable);

    System.out.println("总记录数"+page.getTotalElements());
    System.out.println("总页数"+page.getTotalPages());
    page.getContent().forEach(System.out::println);

}
 
Example 2
Source File: PageCondition.java    From base-admin with MIT License 5 votes vote down vote up
/**
 * 获取JPA的分页查询对象
 */
public Pageable getPageable() {
    //处理非法页码
    if (page < 0) {
        page = 1;
    }
    //处理非法页面大小
    if (rows < 0) {
        rows = 10;
    }
    return PageRequest.of(page - 1, rows);
}
 
Example 3
Source File: FrontOthersController.java    From stone with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 获取文章rss
 *
 * @return rss
 */
@GetMapping(value = {"feed", "feed.xml", "atom", "atom.xml"}, produces = "application/xml;charset=UTF-8")
@ResponseBody
public String feed() {
    String rssPosts = HaloConst.OPTIONS.get(BlogPropertiesEnum.RSS_POSTS.getProp());
    if (StrUtil.isBlank(rssPosts)) {
        rssPosts = "20";
    }
    //获取文章列表并根据时间排序
    final Sort sort = new Sort(Sort.Direction.DESC, "postDate");
    final Pageable pageable = PageRequest.of(0, Integer.parseInt(rssPosts), sort);
    final Page<Post> postsPage = postService.findPostByStatus(0, PostTypeEnum.POST_TYPE_POST.getDesc(), pageable);
    final List<Post> posts = postsPage.getContent();
    return postService.buildRss(posts);
}
 
Example 4
Source File: PageableExecutionUtil.java    From FEBS-Cloud with Apache License 2.0 5 votes vote down vote up
public static <FEBS> Flux<FEBS> getPages(Query query, QueryRequest request, Class<FEBS> clazz,
                                         ReactiveMongoTemplate template) {
    Sort sort = Sort.by("id").descending();
    if (StringUtils.isNotBlank(request.getField()) && StringUtils.isNotBlank(request.getOrder())) {
        sort = FebsConstant.ORDER_ASC.equals(request.getOrder()) ?
                Sort.by(request.getField()).ascending() :
                Sort.by(request.getField()).descending();
    }
    Pageable pageable = PageRequest.of(request.getPageNum(), request.getPageSize(), sort);
    return template.find(query.with(pageable), clazz);
}
 
Example 5
Source File: PageCondition.java    From springBoot with MIT License 5 votes vote down vote up
/**
 * 获取JPA的分页查询对象
 */
public Pageable getPageable() {
    //处理非法页码
    if (page < 0) {
        page = 1;
    }
    //处理非法页面大小
    if (rows < 0) {
        rows = 10;
    }
    return PageRequest.of(page - 1, rows);
}
 
Example 6
Source File: PageUtil.java    From sk-admin with Apache License 2.0 5 votes vote down vote up
public static Pageable initPage(PageVo page) {

        Pageable pageable;
        int pageNumber = page.getPageNumber();
        int pageSize = page.getPageSize();
        String sort = page.getSort();
        String order = page.getOrder();

        if (pageNumber < 1) {
            pageNumber = 1;
        }
        if (pageSize < 1) {
            pageSize = 10;
        }
        if (StrUtil.isNotBlank(sort)) {
            Sort.Direction d;
            if (StrUtil.isBlank(order)) {
                d = Sort.Direction.DESC;
            } else {
                d = Sort.Direction.valueOf(order.toUpperCase());
            }
            Sort s = new Sort(d, sort);
            pageable = PageRequest.of(pageNumber - 1, pageSize, s);
        } else {
            pageable = PageRequest.of(pageNumber - 1, pageSize);
        }
        return pageable;
    }
 
Example 7
Source File: MemberController.java    From Spring-Boot-Book with Apache License 2.0 5 votes vote down vote up
@GetMapping("")

    public ModelAndView userlist(@RequestParam(value = "start", defaultValue = "0") Integer start,
                                 @RequestParam(value = "limit", defaultValue = "5") Integer limit) {
        start = start < 0 ? 0 : start;
        Sort sort = new Sort(Sort.Direction.DESC, "id");
       // Pageable pageable = new PageRequest(start, limit, sort);
        Pageable pageable = PageRequest.of(start, limit, sort);
        Page<User> page = userRepository.findAll(pageable);
        ModelAndView mav = new ModelAndView("admin/member/list");
        mav.addObject("page", page);
        return mav;
    }
 
Example 8
Source File: PageController.java    From stone with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 图库管理
 *
 * @param model model
 * @param page  当前页码
 * @param size  每页显示的条数
 *
 * @return 模板路径admin/admin_page_gallery
 */
@GetMapping(value = "/galleries")
public String gallery(Model model,
                      @RequestParam(value = "page", defaultValue = "0") Integer page,
                      @RequestParam(value = "size", defaultValue = "18") Integer size) {
    final Sort sort = new Sort(Sort.Direction.DESC, "galleryId");
    final Pageable pageable = PageRequest.of(page, size, sort);
    final Page<Gallery> galleries = galleryService.findAll(pageable);
    model.addAttribute("galleries", galleries);
    return "admin/admin_page_gallery";
}
 
Example 9
Source File: SystemGroupDataController.java    From yshopmall with Apache License 2.0 5 votes vote down vote up
@Log("查询数据配置")
@ApiOperation(value = "查询数据配置")
@GetMapping(value = "/yxSystemGroupData")
@PreAuthorize("@el.check('admin','YXSYSTEMGROUPDATA_ALL','YXSYSTEMGROUPDATA_SELECT')")
public ResponseEntity getYxSystemGroupDatas(YxSystemGroupDataQueryCriteria criteria,
                                            Pageable pageable){
    Sort sort = new Sort(Sort.Direction.DESC, "sort");
    Pageable pageableT = PageRequest.of(pageable.getPageNumber(),
            pageable.getPageSize(),
            sort);
    return new ResponseEntity(yxSystemGroupDataService.queryAll(criteria,pageableT),HttpStatus.OK);
}
 
Example 10
Source File: AritcleController.java    From Spring-Boot-Book with Apache License 2.0 5 votes vote down vote up
/**
 * Description: 文章列表
 */
@RequestMapping("")
public ModelAndView articlelist(@RequestParam(value = "start", defaultValue = "0") Integer start,
                                @RequestParam(value = "limit", defaultValue = "5") Integer limit) {
    start = start < 0 ? 0 : start;
    Sort sort = Sort.by(Sort.Direction.DESC, "id");
    Pageable pageable = PageRequest.of(start, limit, sort);
    Page<Article> page = articleRepository.findAll(pageable);
    ModelAndView mav = new ModelAndView("article/list");
    mav.addObject("page", page);
    return mav;
}
 
Example 11
Source File: ArticleController.java    From Spring-Boot-Book with Apache License 2.0 5 votes vote down vote up
/**
 * Description: 文章列表
 */
@RequestMapping("")
public ModelAndView articlelist(@RequestParam(value = "start", defaultValue = "0") Integer start,
                                @RequestParam(value = "limit", defaultValue = "5") Integer limit) {
    start = start < 0 ? 0 : start;
    Sort sort = Sort.by(Sort.Direction.DESC, "id");
    Pageable pageable = PageRequest.of(start, limit, sort);
    Page<Article> page = articleRepository.findAll(pageable);
    ModelAndView mav = new ModelAndView("article/list");
    mav.addObject("page", page);
    return mav;
}
 
Example 12
Source File: PageUtil.java    From beihu-boot with Apache License 2.0 4 votes vote down vote up
public static PageRequest of(ltd.beihu.core.web.boot.response.page.Page  page, Sort sort) {
    return PageRequest.of(page.getIndex() - 1, page.getSize(), sort);
}
 
Example 13
Source File: ProjectUserDaoImpl.java    From Qualitis with Apache License 2.0 4 votes vote down vote up
@Override
public List<ProjectUser> findByUsernameAndPermissionAndProjectType(String username, Integer permission, Integer projectType, int page, int size) {
    Sort sort = new Sort(Sort.Direction.ASC, "project");
    Pageable pageable = PageRequest.of(page, size, sort);
    return projectUserRepository.findByUserNameAndPermissionAndProjectType(username, permission, projectType, pageable).getContent();
}
 
Example 14
Source File: Application.java    From code-examples with MIT License 4 votes vote down vote up
@Bean
public CommandLineRunner specificationsDemo(MovieRepository movieRepository) {
    return args -> {

        // create new movies
        movieRepository.saveAll(Arrays.asList(
                new Movie("Troy", "Drama", 7.2, 196, 2004),
                new Movie("The Godfather", "Crime", 9.2, 178, 1972),
                new Movie("Invictus", "Sport", 7.3, 135, 2009),
                new Movie("Black Panther", "Action", 7.3, 135, 2018),
                new Movie("Joker", "Drama", 8.9, 122, 2018),
                new Movie("Iron Man", "Action", 8.9, 126, 2008)
        ));

        // search movies by `genre`
        MovieSpecification msGenre = new MovieSpecification();
        msGenre.add(new SearchCriteria("genre", "Action", SearchOperation.EQUAL));
        List<Movie> msGenreList = movieRepository.findAll(msGenre);
        msGenreList.forEach(System.out::println);

        // search movies by `title` and `rating` > 7
        MovieSpecification msTitleRating = new MovieSpecification();
        msTitleRating.add(new SearchCriteria("title", "black", SearchOperation.MATCH));
        msTitleRating.add(new SearchCriteria("rating", 7, SearchOperation.GREATER_THAN));
        List<Movie> msTitleRatingList = movieRepository.findAll(msTitleRating);
        msTitleRatingList.forEach(System.out::println);

        // search movies by release year < 2010 and rating > 8
        MovieSpecification msYearRating = new MovieSpecification();
        msYearRating.add(new SearchCriteria("releaseYear", 2010, SearchOperation.LESS_THAN));
        msYearRating.add(new SearchCriteria("rating", 8, SearchOperation.GREATER_THAN));
        List<Movie> msYearRatingList = movieRepository.findAll(msYearRating);
        msYearRatingList.forEach(System.out::println);

        // search movies by watch time >= 150 and sort by `title`
        MovieSpecification msWatchTime = new MovieSpecification();
        msWatchTime.add(new SearchCriteria("watchTime", 150, SearchOperation.GREATER_THAN_EQUAL));
        List<Movie> msWatchTimeList = movieRepository.findAll(msWatchTime, Sort.by("title"));
        msWatchTimeList.forEach(System.out::println);

        // search movies by title <> 'white' and paginate results
        MovieSpecification msTitle = new MovieSpecification();
        msTitle.add(new SearchCriteria("title", "white", SearchOperation.NOT_EQUAL));

        Pageable pageable = PageRequest.of(0, 3, Sort.by("releaseYear").descending());
        Page<Movie> msTitleList = movieRepository.findAll(msTitle, pageable);

        msTitleList.forEach(System.out::println);
    };
}
 
Example 15
Source File: RoleServiceImpl.java    From sureness with Apache License 2.0 4 votes vote down vote up
@Override
public Page<AuthRoleDO> getPageRole(Integer currentPage, Integer pageSize) {
    PageRequest pageRequest = PageRequest.of(currentPage, pageSize);
    return authRoleDao.findAll(pageRequest);
}
 
Example 16
Source File: UserSpecPermissionDaoImpl.java    From Qualitis with Apache License 2.0 4 votes vote down vote up
@Override
public List<UserSpecPermission> findAllUserSpecPermission(int page, int size) {
    Sort sort = new Sort(Sort.Direction.ASC, "id");
    Pageable pageable = PageRequest.of(page, size, sort);
    return userSpecPermissionRepository.findAll(pageable).getContent();
}
 
Example 17
Source File: PermissionDaoImpl.java    From Qualitis with Apache License 2.0 4 votes vote down vote up
@Override
public List<Permission> findAllPermission(int page, int size) {
    Sort sort = new Sort(Sort.Direction.ASC, "id");
    Pageable pageable = PageRequest.of(page, size, sort);
    return permissionRepository.findAll(pageable).getContent();
}
 
Example 18
Source File: EsProductServiceImpl.java    From BigDataPlatform with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Page<EsProduct> search(String keyword, Integer pageNum, Integer pageSize) {
    Pageable pageable = PageRequest.of(pageNum, pageSize);
    return productRepository.findByNameOrSubTitleOrKeywords(keyword, keyword, keyword, pageable);
}
 
Example 19
Source File: EsProductServiceImpl.java    From mall-learning with Apache License 2.0 4 votes vote down vote up
@Override
public Page<EsProduct> search(String keyword, Integer pageNum, Integer pageSize) {
    Pageable pageable = PageRequest.of(pageNum, pageSize);
    return productRepository.findByNameOrSubTitleOrKeywords(keyword, keyword, keyword, pageable);
}
 
Example 20
Source File: Temp.java    From acelera-dev-sp-2019 with Apache License 2.0 2 votes vote down vote up
public static void main(String[] args) {
    PageRequest request = PageRequest.of(1, 20);

}