HQL 参数绑定、唯一结果、分页、投影总结(上)

我们先总结一下HQL语句常用语法:

  1. from子句:;
  2. select子句:用于选取对象和属性;
  3. where子句:用于表达查询语句的限制条件;
  4. 使用表达式:一般用在where子句中;
  5. order by子句:用于排序;

 下面根据我的某个项目的一张表进行总结才学习的HQL查询: 

  1、准备数据:

   数据库(Oracle):

 1 --类型表
 2 create table tb_type(
 3        id number(4) not null primary key,
 4        typename varchar2(10)
 5 
 6 )
 7 --添加测试数据
 8 insert into tb_type 
 9 values
10 (1,'喜剧');
11 insert into tb_type 
12 values
13 (2,'动作');
14 insert into tb_type 
15 values
16 (3,'爱情');
17 insert into tb_type 
18 values
19 (4,'动漫');
20 --dvd信息表
21 
22 create table tb_dvd
23 (
24 id number(4) not null,
25 name varchar2(20) not null,
26 star varchar2(18) not null,
27 intro varchar2(400) not null,
28 price number(2) not null,
29 num number(4) not null,
30 src varchar2(200) not null,
31 typeid number(2) not null
32 
33 )
34 
35 
36 --创建外键
37 alter table tb_dvd add constraint fk_dvd
38 foreign key(typeid)  referencing tb_type(id);
39 
40 --创建索引
41 create sequence seq_dvdindex;
DVD表

  2、配置DVD与HIbernate的映射关系

    (一)在HQL查询语句中绑定参数:

    两种方式:

      1、占位符:“?”

        hql="from DVDEntity as where name like ?";

        query.setParameter(0, "%"+emp.getEname()+"%");

      2、别名

        hql="from DVDEntity as where name like :name";

        query.setParameter("name", "%"+emp.getEname()+"%");

    

      query拥有很多设置参数的方法:

        setDouble()、setInteger()....等等

        我比较喜欢使用上面演示代码提到的setParameter():设置参数;不需要指定参数类型,相当方便

  

  (二)uniqueResult:  

    query查询到的是一个结果集,有和resultSet的异曲同工之妙!

    query.list()和.iteator()都是一系列数据,这里有人会问了,如果我知道查询结果只有可能是一条结果,那么query提供这样的方法了吗?

    sure,query.uniqueResult()返回唯一结果,这样就不浪费资源了;

    语法:

      Test test=(Test)query.uniqueResult();

   

  (三) 分页

    

    分页查询:

      下篇详记!

  (四) 投影&动态查询

    什么是投影:

      有时候并不需要查询对象的所有属性,在没有学习hibernate框架钱,我们使用封装实体类,将需要的数据封装在里面,他并不拥有完整的属性,但对于业务它里面的属性足够了,我们在这里将投影理解为封装一个业务需要的实体类,向业务传递数据,并且接受业务传回的数据,所以猿们弄出投影这么个东西

      第一步:建立业务需要的实体类(DVDForPrint)

          实体类里面需要一个有参构造方法,可以修改值

      第二步:数据操作

        hql="select new DVDForPrint(属性1,属性2) from DVDEntity  as dvd where ";

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

      小总结:

        多联系,慢慢的就能理解投影是什么了,不好解释!

    动态查询到底多动态:?

      需要使用from子句 ,where子句, [ 可能会使用表达式,orderby子句 ]

      代码如下:

      这里我没有细化出dvdfroPrint实体类,直接使用的dvdentity对象

 1 /**
 2      * 动态查询dvd列表
 3      * @param dvd
 4      * @return
 5      */
 6     public List<DVDEntity> getDvdByHiber(DVDEntity dvd){
 7         List<DVDEntity> list=new ArrayList<DVDEntity>();
 8         //hql
 9         StringBuffer hql=new StringBuffer("from DVDEntity where 1=1");
10         try {
11             conf=new Configuration().configure();
12             factory=conf.buildSessionFactory();
13             session=factory.openSession();
14             query=session.createQuery(appendHql(dvd,hql).toString());
15             //指定dvd对象
16             query.setProperties(dvd);
17             list=query.list();
18         } catch (Exception e) {
19             // TODO: handle exception
20             e.printStackTrace();
21         }finally{
22             
23             session.close();
24         }
25         
26         
27         return list;
28         
29         
30     }
31     /**
32      * 拼接hql
33      * @param dvd
34      * @param hql
35      * @return
36      */
37     private StringBuffer appendHql(DVDEntity dvd,StringBuffer hql){
38         if(dvd.getName()!=null){
39             hql.append(" and name like :name");
40             
41         }if(dvd.getIntro()!=null){
42             hql.append(" and intro like :intro");
43             
44         }if(dvd.getStar()!=null){
45             hql.append(" and star like :star");
46             
47         }if(dvd.getPrice()!=null){
48             hql.append(" and price between :starPrice and :endPrice");
49             
50         }if(dvd.getTypeId()!=null){
51             hql.append(" and typeid=:typeid");
52             
53         }
54         if(dvd.getNum()!=null){
55             if(dvd.getNum()>0){
56                 hql.append(" order by num desc");
57                 
58             }
59             
60         }
61         return hql;
62         
63     }
64     
View Code

    经验总结:

      暂无

      

原文地址:https://www.cnblogs.com/gcs1995/p/4135553.html