【问题标题】:How to do paged request - Spring boot如何进行分页请求 - Spring Boot
【发布时间】:2018-12-13 22:54:40
【问题描述】:

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

这是我进行研究的方法,我设置了我的 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 人。 问题是我如何发送限制数然后返回到这个方法继续我上次离开的地方,直到我达到记录的总值?

【问题讨论】:

  • 看看这篇文章以及如何使用查询结果创建一个新的pageimpl petrikainulainen.net/programming/jooq/…
  • 已经试过了,作者用了折旧的方法,没用
  • 我认为带有 PageImpl 的部分仍然有效,不是吗?
  • 不相关:如果您将connection 传递给它,您不必关闭您的DSLContext - 在这种情况下它不是资源丰富。

标签: java spring-boot jooq


【解决方案1】:

为您的方法提供一个 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

【讨论】:

  • 感谢支持,但我无法操纵 getAll 方法的返回,我这样做了 PageImpl page = new PageImpl(peopleList); page = (PeopleManager.getAllPeople(con,page.nextPageable()));大小为零,请勿在此处输入: if (page.hasNext()){ PeopleManager.getAllPeopleTest(con,page.nextPageable());
猜你喜欢
  • 2021-07-11
  • 2020-05-10
  • 2020-09-29
  • 1970-01-01
  • 1970-01-01
  • 2018-06-16
  • 2023-03-29
  • 2021-09-19
  • 1970-01-01
相关资源
最近更新 更多