Spring Boot中实现分页查询的流程一般如下:
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);
}
}
通过以上流程,就可以实现Spring Boot中的分页查询功能。页面会返回一个Page对象,里面包含了分页信息(页数、每页大小等)和查询结果数据。