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

时间:2023-03-09 04:13:49
HQL 参数绑定、唯一结果、分页、投影总结(上)

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

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

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

  1、准备数据:

   数据库(Oracle):

 --类型表
create table tb_type(
id number(4) not null primary key,
typename varchar2(10) )
--添加测试数据
insert into tb_type
values
(1,'喜剧');
insert into tb_type
values
(2,'动作');
insert into tb_type
values
(3,'爱情');
insert into tb_type
values
(4,'动漫');
--dvd信息表 create table tb_dvd
(
id number(4) not null,
name varchar2(20) not null,
star varchar2(18) not null,
intro varchar2(400) not null,
price number(2) not null,
num number(4) not null,
src varchar2(200) not null,
typeid number(2) not null ) --创建外键
alter table tb_dvd add constraint fk_dvd
foreign key(typeid) referencing tb_type(id); --创建索引
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对象

 /**
* 动态查询dvd列表
* @param dvd
* @return
*/
public List<DVDEntity> getDvdByHiber(DVDEntity dvd){
List<DVDEntity> list=new ArrayList<DVDEntity>();
//hql
StringBuffer hql=new StringBuffer("from DVDEntity where 1=1");
try {
conf=new Configuration().configure();
factory=conf.buildSessionFactory();
session=factory.openSession();
query=session.createQuery(appendHql(dvd,hql).toString());
//指定dvd对象
query.setProperties(dvd);
list=query.list();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{ session.close();
} return list; }
/**
* 拼接hql
* @param dvd
* @param hql
* @return
*/
private StringBuffer appendHql(DVDEntity dvd,StringBuffer hql){
if(dvd.getName()!=null){
hql.append(" and name like :name"); }if(dvd.getIntro()!=null){
hql.append(" and intro like :intro"); }if(dvd.getStar()!=null){
hql.append(" and star like :star"); }if(dvd.getPrice()!=null){
hql.append(" and price between :starPrice and :endPrice"); }if(dvd.getTypeId()!=null){
hql.append(" and typeid=:typeid"); }
if(dvd.getNum()!=null){
if(dvd.getNum()>0){
hql.append(" order by num desc"); } }
return hql; }

    经验总结:

      暂无