简体   繁体   English

JQuery DataTables服务器端分页,排序,使用Spring MVC + JPA进行搜索

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

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 pagination I was looking for a .NET solution. 在这个问题JQuery DataTables服务器端分页我正在寻找一个.NET解决方案。 And finally I found it. 最后我发现了它。

Now I need to know if something similar exists for Java applications. 现在我需要知道Java应用程序是否存在类似的东西。 I googled a bit, but I didn't find nothing realy complete... 我用Google搜索了一下,但我没有发现任何真正完整的内容......

There is a simple way of doing pagination using JPA. 有一种使用JPA进行分页的简单方法。 If you look at the javax.persistence.Query interface , you'll see that it has setFirstResult() and setMaxResult() methods. 如果你看一下javax.persistence.Query 接口 ,你会发现它有setFirstResult()setMaxResult()方法。 Using this in combination allows you to retrieve paged results for your query. 通过组合使用,您可以检索查询的分页结果。

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 eg 您可以根据需要排序的字段更改查询,例如

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.CriteriaQuery API which might make things a bit more maintainable: 或者,您可以查看javax.persistence.CriteriaQuery API,它可以使事情更易于维护:

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. 使用JPA搜索实体的所有字段都没有本地方法。 You may need to take a look at something like Hibernate Search to help you with that. 你可能需要看看像Hibernate Search这样的东西来帮助你。 It should integrate fairly cleanly with the JPA API assuming you are using Hibernate as your persistence provider of choice. 假设您使用Hibernate作为您选择的持久性提供程序,它应该与JPA API完全整合。

I finally solved implementing an ad hoc Java library: DataTables Pagination . 我终于解决了实现一个特殊的 Java库: DataTables Pagination

Here you find my project jar : https://bitbucket.org/davioooh/datatablepager/ 在这里,您可以找到我的项目jar https //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

There is a library of classes recently built named JED that can likely help you with your pagination issue. 最近建立了一个名为JED的类库,可以帮助您解决分页问题。 Though JED doesn't involve JPA, it does perform all manor of CRUD (Create,Read,Update,Delete) operations for DataTables. 虽然JED不涉及JPA,但它确实执行DataTables的所有CRUD(创建,读取,更新,删除)操作。 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. 服务器端处理通常在您拥有50K或更多记录等极大数据集时完成,否则,DataTables会在客户端完成所有这些操作。

See the example shown at: http://jed-datatables.net/examples/basicssp.jsp 请参阅以下示例: http//jed-datatables.net/examples/basicssp.jsp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM