用于大表大小的 Java wicket dataprovider [英] Java wicket dataprovider for large tables size

查看:26
本文介绍了用于大表大小的 Java wicket dataprovider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为带有 MySQL 数据库的 EJB3 java 应用程序开发检票口前端.该网站主要是提交表格和显示表格.对于这些表,我更喜欢创建 IDataProviders,它与 AjaxPagingNavigation 配合得非常好.但是我有一些非常大的表,并且 IDataProvider 的大小函数实现起来有点棘手.通常我对查询使用计数,这在大多数情况下表现良好,但我有几个大表,其中计数花费的时间太长(3s+).现在我可以使用一些选项,但我对使用其中任何一个都不太满意,所以我问,有人知道为大表实现 size() 函数的优雅解决方案吗?

I'm developing a wicket front-end for an EJB3 java application with MySQL database. The website mostly is submitting forms and showing tables. For these tables I prefer to create IDataProviders which work very nice with AjaxPagingNavigation. But I have a few tables which are quite large and the size function of the IDataProvider becomes a bit tricky to implement. Usually I use a count for the query and this performs good in most cases but I have a few large table where a count takes too long (3s+). Now there are a few options I can use but I'm not really happy using any of these so that's why I ask, does anyone know an elegant solution for implementing the size() function for a large table?

我在 Google 上找到的第一个选项是使用带有限制结果的子查询并执行计数.但是因为我主要使用命名查询和 EJB3,所以感觉很hackish,如果有什么变化就很难维护.

The first option I found on Google was use a subquery with limit results an perform a count a that. But since I mainly use named queries and EJB3 it feels hackish and it can be hard to maintain if something changes.

第二个选项是使用 ListDataView 并只对表进行有限查询,而无需进行大计数查询.到目前为止,这个解决方案是我最喜欢的,但不是最好的,因为它(几乎)总是获得最大数量的记录.我还必须在存储此 List 对象或在请求之间再次查询数据库之间做出选择.

The second option is use a ListDataView and just do a limited query on table and omit the need for a large count query. This solution is my favorite so far but is not the nicest since it (almost) always gets the maximum amount of records. I also have to chose between storing this List object or query the database again between requests.

我发现的最后一个选项是欺骗大小选项.我还没有实现这个,但我可以让 IDataProvider 的 size() 函数返回类似 pagesize + 1 的东西.这会带来一些其他问题,比如有一个 pagesize 的记录数.有检查可以捕获这些,但这也很麻烦.

The last option I found was spoofing the size option. I havn't implemented this but I could make the size() function of the IDataProvider return something like pagesize + 1. This bring a few other problems like having a pagesized number of records. There are checks to catch these but this is also messy.

有人知道在 wicket 中使用 IDataProvider 来显示大型数据库表的分页表的优雅解决方案吗?

Does someone know an elegant solution to use an IDataProvider in wicket for showing paged tables of large database tables?

非常感谢,马丁

推荐答案

我已经用数据提供程序实现了网格视图,它只需要迭代器,不需要大小信息——它叫做 IterableGridView

I've implemented grid view with data provider which requires only iterator, without size information - its called IterableGridView

这是代码:https://github.com/maciejmiklas/cyclop/tree/master/cyclop-wicket-components

Iterable Grid View 基于 Wicket 的 GridView,但它不适用于 IDataProvider 但与IterableDataProvider.这个新的数据提供者只依赖于普通的 java 迭代器——不需要大小信息,也不需要为每个页面创建范围迭代器.

Iterable Grid View is based on Wicket's GridView, however it does not work with IDataProvider but with IterableDataProvider. This new data provider relies only on plain java iterator - size information is not needed, and there is also no need to create range iterators for each page.

final List<String> myGridData = new ArrayList<>();
myGridData.add("value 1");
myGridData.add("value 2");

IterableDataProvider<String> iterableDataProvider = new IterableDataProvider<String>(10) {
    @Override
    protected Iterator<String> iterator() {
        return myGridData.iterator();
    }

    @Override
    public IModel<String> model(String s) {
        return Model.of(s);
    }

    @Override
    public void detach() {
    }
};

IterableGridView<String> myGrid = new IterableGridView<String>("myGrid", iterableDataProvider) {
    @Override
    protected void populateEmptyItem(Item<String> item) {
        item.add(new Label("myValue"));
    }

    @Override
    protected void populateItem(Item<String> item) {
        item.add(new Label("myValue", item.getModelObject()));
    }
};

add(myGrid);

myGrid.setItemsPerPage(10);

// you have to use custom pager and not AjaxPagingNavigator
IterablePagingNavigator pager = new IterablePagingNavigator("rowNamesListPager", rowNamesList);
resultTable.add(pager);

这篇关于用于大表大小的 Java wicket dataprovider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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