首页 > 代码库 > query.list()和query.iterate()区别

query.list()和query.iterate()区别

          list方法返回的是结果集的list集合,iterate返回的是iterator迭代器。

 

     list方法查询所有记录只发送一条sql语句,select * from 表名

     iterate方法查询所有记录的话,先查询到满足条件的所有记录id,并不立即查询所有结果,当我们有用到某个记录的话,他才查询。如果显示所有记录的话需要n+1条sql语句。第一条查id,接下去根据id,一条一条查询,所以是n+1条。

 

    list方法缓存中有记录了,他还是会发生sql语句到数据库中查询。

 

   iterate方法缓存中有记录了,他只发送一条查符合条件的id的sql语句。

 

   先看看list方法。

       

    Session session =DBUtil.getSession();          String hql ="from Deptment" ;        Query query = session.createQuery(hql);        System.out.println("第一次查询");        List list1 = query.list();        Iterator iter1 = list1.iterator();        while(iter1.hasNext())        {               System.out.println( iter1.next());        }               System.out.println("第二次查询");        List list2 = query.list();        Iterator iter2 = list2.iterator();        while(iter2.hasNext())        {               System.out.println( iter2.next());        }           session.close();

 

结果:

第一次查询

Hibernate: select deptment0_.DEPTID as DEPTID0_, deptment0_.DEPTNAME as DEPTNAME0_ from SCOTT.DEPTMENT deptment0_

Deptment [deptid=2, deptname=sdsdzz]

Deptment [deptid=3, deptname=sdsdzz]

Deptment [deptid=4, deptname=sdsdzz]

Deptment [deptid=1, deptname=a]

第二次查询

Hibernate: select deptment0_.DEPTID as DEPTID0_, deptment0_.DEPTNAME as DEPTNAME0_ from SCOTT.DEPTMENT deptment0_

Deptment [deptid=2, deptname=sdsdzz]

Deptment [deptid=3, deptname=sdsdzz]

Deptment [deptid=4, deptname=sdsdzz]

Deptment [deptid=1, deptname=a]

iterate()方法

        

   Session session =DBUtil.getSession();          String hql ="from Deptment" ;        Query query = session.createQuery(hql);        System.out.println("第一次查询");               Iterator iter1 = query.iterate();        while(iter1.hasNext())        {               System.out.println( iter1.next());        }               System.out.println("第二次查询");        Iterator iter2 =  query.iterate();        while(iter2.hasNext())        {               System.out.println( iter2.next());        }           session.close();

 

 结果:

第一次查询

Hibernate: select deptment0_.DEPTID as col_0_0_ from SCOTT.DEPTMENT deptment0_

Hibernate: select deptment0_.DEPTID as DEPTID0_0_, deptment0_.DEPTNAME as DEPTNAME0_0_ from SCOTT.DEPTMENT deptment0_ where deptment0_.DEPTID=?

Deptment [deptid=1, deptname=a]

Hibernate: select deptment0_.DEPTID as DEPTID0_0_, deptment0_.DEPTNAME as DEPTNAME0_0_ from SCOTT.DEPTMENT deptment0_ where deptment0_.DEPTID=?

Deptment [deptid=2, deptname=sdsdzz]

Hibernate: select deptment0_.DEPTID as DEPTID0_0_, deptment0_.DEPTNAME as DEPTNAME0_0_ from SCOTT.DEPTMENT deptment0_ where deptment0_.DEPTID=?

Deptment [deptid=3, deptname=sdsdzz]

Hibernate: select deptment0_.DEPTID as DEPTID0_0_, deptment0_.DEPTNAME as DEPTNAME0_0_ from SCOTT.DEPTMENT deptment0_ where deptment0_.DEPTID=?

Deptment [deptid=4, deptname=sdsdzz]

第二次查询

Hibernate: select deptment0_.DEPTID as col_0_0_ from SCOTT.DEPTMENT deptment0_

Deptment [deptid=1, deptname=a]

Deptment [deptid=2, deptname=sdsdzz]

Deptment [deptid=3, deptname=sdsdzz]

Deptment [deptid=4, deptname=sdsdzz]