简体   繁体   English

如何在 Play 框架中使用 Ebean 限制 MySQL 查询

[英]How to limit the MySQL Query with Ebean in Play Framework

Heys guys, i have a Problem using Play Framework.嘿伙计们,我在使用 Play Framework 时遇到了问题。 I am trying to display a large amount of Data (from this Database).我正在尝试显示大量数据(来自此数据库)。 When i am using the "find.all()" the Play Framework Server crashes, since it takes to much memory.当我使用“find.all()”时,Play Framework Server 崩溃了,因为它需要很多内存。

I got a DB Model named:我有一个名为的数据库模型:

@Entity
public class dblp_pub_new extends Model {
[...]
    public dblp_pub_new() {}

    public static List<dblp_pub_new> all() {
        return find.all();
    }

    public String getDoi() {
        return doi;
    }

    public void setMdate() {
        this.mdate = new Date();
    }

    public static Finder<String,dblp_pub_new> find = new Finder<String, dblp_pub_new>(String.class, dblp_pub_new.class);

}

My rendering function is, which is contained in Application.java:我的渲染函数是,它包含在 Application.java 中:

public static Result dois(){
    return ok(views.html.index.render(dblp_pub_new.all(), DoiForm));
}

I am trying to limit the all() Query to 50 (best would be per page).我试图将 all() 查询限制为 50(最好是每页)。 But i cant seem to figure it out.但我似乎无法弄清楚。 I think i need a List returned to display it on the webpage.我想我需要返回一个列表以在网页上显示它。 But I can't get it to work.但我无法让它工作。 I would be really relieved if one of you guys (and girls) could help me figuring out this problem.如果你们中的一个人(和女孩)能帮我解决这个问题,我会真的很欣慰。 I've tried it with "fetch" and "setMaxRows()" but I only get errors i can't seem to solve.我已经用“fetch”和“setMaxRows()”试过了,但我只得到我似乎无法解决的错误。 If there is something unclear, please just ask and i will try to provide as much information as i can.如果有不清楚的地方,请询问,我会尽力提供尽可能多的信息。 Thank you.谢谢你。

Using setMaxRows() should work. 使用setMaxRows()应该可以工作。 Try this: 尝试这个:

dblp_pub_new.find.setMaxRows(50).findList()

By the way, you should name your class according to the Java convention : DblpPubNew . 顺便说一句,您应该根据Java约定命名您的类: DblpPubNew It will make your code easier to read. 它将使您的代码更容易阅读。

Ebean has a helper for making pagination of data easier. Ebean有助于简化数据分页。 It's called a PagingList there. 它在那里被称为PagingList Play's Finder helper class for Ebeans allows you to get such a PagingList for your query . 用于Ebeans的Play的Finder助手类 允许您为查询获取此类PagingList

Say you want to display 50 items per page, and want to retrieve the items for the first page. 假设您要在每页显示50个项目,并且想要检索第一页的项目。 Then you'd write something like this 然后你会写这样的东西

public static List<dblp_pub_new> getpageItems(int page) {
    int pageSize = 50;
    return find.findPagingList(pageSize).getPage(page).getList();
}

Also, please note that your class name dblp_pub_new is highly unusual. 另请注意,您的类名dblp_pub_new非常不寻常。 Refer to this question for more information. 有关更多信息,请参阅此问题

in addition to Carsten's answer I only mention that Page itself contains several useful methods which help ie building pagination menu in the view, so it's easier to use whole Page object: 除了Carsten的回答,我只提到Page本身包含几个有用的方法,这些方法有助于在视图中构建分页菜单,因此使用整个Page对象更容易:

public static com.avaje.ebean.Page<dblp_pub_new> getPage(int pageSize, int page) {
    return find.findPagingList(pageSize).getPage(page);
}

and then you can use it in the view like: 然后你可以在视图中使用它:

<ul>
  @for(item <- myPage.getList){
    <li>@item.name</li>
  }
</ul>

<b>Total page count is: @myPage.getTotalPageCount()</b>

The currently accepted answer suggesting findPagingList is not valid anymore.当前接受的答案表明findPagingList不再有效。 See https://github.com/ebean-orm/ebean/issues/96https://github.com/ebean-orm/ebean/issues/96

Now using setMaxRows() should work with any Query but you have to use findPagedList on that (vs. findPagingList ).现在使用setMaxRows()应该适用于任何Query但您必须在其上使用findPagedList (与findPagingList )。 Something like this:像这样的东西:

List<dblp_pub_new> list = dblp_pub_new.find.query()
   .where().eq("some_column", some_value)
   .orderBy("some_column desc")
   .setMaxRows(50)
   .findPagedList()
   .findList()

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

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