springboot 某个表有50000条数据每次分页查询200条
你可以使用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次查询才能查询完所有数据。你可以通过循环查询的方式来实现,或者使用异步方法来提高查询的效率
原文地址: https://www.cveoy.top/t/topic/i0b7 著作权归作者所有。请勿转载和采集!