Spring Boot中實現(xiàn)分頁查詢的流程一般如下:
public interface UserRepository extends JpaRepository<User, Long> {
Page<User> findAll(Pageable pageable);
}
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public Page<User> getUsers(Pageable pageable) {
return userRepository.findAll(pageable);
}
}
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public ResponseEntity<Page<User>> getUsers(Pageable pageable) {
Page<User> users = userService.getUsers(pageable);
return ResponseEntity.ok(users);
}
}
通過以上流程,就可以實現(xiàn)Spring Boot中的分頁查詢功能。頁面會返回一個Page對象,里面包含了分頁信息(頁數(shù)、每頁大小等)和查詢結果數(shù)據(jù)。