Hibernate學習之路(七)

來源:互聯網
上載者:User

標籤:public   hibernate   sel   session   對象建立   開啟   []   sys   string   

hql:hibernate query language   hibernate查詢語言

1、單屬性查詢

 1     public void testQuery1(){ 2         //通過Configuration對象建立SessionFactory對象 3         SessionFactory sf = new Configuration().configure().buildSessionFactory(); 4         //建立Session對象 5         Session s = sf.openSession(); 6         //開啟事務 7         s.beginTransaction(); 8          9         //我們只需要關注這裡的事務。10         String sql = "select name from Student";11         Query query = s.createQuery(sql);12         //返回結果類型,根據查詢的列決定13         List<String> names = query.list();14         for(String name: names){15             System.out.println(name);16         }17         //提交事務18         s.getTransaction().commit();19         //關閉Session20         s.close();21         //關閉SessionFactory22         sf.close();23     }

2、多個屬性查詢

 1     public  void testQuery1(){ 2         //通過Configuration對象建立SessionFactory對象 3         SessionFactory sf = new Configuration().configure().buildSessionFactory(); 4         //建立Session對象 5         Session s = sf.openSession(); 6         //開啟事務 7         s.beginTransaction(); 8          9         //我們只需要關注這裡的事務。10         String sql = "select name,age from Student";11         Query query = s.createQuery(sql);12         //返回結果類型,根據查詢的列決定13         List<Object[]> list = query.list();14         for(Object[] objs: list){15             System.out.println(objs[0]+"---"+objs[1]);16         }17         //提交事務18         s.getTransaction().commit();19         //關閉Session20         s.close();21         //關閉SessionFactory22         sf.close();23     }

3、查詢所有列

 1     public  void testQuery1(){ 2         //通過Configuration對象建立SessionFactory對象 3         SessionFactory sf = new Configuration().configure().buildSessionFactory(); 4         //建立Session對象 5         Session s = sf.openSession(); 6         //開啟事務 7         s.beginTransaction(); 8          9         //我們只需要關注這裡的事務。10         String sql = "from Student";11         Query query = s.createQuery(sql);12         //返回結果類型,根據查詢的列決定13         List<Student> list = query.list();14         for(Student stu: list){15             System.out.println(stu);16         }17         //提交事務18         s.getTransaction().commit();19         //關閉Session20         s.close();21         //關閉SessionFactory22         sf.close();23     }

4、條件查詢

 1     public  void testQuery1(){ 2         //通過Configuration對象建立SessionFactory對象 3         SessionFactory sf = new Configuration().configure().buildSessionFactory(); 4         //建立Session對象 5         Session s = sf.openSession(); 6         //開啟事務 7         s.beginTransaction(); 8          9         //我們只需要關注這裡的事務。10         String sql = "from Student where id<?";11         Query query = s.createQuery(sql);12         query.setParameter(0, 2);13         //返回結果類型,根據查詢的列決定14         List<Student> list = query.list();15         for(Student stu: list){16             System.out.println(stu);17         }18         //提交事務19         s.getTransaction().commit();20         //關閉Session21         s.close();22         //關閉SessionFactory23         sf.close();24     }

5、分頁查詢

 1     public  void testQuery1(){ 2         //通過Configuration對象建立SessionFactory對象 3         SessionFactory sf = new Configuration().configure().buildSessionFactory(); 4         //建立Session對象 5         Session s = sf.openSession(); 6         //開啟事務 7         s.beginTransaction(); 8          9         //我們只需要關注這裡的事務。10         String sql = "from Student";11         Query query = s.createQuery(sql);12         query.setFirstResult(0);13         query.setMaxResults(5);14         //返回結果類型,根據查詢的列決定15         List<Student> list = query.list();16         for(Student stu: list){17             System.out.println(stu);18         }19         //提交事務20         s.getTransaction().commit();21         //關閉Session22         s.close();23         //關閉SessionFactory24         sf.close();25     }

 

Hibernate學習之路(七)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.