Jpa使用数字和next,previous分页 [英] Jpa paging with numbers and next, previous

查看:106
本文介绍了Jpa使用数字和next,previous分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人问过这个问题并提出一个可能愚蠢的问题,但我是新手,而且您是专家,我很乐意从您的专家建议和经验中学习。


$ b

我想将分页添加到每页显示200条记录的应用程序中。页面的底部应该有数字。如果有1050,第一页将显示100,并且页面的底部将显示数字1,2,3,4,5& 6,

完成这个的一般逻辑是什么?
我知道数据库必须每次选择200,我必须跟踪第一条记录。


  1. 有没有办法知道总共返回多少条记录,这样我就可以知道在底部显示多少个数字页?它是否需要选择一个count()语句或其他内容?

  2. 对于1050条记​​录,数字1,2,3,4,5& 6将显示并点击每一个需要打电话给服务器。有没有办法知道下次调用服务器时会返回多少条记录?它是否需要选择一个count()语句或其他内容?


解决方案

JPA Criteria API 来完成这项工作。假设 TypedQuery ,您可以使用 setFirstResult setLastResult 来限制从数据库返回的记录。当然,这些方法的值将取决于请求的页面以及每页显示的记录数。

  first =(page  -  1)* page size; 
last =(page * size) - 1;

注意:假设第一页是1(而不是零) em>



要获得记录计数,请执行标准Criteria查询。举个例子:

$ p $ final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
final CriteriaQuery< Long> countQuery = builder.createQuery(Long.class);
countQuery.select(builder.count(countQuery.from(MyEntity.class)));
final Long count = entityManager.createQuery(countQuery)
.getSingleResult();

您也可以自定义上述代码以执行相对于另一个查询的计数。最后,你需要一些方法将总数传回客户端。这样做的一种方法是将查询的结果集包装到另一个包含计数属性的对象中,或者由于DAO调用返回所述对象。或者,您可以将count属性存储在请求范围内的对象中。

  public class ResultListWrapper< T> {
私人长期计数;
私人收藏< T>结果;

//构造函数,getters,setters
}


I apologize for asking this question if someone already asked it and for asking a perhaps dumb question but I am new to this and you are experts and I would love to learn from your expert advice and experience.

I want to add paging to an application displaying 200 records per page. The bottom of the page should have numbers. If there are 1050, the first page will display 100 and the bottom of the page will show numbers 1,2,3,4,5 & 6.

What is the general logic to accomplish this? I know that the database must select 200 every time and I must keep track of the first record.

  1. Is there a way to know how many records will be returned total so that I can know how many numbers to display on the bottom of the page? Does it require selecting a count() statement or something else?
  2. For the 1050 records, The numbers 1,2,3,4,5 & 6 will display and clicking each one requires a call to the server. Is there a way to know how many records will be returned in the next call to the server? Does it require selecting a count() statement or something else?

解决方案

You can use the JPA Criteria API to accomplish this. Assuming TypedQuery, you would use setFirstResult and setLastResult to limit the records returned from the database. Of course the values for these methods will be dependent on what page was requested, and how many records are getting displayed per page.

first = (page - 1) * page size;
last = (page * size) - 1;

Note: This assumes the first page is 1 (as opposed to zero).

To get a record count you execute a standard Criteria query. As an example:

final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
final CriteriaQuery<Long> countQuery = builder.createQuery(Long.class);
countQuery.select(builder.count(countQuery.from(MyEntity.class)));
final Long count = entityManager.createQuery(countQuery)
        .getSingleResult();

You can customize the above code to perform the count relative to another query as well. Finally, you need some way of communicating the total count back to the client. One way of doing this is wrapping the result set of your query in another object that contains an attribute for the count, or returns said object as a result of your DAO call. Or alternatively, you could store the count attribute in an object in your request scope.

public class ResultListWrapper<T> {
    private Long count;
    private Collection<T> results;

    // constructor, getters, setters
}

这篇关于Jpa使用数字和next,previous分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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