gpt4 book ai didi

java - 如何进行分页请求 - Spring boot

转载 作者:行者123 更新时间:2023-11-30 06:06:32 28 4
gpt4 key购买 nike

在我的系统(Spring boot 项目)中,我需要向每 350 个搜索我的数据的人发出请求,我需要寻呼并发送。我寻找了很多方法来做到这一点,发现很多都是用 JPA 实现的,但我使用的是 Jooq,所以我向用户的工具寻求帮助,他们指导我使用 limit 和 offset 选项。

这是我进行研究、设置 DTO 并最终返回人员列表的方法。

 public static ArrayList getAllPeople(Connection connection) {
ArrayList<peopleDto> peopleList = new ArrayList<>();
DSLContext ctx = null;
peopleDto peopleDto;
try {
ctx = DSL.using(connection, SQLDialect.MYSQL);
Result<Record> result = ctx.select()
.from(people)
.orderBy(people.GNUM)
.offset(0)
.limit(350)
.fetch();

for (Record r : result) {

peopleDto = new peopleDto();
peopleDto.setpeopleID(r.getValue(people.GNUM));
peopleDto.setName(r.get(people.SNAME));
peopleDto.setRM(r.get(people.SRM));
peopleDto.setRG(r.get(people.SRG));
peopleDto.setCertidaoLivro(r.get(people.SCERT));
peopleDto.setCertidaoDistrito(r.get(people.SCERTD));
peopleList.add(peopleDto);
}
} catch (Exception e) {
log.error(e.toString());
} finally {
if (ctx != null) {
ctx.close();
}
}
return peopleList;
}

无限制的搜索会返回 1,400 人。问题是如何发送限制数量,然后返回到此方法以从上次中断的地方继续,直到达到记录总数?

最佳答案

向您的方法提供 Pageable 参数,并从您的方法返回一个 Page。类似于...

public static ArrayList getAllPeople(Connection connection, Pageable pageable) {
ArrayList<peopleDto> peopleList = new ArrayList<>();
DSLContext ctx = null;
peopleDto peopleDto;
try {
ctx = DSL.using(connection, SQLDialect.MYSQL);
Result<Record> result = ctx.select()
.from(people)
.orderBy(people.GNUM)
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();

for (Record r : result) {

peopleDto = new peopleDto();
peopleDto.setpeopleID(r.getValue(people.GNUM));
peopleDto.setName(r.get(people.SNAME));
peopleDto.setRM(r.get(people.SRM));
peopleDto.setRG(r.get(people.SRG));
peopleDto.setCertidaoLivro(r.get(people.SCERT));
peopleDto.setCertidaoDistrito(r.get(people.SCERTD));
peopleList.add(peopleDto);
}
} catch (Exception e) {
log.error(e.toString());
} finally {
if (ctx != null) {
ctx.close();
}
}
return new PageImpl(peopleList, pageable, hereyoushouldQueryTheTotalItemCount());
}

现在您可以对这 350 个用户做一些事情。借助该页面,您现在可以迭代剩余的人员:

if(page.hasNext())
getAllPeople(connection, page.nextPageable());

受到这篇文章的启发Sorting and Pagination with Spring and Jooq

关于java - 如何进行分页请求 - Spring boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51191622/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com