package com.itcloud.test; import com.itcloud.pojo.Dept; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.query.Query; import org.junit.After; import org.junit.Test; import java.util.List; public class TestHibernate { public static SessionFactory sessionFactory =null ; public static Session session = null ; public static Transaction transaction ; static { Configuration configuration = new Configuration().configure() ; sessionFactory = configuration.buildSessionFactory() ; session = sessionFactory.openSession() ; transaction = session.beginTransaction() ; } @Test public void post(){ String hql = "from Dept as d" ; //as請別名 //經過session建立quer查詢 Query<Dept> query = session.createQuery(hql,Dept.class) ;
//上面的語句也能夠這樣寫:Query query = session.createQuery(hql) ;
List<Dept> allDept = query.list(); System.out.print(allDept);
}
@After public void destroy(){ transaction.commit(); session.close(); sessionFactory.close(); } }
*******************進行模糊查詢java
@Test public void post(){ String hql = "from Dept as d where d.loc like ?" ; //as請別名 //經過session建立quer查詢 Query<Dept> query = session.createQuery(hql,Dept.class) ; //設置參數 query.setParameter(0,"%"+3+"%") ; //hibernate的參數是從零開始的 List<Dept> allDept = query.list(); System.out.print(allDept); }
*************進行分頁查詢:session
@Test public void post(){ String column="loc" ; //表示模糊查詢列 ; String keyword="" ;//空字符串表示查詢所有 int currentPage = 1 ;//當前頁碼,第一頁 int lineSize =2 ;//每頁顯示兩條數據 ; String hql = "From Dept as d where d."+column+" like ?" ; //注意點,Dept必須對應着實體類 //經過session建立quer查詢 Query<Dept> query = session.createQuery(hql,Dept.class) ; //設置參數 query.setParameter(0,"%"+keyword+"%") ; //hibernate的參數是從零開始的 /** 在查詢以前進行分頁操做 查詢行數仍是從0開始 * 第1頁從0行開始,第二頁從(2-1)*2=2行開始,... * 動態的根據頁碼的變化,跟新查詢的數據 */ query.setFirstResult((currentPage-1)*lineSize) ;//設置開始行 query.setMaxResults(lineSize) ; //每頁查詢lineSize行數據 //上面的語句就表示,從第一行開始查詢,每次查詢2行 List<Dept> allDept = query.list(); System.out.print(allDept); }
查詢結果post
Hibernate:
select
dept0_.deptno as deptno1_0_,
dept0_.dname as dname2_0_,
dept0_.loc as loc3_0_
from
Dept dept0_
where
dept0_.loc like ? limit ?
********統計查詢:ui
@Test public void post(){ String hql = "select count(deptno) From Dept as d" ; //注意點,Dept必須對應着實體類 Query query = session.createQuery(hql) ; System.out.println(query.uniqueResult()); }
注意點:spa
query.uniqueResult()返回是的數據類型是Long,能夠經過如下的方法轉換成整型
取得查詢語句:hibernate
Query query = session.getNamedQuery("findById") ; query.setParameter(0,4);//設置參數能夠不考慮數據類型 News vo =(News)query.uniqueResult() ; //查詢出單個數據 System.out.println(vo); HibernateSessionFactory.closeSession();
*********query進行數據的更新操做設計
@Test public void post(){ String hql = "update Dept set dname=? where deptno=?" ; Query query = session.createQuery(hql) ; //設置參數 query.setParameter(0,"更新部門操做") ; query.setParameter(1,1) ; int x = query.executeUpdate() ; //執行更新語句返回的是影響的行數 ; System.out.print(x); }
*********刪除數據操做code
String hql = "delete from News where nid=?" ; Query query = session.createQuery(hql); query.setParameter(0, 6); int len = query.executeUpdate() ;//進行數據的更新返回的是影響的行數 HibernateSessionFactory.getSession().beginTransaction().commit();//更新操做必需要進行數據的提交 System.out.println(len);
*********使用iterator取得查詢結果xml
@Test public void post(){ String hql = "from Dept as d" ; //as請別名 //經過session建立quer查詢 Query<Dept> query = session.createQuery(hql,Dept.class) ; //上面的語句也能夠這樣寫:Query query = session.createQuery(hql) ; Iterator<Dept> iter = query.iterate(); while(iter.hasNext()){ System.out.println(iter.next()); } }
結果:blog
Hibernate: select dept0_.deptno as col_0_0_ from Dept dept0_ Hibernate: select dept0_.deptno as deptno1_0_0_, dept0_.dname as dname2_0_0_, dept0_.loc as loc3_0_0_ from Dept dept0_ where dept0_.deptno=? Dept{deptno=1, dname='更新部門操做', loc='403'} Hibernate: select dept0_.deptno as deptno1_0_0_, dept0_.dname as dname2_0_0_, dept0_.loc as loc3_0_0_ from Dept dept0_ where dept0_.deptno=? Dept{deptno=2, dname='運營部門', loc='411'} Hibernate: select dept0_.deptno as deptno1_0_0_, dept0_.dname as dname2_0_0_, dept0_.loc as loc3_0_0_ from Dept dept0_ where dept0_.deptno=? Dept{deptno=3, dname='設計部門', loc='401'}
這種方式我是不會用的