用于存储过程的Spring Boot分页

时间:2018-08-08 10:04:10

标签: spring-mvc spring-boot pagination spring-data-jpa

在我的项目中,我有一个控制器类,其中有2种方法

方法1

@RequestMapping("/usersa")
public String apage(Model model, @PageableDefault(size = 3) Pageable pageable) {
    Page<TblEmployee> pageOfUsers = usersService.getAllUsers(pageable);
    model.addAttribute("number", pageOfUsers.getNumber());
    model.addAttribute("totalPages", pageOfUsers.getTotalPages());
    model.addAttribute("totalElements", pageOfUsers.getTotalElements());
    model.addAttribute("size", pageOfUsers.getSize());
    model.addAttribute("users", pageOfUsers.getContent());
    System.out.println("Content Size "+pageOfUsers.getContent().size());
    return "usersa";
}

方法2

@RequestMapping("/users")
public String apage(Model model, @PageableDefault(size = 3) Pageable pageable) {
    Page<Object[]> pageOfUsers = usersService.getUserWithRole(pageable);
    model.addAttribute("number", pageOfUsers.getNumber());
    model.addAttribute("totalPages", pageOfUsers.getTotalPages());
    model.addAttribute("totalElements", pageOfUsers.getTotalElements());
    model.addAttribute("size", pageOfUsers.getSize());
    model.addAttribute("users", pageOfUsers.getContent());
    System.out.println("Content Size "+pageOfUsers.getContent().size());
    return "users";
}

在方法1中,usersService.getAllUsers(pageable)从PagingAndSortingRepository的findAll(Pageable pageable)返回Page,当我打印pageOfUsers.getContent().size()时,得到的输出为3,这与预期的一样。

在方法2中,usersService.getUserWithRole(pageable)从存储过程中返回Page,但是当我打印pageOfUsers.getContent().size()时,输出为12,这是记录的总数。为什么?有什么区别?

0 个答案:

没有答案