按主鍵查詢
dao層
public Emp get(Serializable id){ //通過session的get方法根據加載指定對象 return (Emp)HibernateUtil.currentSession().get(Emp.class,id); }
service層
public Emp findEmpById(Integer id){ Transaction tx=null; Emp result =null; try { tx=HibernateUtil.currentSession().beginTransaction(); //開啟事務 result=empDao.get(id); //調用Dao,根據OID加載指定對象 tx.commit(); } catch (HibernateException e) { e.printStackTrace(); if(tx!=null) tx.rollback(); //回滾事務 } return result; }
test測試類
public static void main(String[] args) { EmpService empService = new EmpService(); //查詢 Emp emp =empService.findEmpById(7788); System.out.printf(emp.getEname()); }
使用HQL
什么是hql
hql查詢是一種面向對象的查詢語言,其中沒有表和字段的概念,只有類,對象和屬性的概念,hql語句中除了java類和屬性名稱外,查詢語句對大小寫敏感,
所以SELECT和select是相同的.但是cn.entity.emp和cn.entity.EMP.hql語句中的關鍵字建議使用小寫字母.
編寫hql
hql的語法跟我們數據庫中寫的語法差不多,需要注意的是hql中查詢所有的列並不需要select關鍵字如下,最重要的一點是hql列對應的是類的中的字段名稱,而不是數據庫中的列
dao層
public List<Emp> findEmp(String job){ String hql = "from Emp"; Query query =HibernateUtil.currentSession().createQuery(hql);return query.list(); }
service層
public void findEmp(String job){ Transaction tx =null; try { tx=HibernateUtil.currentSession().beginTransaction(); List<Emp> emps = empDao.findEmp(); for (Emp e:emps) { System.out.println(e.getEname()); } tx.commit(); } catch (HibernateException e) { e.printStackTrace(); } }
這樣即可把全表的數據讀取出來,或者使用迭代器如下
public Iterator<Emp> findAll(){ String hql ="from Emp"; //定義hql Query query = HibernateUtil.currentSession().createQuery(hql); return query.iterate(); //執行查詢 }
public Iterator<Emp> findAllEmps(){ Transaction tx=null; Iterator<Emp> emps=null; try { tx=HibernateUtil.currentSession().beginTransaction(); //打開事務 emps=empDao.findAll(); //獲取數據 Emp emp=null; while (emps.hasNext()){ //判斷是否遍歷到末尾 emp=emps.next(); System.out.println("員工名稱:"+emp.getEname()); } tx.commit(); } catch (HibernateException e) { e.printStackTrace(); if(tx!=null) tx.rollback(); } return emps; }
在hql語句中綁定參數
query接口提供的綁定不同類型的參數的方法
- setBoolean():綁定Boolean類型的參數
- setByte():綁定byte類型的參數
- setDouble():綁定double類型的參數
- setDate():綁定util.Date類型的參數
- setString():綁定String類型的參數
兩種語法:
- setXXX(下標,值)
- setXXX(參數名稱,值)
如果需要使用setXXX(參數名稱,值)進行綁定參數 如下:
String hql = "from Emp where job=:j"; Query query =HibernateUtil.currentSession().createQuery(hql); query.setString("j",job);
如果是setXXX(下標,值)參數綁定 如下:
String hql = "from Emp where job= ?"; Query query =HibernateUtil.currentSession().createQuery(hql); query.setString(1,job);
注意
setXXX(參數名稱,值) 在hql中 需要使用 :自定義參數名稱
setXXX(下標,值) 在hql中需要使用 ?
除了以上用於綁定特定類型參數的方法,hibernate還提供了setParameter()方法,用來綁定任意類型的參數.
public List<Emp> findDate(Emp hireDate){ String hql = "from Emp where hiretDate > :hiretDate"; // :hiretDate 的hiretDate是與emp類的hiretDate名字一致 Query query =HibernateUtil.currentSession().createQuery(hql); query.setProperties(hireDate); //傳參 return query.list(); }
分頁和投影
hql中的分頁非常簡單,如下代碼
public List<Emp> empPage(Integer start,Integer end){ String hql = "from Emp"; Query query =HibernateUtil.currentSession().createQuery(hql); query.setFirstResult((start-1)*end); //設置頁數 query.setMaxResults(end); //設置每頁顯示的數據 return query.list(); }
public void empPage(Integer start,Integer end){ Transaction tx=null; try { tx=HibernateUtil.currentSession().beginTransaction(); List<Emp> emps = empDao.empPage(start,end); for (Emp e:emps) { System.out.println(e.getEname()); } tx.commit(); } catch (HibernateException e) { e.printStackTrace(); if(tx!=null) tx.rollback(); } }
假設是第二頁 查六筆數據如下結果
我們再來看下數據庫中
投影
使用from 表名 表示查詢表的所有列,使用SELECT 列1, 列2, 列3 from 表名
則可以僅返回指定列,這種操作稱為投影。