0

Let's say I have a list of fruits(and let's assume these fruits are in the database)

    myList.add('Apple');

    myList.add('Mango');

    myList.add('Guava');

    myList.add('Tomato');

    myList.add('Dragon Fruit');

    myList.add('Orange');
   //More Fruits here

and let's say in my domain or data access object I have this

   public List<String> listOfFruits(){
        return myList;
    }

Now let's assume that I have 1000 fruits and I called listOfFruits of course it will load the whole list of fruits, and assuming that we have a thousand records of fruits therefore it will load and query of those fruits and it will /give performance impact,

now my question how can add a limit or offset in an arraylist when getting those fruits? I am currently making a pagination for this.

4

3 回答 3

2

在您的代码中,您手动将水果添加到列表中,这些记录已经在内存中,因此在获取这些水果时添加限制没有多大意义。

但是如果你使用 Hibernate 从数据库中检索记录,你可以这样做:

Query q = session.createQuery("FROM table");
q.setFirstResult(firstResult);
q.setMaxResults(maxResults);
于 2013-03-06T03:42:50.060 回答
0

limit 0, 10如果您使用 hql 查询获取以 0 开头的前 10 条记录,只需添加到 hql。
或者使用方法setMaxResultssetFirstResulton Query

于 2013-03-06T03:33:00.287 回答
0

如果有人想从表中部分获取记录,则添加限制或设置开始和最大结果将解决问题,但是如果水果列表是某些对象(如 currentStock)的一部分,并且我们想部分加载此列表,那么这些解决方案将如何工作.

于 2013-03-06T03:52:42.487 回答