1. 程式人生 > >HQL(動態查詢,分頁查詢)

HQL(動態查詢,分頁查詢)

分頁查詢 div set 回話 from ber tor string onf

動態查詢
   Session session;
Transaction tx;
@Before
public void mybefor() {
//1構建配置對象COnfiguration
Configuration cfg = new Configuration().configure("hibernate02.cfg.xml");
//2.構建工廠SessionFactory
SessionFactory factory = cfg.buildSessionFactory();
//3.構建回話Session
session = factory.openSession();//從連接池中隨機取出連接
//4.開始事務
tx = session.beginTransaction();
}
@Test
public void RunSelectDept(){
String hql = "select new Dept(d.dno,d.dname,d.loc) from Dept d";
Query query = session.createQuery(hql);
List <Dept>list = query.list();
for (Dept dept: list) {
System.out.println(dept.getDname());
}
}
@Test
public void RunSelectDept2(){
String hql = "from Dept d where d.dname=? and d.loc=?";
Query query = session.createQuery(hql);
query.setParameter(0,"LENK");
query.setParameter(1,"CHINA");
List <Dept>list = query.list();
for (Dept dept: list) {
System.out.println(dept.getDname());
}
}
@Test
public void RunSelectDept3(){
String hql = "from Dept d where d.dname=:dname and d.loc=:loc";
Query query = session.createQuery(hql);
query.setParameter("dname","LENK");
query.setParameter("loc","CHINA");
List <Dept>list = query.list();
for (Dept dept: list) {
System.out.println(dept.getDname());
}
}
@After
public void myafter(){
tx.commit();
session.close();
}

HQL(動態查詢,分頁查詢)