你可以使用Spring Data JPA的'Pageable'来进行分页查询。以下是一个使用Spring Boot和Spring Data JPA进行分页查询的示例代码:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @GetMapping("/users")
    public Page<User> getUsers(Pageable pageable) {
        return userRepository.findAll(pageable);
    }
}

在上面的代码中,'UserRepository'是一个继承自'JpaRepository'的接口,它提供了各种查询方法。'findAll'方法接受一个'Pageable'参数,可以通过该参数来指定查询的页数、每页的大小等。

当你发送GET请求到'/api/users'时,Spring Boot将会调用'getUsers'方法来处理请求,并将返回的结果进行分页。

你可以在请求中添加查询参数来指定页数和每页的大小,例如:'/api/users?page=0&size=200'将会返回第一页的200条数据。

注意,如果你的表中有50000条数据,每次查询200条,总共需要进行250次查询才能查询完所有数据。你可以通过循环查询的方式来实现,或者使用异步方法来提高查询的效率。

SpringBoot分页查询50000条数据:使用Spring Data JPA实现高效分页

原文地址: https://www.cveoy.top/t/topic/pYje 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录