弹簧数据可分页和LIMIT/OFFSET [英] Spring data Pageable and LIMIT/OFFSET

查看:140
本文介绍了弹簧数据可分页和LIMIT/OFFSET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑将我们的传统jpa/dao解决方案迁移到Spring Data.

i'm looking at migrating our traditional jpa/dao solution to Spring Data.

但是,我们的前端之一是SmartGWT,它们的数据绑定组件仅使用限制/偏移量逐步加载数据,这使得难以使用Pageable.

However, one of our front-ends is SmartGWT, and their databound components load data progressively using limit/offset only, making it hard to use Pageable.

这会导致问题,因为不确定是否可以将限制/偏移量最终转换为页码. (取决于用户的滚动方式,屏幕调整大小等).

This causes problems, since it's not certain that the limit/offset can be translated evently into a page number. (it might differ depending on how the user scrolls, screen is resized etc.).

我查看了Slice等,但是找不到在任何地方使用限制/偏移值的方法.

I looked at Slice etc, but wasn't able to find a way to use the limit/offset values anywhere.

想知道某人是否有任何指针吗?理想情况下,我希望继续使用限制/偏移量,但是可以在我的Repository接口中使用它们,而不必像现在一样进行编码并手动设置它们(query.setMaxResults等)

Was wondering if someone has any pointers? Optimally i would like to continue using limit/offset, but use them in my Repository interfaces without having to code an implementation and set them manually like i do now (query.setMaxResults etc.)

为了弄清为什么我有问题-smartgwt组件中的初始数据读取和后续数据读取之间的限制/偏移量可能有所不同.对于listgrid,例如,第一次获取的限制可能设置为89,因为这是屏幕上可见的行数,并且偏移量为0.但是,下一个请求可能具有偏移量89,限制为50,因为那是组件的"datapagesize". "值设置为50,因此这就是我向下滚动时要获取的内容. 如果我在释放之前滚动到很低的位置,则可能会取决于设置,例如取回第159-209行.基本上,不能保证偏移量是任何东西的倍数.很难将偏移量17(限制为5)转换为一页.

To clarify why i have issues - The limit/offset might differ between the initial and subsequent data fetches in a smartgwt component. For a listgrid, the first fetch might have limit set to 89 for example, since that's the amount of rows visible on screen, and offset 0. The next request, though, might have offset 89, and limit 50 since that's the component's "datapagesize" value to 50, so that's what it'll fetch when i scroll down. If i scroll to far down before releasing, it might, depending on the settings, fetch for example rows 159-209 instead. Basically, there's no guarantee that the offset is a multiple of anything. It's hard to convert offset 17, limit 5 to a page.

推荐答案

Pagebale实现 do 使用limitoffset创建分页.构造函数中的page值用于在AbstractPageRequestgetOffset方法中生成偏移值:

Pagebale implementations do use limit and offset to create pagination. The page value in the constructor is used to generate the offset value in the AbstractPageRequest class getOffset method:

public int getOffset() {
    return this.page * this.size;
}

如果只想使用limitoffset并从混合中丢弃page参数,请查看

If you want to only use limit and offset and discard the page parameter from the mix, take a look at the Spring Data documentation on web support, particularly the part about overriding the default configuration. You could create your own implementation of Pageable that takes limit and offset as constructor arguments and the implement your own HandlerMethodArgumentResolver to replace the standard PageRequest resolving. Quick-and-dirty example:

可分页实施

public class BetterPageRequest implements Pageable {

    public BetterPageRequest(int limit, int offset){
        this.limit = limit;
        this.offset = offset;
    }

    // Other method implementations

}

HandlerMethodArgumentResolver实现

public class BetterPageableResolver implements HandlerMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter parameter){
        return Pageable.class.equals(parameter.getParameterType());
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container, NativeWebRequest request, WebDataBinderFactory factory){
        Map<String,String[]> params = request.getParameterMap();
        return new BetterPageRequest(params.get('limit')[0], params.get('offset')[0]);
    }

}

这篇关于弹簧数据可分页和LIMIT/OFFSET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆