码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-11-27 20:11:52      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   for   on   

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     }

  

    

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

标签:style   blog   io   ar   color   os   sp   for   on   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!