hibernate和HQL語句
阿新 • • 發佈:2018-12-11
注意:HQL語言,是基於物件進行查詢的,不是基於資料庫的表。
1、基本的HQL查詢
1.1 使用HQL查詢的一般步驟
[java] view plain copy- public void test01() {
- Session session = HibernateUtils.openSession();
- Query query = session.createQuery("from Student");
- List<Student> stus = query.list();
- for (Student stu : stus) {
- System.out.println(stu);
- }
- }
1.2 在HQL中,不能使用 select * from Student ,但是可以使用別名 select stu from Student stu
[java] view plain copy- public void test02() {
- Session session = HibernateUtils.openSession();
- Query query = session.createQuery("select stu from Student stu");
- List<Student> stus = query.list();
- for (Student stu : stus) {
- System.out.println(stu);
- }
- }
1.3 鏈式查詢的用法
[java] view plain copy- public void test03() {
- Session session = HibernateUtils.openSession();
- List<Student> stus = session.createQuery(" from Student ")
- .list();
- for (Student stu : stus) {
- System.out.println(stu);
- }
- }
1.4 基於?的查詢 (JDBC的下標從1開始,Hibernate的下標從0開始)
- public void test04() {
- Session session = HibernateUtils.openSession();
- Student stu = (Student) session
- .createQuery(" from Student where id = ? ").setParameter(0, 1)
- .uniqueResult();
- System.out.println(stu);
- }
1.5 基於別名的查詢 id = :id (使用Map進行,別名的封裝)
- public void test05() {
- Session session = HibernateUtils.openSession();
- // 利用一個Map對,用到的別名進行封裝
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("id", 1);
- map.put("age", 18);
- // 查詢
- Student stu = (Student) session
- .createQuery(" from Student where id = :id and age = :age")//
- .setParameter("id", map.get("id"))//
- .setParameter("age", map.get("age"))//
- .uniqueResult();
- // 輸出查詢結果
- System.out.println(stu);
- }
2、常用的HQL查詢
2.1 查詢一個表裡面的記錄數(當查詢結果唯一時,可以使用 uniqueResult( ) )
[html] view plain copy- public void test06() {
- Session session = HibernateUtils.openSession();
- Long countRecord = (Long) session.createQuery(
- "select count(*) from Student").uniqueResult();
- int count = countRecord.intValue();
- System.out.println(count);
- }
2.2 查詢一個表中的某幾個欄位,返回的是一個 Object [ ] 的陣列(投影查詢) 按照年齡的分組查詢
[java] view plain copy- public void test07() {
- Session session = HibernateUtils.openSession();
- // 按照年齡的分組查詢
- List<Object[]> stus = session.createQuery(
- "select stu.age , count(*) from Student stu group by stu.age")
- .list();
- // 輸出查詢結果
- for (Object[] obj : stus) {
- System.out.println("年齡:" + obj[0] + " 的人數為:" + obj[1]);
- }
- }
2.3 DTO 資料傳輸物件的使用,
2.3.1 要將查詢出來的列,利用 as 關鍵字進行重新的命名
[sql] view plain copy
- select stu.age as age , count(*) as count from Student stu group by stu.age
2.3.2 使用,setResultTransformer(Transformers.aliasToBean(AgeGroup.class)),進行結果的轉化
2.3.3 DTO資料傳輸物件 ,AgeGroup.java的程式碼
[java] view plain copy- public class AgeGroup {
- private int age;
- private Long count;
- public AgeGroup() {
- }
- // get set
- @Override
- public String toString() {
- return "AgeGroup [age=" + age + ", count=" + count + "]";
- }
- }
2.3.4 使用DTO,查詢的程式碼如下
[java] view plain copy- public void test08() {
- Session session = HibernateUtils.openSession();
- // 按照年齡的分組查詢
- List<AgeGroup> stus = session
- .createQuery(//
- &