HQL
hibernate Query Language 是Hibernate查询语言
HQL 是面向对象的查询语言
HQL是 查询主体是 映射配置的持久化类以及属性
SQL 是查询主体是数据库表
----------------------------------
HQL 语句形式
select。。。。from。。。。。where。。。。group by。。。。。having。。。order by。。。。
HQL 对java类与属性大小写是敏感的 但是对关键字的大小写是不敏感的 (select)
--------------------------------------------------
Query接口 org.hibernate.Query
什么是方法链编程风格 ???就是返回的还是调用该方法的对象
Query的创建
1.Session 的createQuery()方法创建
2.传入一个参数 hql的查询语句 createQuery(hql)
------------------------------
from 是HQL中必须的一个关键字
select 中查询的结果可以有 以object{] List Map 自定义 的形式去返回
distinct 是将查询结果中的记录 去重,只留下没有重复的记录
注意 的是 自定义 使用持久化类的时候,new的类中对象是选择性的话,那么需要在bean中将选择性
的参数进行构造一个含有参数的构造方法
public static void test(){
String hql="select new Map(s.name,s.tel,s.address) from Seller s";
Query query=session.creatQuery();
List<Map> maps=query.list();||List<List> lists=query.list();||List<object> lists=query.list();
||list<Seller> sellers=query.list();
//其中object 在写HQl语句的时候不需要new ,,,而其他的则需要
for(Map map:maps){
System.out.println("name:"+map.get("name"));
System.out.println("tel:"+map.get("1"));
System.out.println("address:"+map.get("2"));
}
}
---------------------------------------------------------
where 条件限制
1.范围运算
in ------from Customer c where c.age in(20,40)
not in
between...and....-----from Customer c where between 20 and 40
2.字符串模式匹配
like 模糊匹配
通配符 % 匹配任意一个字符 _ 下划线匹配一个字符
from Customer c where c.name like‘张_"
from Customer c where c.address like '%北京%' 只要在字符串中有北京则就会被查出
3.逻辑运算
and 与 or 逻辑或 not 逻辑非
4.集合运算
1.is[not] empty 集合为空或者不为空---相当于sql中的exists
2.member of 元素属于集合-----相当于sql中的in
5.使用四则运算符
from Commodity c where c.price*5>3000
6.排序规则
order by
asc 升序 desc是降序
from Commodity order by price asc
from Commodity order by seller.id asc,price desc,name asc
-------------------------------------------------------
总结
1.HQL 是对持久化类以及属性的大小写 敏感
2.select 子句使用自定义类返回选择属性的时候 ,持久化类构造器 不单单要写含参数构造器 还要写默认的构造函数
防止 有错误的发生
hibernate Query Language 是Hibernate查询语言
HQL 是面向对象的查询语言
HQL是 查询主体是 映射配置的持久化类以及属性
SQL 是查询主体是数据库表
----------------------------------
HQL 语句形式
select。。。。from。。。。。where。。。。group by。。。。。having。。。order by。。。。
HQL 对java类与属性大小写是敏感的 但是对关键字的大小写是不敏感的 (select)
--------------------------------------------------
Query接口 org.hibernate.Query
什么是方法链编程风格 ???就是返回的还是调用该方法的对象
Query的创建
1.Session 的createQuery()方法创建
2.传入一个参数 hql的查询语句 createQuery(hql)
------------------------------
from 是HQL中必须的一个关键字
select 中查询的结果可以有 以object{] List Map 自定义 的形式去返回
distinct 是将查询结果中的记录 去重,只留下没有重复的记录
注意 的是 自定义 使用持久化类的时候,new的类中对象是选择性的话,那么需要在bean中将选择性
的参数进行构造一个含有参数的构造方法
public static void test(){
String hql="select new Map(s.name,s.tel,s.address) from Seller s";
Query query=session.creatQuery();
List<Map> maps=query.list();||List<List> lists=query.list();||List<object> lists=query.list();
||list<Seller> sellers=query.list();
//其中object 在写HQl语句的时候不需要new ,,,而其他的则需要
for(Map map:maps){
System.out.println("name:"+map.get("name"));
System.out.println("tel:"+map.get("1"));
System.out.println("address:"+map.get("2"));
}
}
---------------------------------------------------------
where 条件限制
1.范围运算
in ------from Customer c where c.age in(20,40)
not in
between...and....-----from Customer c where between 20 and 40
2.字符串模式匹配
like 模糊匹配
通配符 % 匹配任意一个字符 _ 下划线匹配一个字符
from Customer c where c.name like‘张_"
from Customer c where c.address like '%北京%' 只要在字符串中有北京则就会被查出
3.逻辑运算
and 与 or 逻辑或 not 逻辑非
4.集合运算
1.is[not] empty 集合为空或者不为空---相当于sql中的exists
2.member of 元素属于集合-----相当于sql中的in
5.使用四则运算符
from Commodity c where c.price*5>3000
6.排序规则
order by
asc 升序 desc是降序
from Commodity order by price asc
from Commodity order by seller.id asc,price desc,name asc
-------------------------------------------------------
总结
1.HQL 是对持久化类以及属性的大小写 敏感
2.select 子句使用自定义类返回选择属性的时候 ,持久化类构造器 不单单要写含参数构造器 还要写默认的构造函数
防止 有错误的发生