hql是非常有意识的被设计为完全面向对象的查询
基本规则:
1.hql语法类似于sql,但它后面跟的不是表名和字段名,而是类名和属性名
2.hql大小写不敏感.但是设计java类名,包名,属性名时大小写敏感.
public void test1(){ //重要
Session session = HibUtil.getSession();
Transaction tx =session.beginTransaction();
Query query =session.createQuery("from Emp e");
List<Emp> empList = query.list();
for(Emp e:empList){
System.out.println(e.getEname());
}
tx.commit();
session.close();
}
public void test2(){ //单个对象 重要
Session session = HibUtil.getSession();
Transaction tx =session.beginTransaction();
Query query =session.createQuery("select count(*) from Emp e");//集合函数查询
Long n = (Long) query.uniqueResult();
System.out.println(n);
tx.commit();
session.close();
}
public void test3(){ //单个对象 重要
Session session = HibUtil.getSession();
Transaction tx =session.beginTransaction();
Query query =session.createQuery("from Emp e where e.empno=7369");
Emp emp = (Emp) query.uniqueResult();
System.out.println(emp.getEname());
tx.commit();
session.close();
}
public void test4(){ //Object[] 次重要
Session session = HibUtil.getSession();
Transaction tx =session.beginTransaction();
Query query =session.createQuery("select e.empno,e.ename from Emp e ");
List<Object[]> list =query.list();
for(Object[] obj:list){
System.out.println(obj[0]+"-->"+obj[1]);
}
tx.commit();
session.close();
}
public void test5(){ //map 次重要
Session session = HibUtil.getSession();
Transaction tx =session.beginTransaction();
Query query =session.createQuery("select new map(e.empno as empno,e.ename as ename) from Emp e ");
List<Map<String,Object>> list =query.list();
for(Map<String,Object> map:list){
System.out.println(map.get("empno")+"-->"+map.get("ename"));
}
tx.commit();
session.close();
}
public void test6(){ //对象 次重要
Session session = HibUtil.getSession();
Transaction tx =session.beginTransaction();
Query query =session.createQuery("select new com.cdsxt.hib.vo.EmpVo(e.empno,e.ename) from Emp e ");//投影查询
List<EmpVo> list =query.list();
for(EmpVo emp:list){
System.out.println(emp.getEmpno()+"-->"+emp.getEname());
}
tx.commit();
session.close();
}
public void test7(){ //where条件 重要
Session session = HibUtil.getSession();
Transaction tx =session.beginTransaction();
// Query query =session.createQuery("from Emp e where e.job=?");
// query.setParameter(0, "CLERK");//?是从0开始的
Query query =session.createQuery("from Emp e where e.job=:job");
query.setParameter("job", "CLERK");
List<Emp> list =query.list();
for(Emp emp:list){
System.out.println(emp.getEmpno()+"-->"+emp.getEname());
}
tx.commit();
session.close();
}
public void test8(){ //where条件 重要
Session session = HibUtil.getSession();
Transaction tx =session.beginTransaction();
Query query =session.createQuery("from Emp e where e.dept.deptno=:deptno");
query.setParameter("deptno", 10);
List<Emp> list =query.list();
for(Emp emp:list){
System.out.println(emp.getEmpno()+"-->"+emp.getEname()+"-->"+emp.getDept().getDname());
}
tx.commit();
session.close();
}
public void test9(){ //where条件 重要
Session session = HibUtil.getSession();
Transaction tx =session.beginTransaction();
Query query =session.createQuery("from Emp e left outer join e.dept"); //hql会自动匹配e.deptno=dept.deptno
query.list();
tx.commit();
session.close();
}
public void test10(){ //where条件 重要
Session session = HibUtil.getSession();
Transaction tx =session.beginTransaction();
SQLQuery sqlQuery=session.createSQLQuery("select * from Emp e where e.ename like ?");
sqlQuery.setParameter(0, "%A%");
sqlQuery.addEntity(Emp.class);
List<Emp> empList = sqlQuery.list();
for(Emp emp:empList){
System.out.println(emp.getEmpno()+"-->"+emp.getEname()+"-->"+emp.getDept().getDname());
}
tx.commit();
session.close();
}