java - 使用 Java 时如何在 Spanner 中实现分页
问题描述
我有以下代码,其中有 200 条记录。我需要在此实现分页。
Statement.Builder buildStatement = Statement.newBuilder(sql);
List<EmployeeSpanner> spannerList= this.spannerTemplate
.query(EmployeeSpanner.class, buildStatement.build(), null);
SQL 是基于多个逻辑和条件创建的,因此我不能使用类似findAll
.
这将返回 200 条记录,我们需要通过分页将其发送到 UI,以便在第一页仅显示 25 条记录,在第二页显示 25 条,依此类推。
当为下面的 OLTP 完成此操作时,我们会这样做,但对于 Google 扳手,我需要类似的东西。
int countset= (int) (pageable.getOffset() > countOfRecord? 0 : pageable.getOffset());
query.setFirstResult(countset);
query.setMaxResults(pageable.getPageSize());
return new PageImpl<>(query.getResultList(), pageable, countOfRecord);
解决方案
要实现分页查询,您需要SpannerQueryOptions
使用 alimit
和 anoffset
进行配置,然后将其传递给查询。
SpannerQueryOptions spannerQueryOptions = new SpannerQueryOptions().setLimit(25L).setOffset(page * 25L);
Statement.Builder buildStatement = Statement.newBuilder(sql);
List<EmployeeSpanner> spannerList= this.spannerTemplate.query(EmployeeSpanner.class, buildStatement.build(), spannerQueryOptions);
推荐阅读
- asp.net - 如何获取为页面控件生成的唯一 ID 前缀
- node.js - 即使设置了自定义 usernameField,Passport.js 也会使用本地身份验证策略引发“缺少凭据”错误
- c - 当 inet_pton 失败但 getaddrinfo 成功时?
- python-3.x - Discord.py - Asyncio 超时无法正常工作
- php - PHP preg_replace 多行指南
- maven - 使用 maven javafx 嵌入 derby db 的 jpa
- php - PHP 创建函数回文
- r - 如何正确使用 lapply 和 Bloomberg Rblpapi 包来提取多组代码
- python - 计算 MNIST 数据库灰度图像的密度
- python - 我需要使用 pyodbc 模块才能连接到 SQL-server 但我不能使用 SQL-SERVER 驱动程序