Java JQuery DataTables 服务器端分页、排序、搜索与 Spring MVC + JPA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21047867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:11:03  来源:igfitidea点击:

JQuery DataTables server-side pagination, sorting, search with Spring MVC + JPA

javaspring-mvcjpadatatables

提问by davioooh

I'm working on the porting of an old .NET web application to Java + Spring MVC + JPA.

我正在将旧的 .NET Web 应用程序移植到 Java + Spring MVC + JPA。

And an old problem is raised up again. In this question JQuery DataTables server-side paginationI was looking for a .NET solution. And finally I found it.

又提出了一个老问题。在这个问题JQuery DataTables 服务器端分页中,我正在寻找一个 .NET 解决方案。最后我找到了。

Now I need to know if something similar exists for Java applications. I googled a bit, but I didn't find nothing realy complete...

现在我需要知道 Java 应用程序是否存在类似的东西。我用谷歌搜索了一下,但我没有找到任何真正完整的东西......

采纳答案by davioooh

I finally solved implementing an ad hocJava library: DataTables Pagination.

我终于解决了实现一个特别的Java 库:DataTables Pagination

Here you find my project jar: https://bitbucket.org/davioooh/datatablepager/

在这里你可以找到我的项目jarhttps: //bitbucket.org/davioooh/datatablepager/



UPDATE (01 Sept. 2016): I recently updated my project to support also v 1.10.x

更新(2016 年 9 月 1 日):我最近更新了我的项目以支持 v 1.10.x



UPDATE (26 May 2017): I moved the project repo to GitHub.

更新(2017 年 5 月 26 日):我将项目存储库移至GitHub

回答by Rob Blake

There is a simple way of doing pagination using JPA. If you look at the javax.persistence.Queryinterface, you'll see that it has setFirstResult()and setMaxResult()methods. Using this in combination allows you to retrieve paged results for your query.

有一种使用 JPA 进行分页的简单方法。如果您查看javax.persistence.Queryinterface,您会看到它具有setFirstResult()setMaxResult()方法。结合使用它允许您检索查询的分页结果。

Some code to illustrate:

一些代码来说明:

Query q = entityManager.createQuery("select f from Foo");
q.setFirstResult(10).setMaxResults(10);
List<Foo> foos = q.getResultList();

This will retrieve 10 results from the query starting at the 10th result.

这将从第 10 个结果开始的查询中检索 10 个结果。

For sorting you have a couple of approaches. You can either change the query dependent upon the field that needs to be sorted e.g

对于排序,您有几种方法。您可以根据需要排序的字段更改查询,例如

entityManager.createQuery("select f from Foo order by f.field1 asc").setFirstResult(10).setMaxResults(10).getResultList();

or

或者

entityManager.createQuery("select f from Foo order by f.field2 asc").setFirstResult(10).setMaxResults(10).getResultList();

Alternatively you could look at the javax.persistence.CriteriaQueryAPI which might make things a bit more maintainable:

或者,您可以查看javax.persistence.CriteriaQueryAPI,这可能会使事情更易于维护:

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Foo> query = cb.createQuery(Foo.class);
Root<Foo> root = query.from(Foo.class);
query.select(root).orderBy(cb.asc(root.get("theFieldToOrderBy")));
entityManager.createQuery(query).setFirstResult(10).setMaxResults(10).getResultList();

Search is a bit more difficult. There isn't a native way to search across all fields of an entity using JPA. You may need to take a look at something like Hibernate Searchto help you with that. It should integrate fairly cleanly with the JPA API assuming you are using Hibernate as your persistence provider of choice.

搜索有点困难。没有使用 JPA 搜索实体的所有字段的本机方法。您可能需要查看Hibernate Search 之类的东西来帮助您。假设您使用 Hibernate 作为您选择的持久性提供程序,它应该与 JPA API 相当干净地集成。

回答by Alan

There is a library of classes recently built named JED that can likely help you with your pagination issue. Though JED doesn't involve JPA, it does perform all manor of CRUD (Create,Read,Update,Delete) operations for DataTables. It also can be configured to perform server-side processing, including the sorting, searching, and pagination. Server side processing is generally done when you have extremely large datasets like 50K records or more, otherwise, DataTables does all that stuff itself on the client side.

最近构建了一个名为 JED 的类库,可以帮助您解决分页问题。尽管 JED 不涉及 JPA,但它确实为 DataTables 执行了所有 CRUD(创建、读取、更新、删除)操作。它还可以配置为执行服务器端处理,包括排序、搜索和分页。服务器端处理通常在您拥有 50K 记录或更多记录等超大数据集时完成,否则,DataTables 会在客户端自行完成所有这些工作。

See the example shown at: http://jed-datatables.net/examples/basicssp.jsp

请参见以下示例:http: //jed-datatables.net/examples/basicssp.jsp