HQL ,Hibernate Query Language ,是Hibernate查询语言,不直接操作数据表,而是操作实体类,根据实体类和对应数据表中的映射关系,查找数据。

下面是hql的基本步骤:

 1.获取session 

  session=new Configuration().configure().buildSessionFactory().openSession();

 2.构建hql语句
  String hql="from Dept ";

 3.得到query对象
  Query query=session.createQuery(hql);

 4.得到查到的结果

  List<Dept> list=query.list();

  或者

  Iterator<Dept> it=query.iterate();

 

下面是几个hql例子

①职位是店员,如:job='CLERK'

工资大于1000元,如:salary>1000

 入职时间在1981年4月1日至1985年9月9日之间

 1 public void SearchCondition(){
 2         /**
 3          * 职位是店员,如:job='CLERK'
 4          * 工资大于1000元,如:salary>1000
 5          * 入职时间在1981年4月1日至1985年9月9日之间
 6          */
 7         Session session=null;    
 8         try {
 9             session=new Configuration().configure().
10                     buildSessionFactory().openSession();
11         //1.通过辅助类对象统一处理命名参数
12             //创建辅助类的对象
13             ConditionEmp cm=new ConditionEmp();
14             cm.setJob("CLERK");
15             cm.setSalary(566666);
16             SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
17             cm.setHireDateBegin(sdf.parse("2000-10-27"));
18             cm.setHireDateEnd(sdf.parse("2003-12-12"));
19             
20             StringBuilder hql=new StringBuilder("from Emp as emp where 1=1");
21             
22             //上面的数据有的有,有的木有
23             if(null!=cm.getJob()){
24                 //工作不是空的
25                 hql.append(" and emp.job=:job");
26             }
27             
28             if(0!=cm.getSalary()){
29                 hql.append(" and emp.salary>:salary");
30             }
31             if(null!=cm.getHireDateBegin()){
32                 hql.append(" and emp.hiredate>:hireDateBegin");
33             }
34             if(null!=cm.getHireDateEnd()){
35                 hql.append(" and emp.hiredate<:hireDateEnd");
36             }                        
37             Query query=session.createQuery(hql.toString());
38             //处理命名参数
39             query.setProperties(cm);
40             
41             List<Emp> list=query.list();
42             for (Emp emp : list) {
43                 System.out.println(emp.getEmpno());
44                 System.out.println(emp.getEmpname());
45             }        
46         } catch (Exception e) {
47             e.printStackTrace();
48         }finally{
49             session.close();
50         }
51     }
52     

② hql分页

  

 1 public void pageDemo(){
 2         Session session=null;    
 3         try {
 4             session=new Configuration().configure().
 5                     buildSessionFactory().openSession();
 6             String hql="from Emp order by empno";
 7             
 8             Query query=session.createQuery(hql);
 9             //每页显示几条数据
10             int pageSize=2;
11             
12             //计算总记录(总条数)
13             String countHql="select count(*) from Emp";
14             
15             //接收数字
16             int count=((Long)session.createQuery(countHql).uniqueResult()).intValue();
17              
18             //计算总页数
19             int totalPage=(count%pageSize==0)?(count/pageSize):(count/pageSize+1);
20             
21             //页号
22             int pageIndex =1;
23             
24             //设置每页显示的最大记录数字
25             query.setMaxResults(pageSize);
26             //设置从第几条开始输出,不包括该条
27             query.setFirstResult((pageIndex-1)*pageSize);
28             
29             List<Emp> list=query.list();
30             System.out.println("总页数是:"+totalPage);
31 
32             for (Emp emp : list) {
33                 System.out.println(emp.getEmpno());
34                 System.out.println(emp.getEmpname());
35                 
36             }
37             
38         } catch (Exception e) {
39             e.printStackTrace();
40         }finally{
41             session.close();
42         }
43     }

 

 

posted on 2015-12-02 20:29  映日残阳  阅读(240)  评论(0编辑  收藏  举报