HQL基础Query简单查询结果for输出和Iterator输出

HQL第一次课:

  hibernate Query Language:hibernate 查询语言

  

  语法:

  query:

    String hql="from dept";

    Query query=session.createQuery(hql);//返回list集合

    query是一个对象,拥有很多方法,.list()方法返回集合;

  hql语句下次笔记详解,本次制作简单操作!

    

 1     public static List<Dept> getDept(){
 2         List<Dept> list=new ArrayList<Dept>();
 3         Configuration conf=null;
 4         SessionFactory factory=null;
 5         Session session=null;
 6         
 7         try {
 8             
 9             conf=new Configuration().configure();
10             //创建sessionfactory
11             factory=conf.buildSessionFactory();
12             //打开会话
13             session=factory.openSession();
14             //hql
15             String hql="select dept from Dept as dept where dept.deptname like '%发%'";
16             Query query=session.createQuery(hql);
17 //            list=query.list();
18 //            for(Dept dep:list){
19 //                System.out.println(dep);
20 //                
21 //            }
22             System.out.println("迭代器");
23             Iterator<Dept> ite=query.iterate();
24             while(ite.hasNext()){
25                 System.out.println(ite.next());
26                 
27             }
28             
29             
30         } catch (Exception e) {
31             e.printStackTrace();
32             
33         }finally{
34             if(session!=null){
35                 session.close();
36                 
37             }
38         }
39         
40         
41         
42         return list;
43         
44     }

  

    

原文地址:https://www.cnblogs.com/gcs1995/p/4126899.html