决定不使用 SQL 对图书列表进行分页的策略 [英] Deciding on a strategy for paginating Book listings without SQL

查看:39
本文介绍了决定不使用 SQL 对图书列表进行分页的策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ArrayListBook 从不同的 Merchant 中提取并在 Java 中排序,这取决于用户偏好,根据价格或客户评论:

I have an ArrayList of Books pulled from different Merchants and sorted in Java, depending on user preferences, according to price or customer reviews:

List<Book> books = new ArrayList<Book>();

这要求我始终在内存中保存大量数据作为 Java 对象存储.现在我需要将这些数据分页到跨越多个网页的列表中,并允许用户点击一个带编号的页面链接以跳转到该数据段,那么最好的方法是什么?

This requires me to keep a large chunk of data in memory stored as Java objects at all times. Now that I need to paginate this data into listings that span multiple web pages and allow a user to click on a numbered page link to hop to that segment of the data, what's the best way to do this?

我的想法是 每页 25 个图书列表,而不是使用超链接将表单数据作为 URL 参数的 GET 请求提交,页码超链接将简单地重新提交表单,将请求的页码作为附加表单POST 参数传递.

My idea was to have maybe 25 book listings per page and rather than use hyperlinks that submit the form data as a GET request of URL parameters, the page number hyperlinks would simply resubmit the form, passing the requested page number as an additional form POST parameter.

<input type="hidden" id="pageNumber" value="0">
<a href="#" onClick="pageNumber=5; this.form.submit()">Page 5</a>

在这种情况下,第 5 页将只是一组 25 条记录,从 ArrayList 中的第 125 个(5 * 25)记录开始,到 ArrayList 中的第 149 条记录结束代码>.

In that case page 5 would simply be a set of 25 records starting at the 125th (5 * 25) record in the ArrayList and ending at the 149th record in the ArrayList.

有没有更好的方法来做到这一点?

Is there a better way to do this?

推荐答案

重构您的应用程序以让例如Hibernate 从底层数据库中提取数据.

Refactor your application to let e.g. Hibernate pull out data from the underlying database.

Hibernate 可以完成所有的排序和分页,而您无需始终将其全部保存在内存中.

Hibernate can do all the sorting and pagination without you having to keep it all in memory at all times.

这篇关于决定不使用 SQL 对图书列表进行分页的策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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