• 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]

  • 相关阅读:
    Github地址
    第三次冲刺12.16
    第三次冲刺12.07~12.15
    第二次冲刺11.24~12.03
    第十天
    照片
    最终总结
    app的推广
    第三个Sprint冲刺事后诸葛亮报告
    第三个Sprint团队贡献分
  • 原文地址:https://www.cnblogs.com/Fc-ios/p/3794171.html
Copyright © 2020-2023  润新知