query.list()和query.iterate()區別

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

 

     list方法查詢全部記錄只發送一條sql語句,select * from 表名數據庫

     iterate方法查詢全部記錄的話,先查詢到知足條件的全部記錄id,並不當即查詢全部結果,當咱們有用到某個記錄的話,他才查詢。若是顯示全部記錄的話須要n+1條sql語句。第一條查id,接下去根據id,一條一條查詢,因此是n+1條。緩存

 

    list方法緩存中有記錄了,他仍是會發生sql語句到數據庫中查詢。session

 

   iterate方法緩存中有記錄了,他只發送一條查符合條件的id的sql語句。spa

 

   先看看list方法。code

       

    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();

 

結果:

第一次查詢blog

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

Deptment [deptid=2, deptname=sdsdzz]it

Deptment [deptid=3, deptname=sdsdzz]io

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]

相關文章
相關標籤/搜索