NHibernate应用五:HQL大全
2014-09-28 13:03
232 查看
一、基础内容
1、FROM子句
A、简单用法:
B、别名:
注意,as 是可选的,也就是说,上面代码可以写成
C、笛卡尔积
注意:其实际是使用cross join(笛卡儿积)来进行连接查询,这样性能很差,不建议使用。
2、SELECT子句
A、简单用法:结果集中返回指定的对象和属性
B、数组:用Object[]的数组返回多个对象或者属性
C、统计函数:用Object[]的数组返回属性的统计函数的结果,注意统计函数的变量也可以是集合count( elements(c.CustomerID) )
D、Distinct用法:distinct 和all 关键字的用法和语义与SQL 相同。
3、WHERE子句
A、数学运算符+, -, *, /;
B、二进制比较运算符=, >=, <=,<>, !=, like;
C、逻辑运算符and, or, not;
D、判断比较符 in, not in, between, is null, is not null, is empty, is not empty;
E、SQL 标量函数:upper(),lower();
F、位置参数:?
G、命名参数::name,:x1;
4、ORDER BY 子句
A、DESC 降序
B、ASC 升序
5、GROUP BY 子句
二、深入扩展
1、连接
A、inner join 内连接
B、left outer join 左外连接
C、right outer join 右外连接
D、full join 全连接,并不常用
2、聚集函数
1、FROM子句
A、简单用法:
public IList<CustomerEntity> GetCustomerFrom() { //返回所有Customer 类的实例 return _session.CreateQuery("from CustomerEntity").List<CustomerEntity>(); }
B、别名:
public IList<CustomerEntity> FromAlias() { //返回所有Customer 类的实例,Customer 赋予了别名customer return _session.CreateQuery("from CustomerEntity as customer").List<CustomerEntity>(); }
注意,as 是可选的,也就是说,上面代码可以写成
public IList<CustomerEntity> FromAlias() { //返回所有Customer 类的实例,Customer 赋予了别名customer return _session.CreateQuery("from CustomerEntity customer").List<CustomerEntity>(); }
C、笛卡尔积
public IList<object[]> GetFromAlias() { return _session.CreateQuery("from OrderProduct,OrderForm").List<object[]>(); }
注意:其实际是使用cross join(笛卡儿积)来进行连接查询,这样性能很差,不建议使用。
2、SELECT子句
A、简单用法:结果集中返回指定的对象和属性
public IList<string> GetCustomerAll() { //返回所有Customer 的CustomerId return _session.CreateQuery("select c.CustomerID from CustomerEntity c").List<string>(); }
B、数组:用Object[]的数组返回多个对象或者属性
public IList<object[]> GetCustomerObject() { return _session.CreateQuery("select c.CustomerID c. CustomerName from CustomerEntity c ").List<object[]>(); }
C、统计函数:用Object[]的数组返回属性的统计函数的结果,注意统计函数的变量也可以是集合count( elements(c.CustomerID) )
public IList<object[]> AggregateFunction() { return _session.CreateQuery("select avg(c.CustomerID),sum(c.CustomerID),count(c) from CustomerEntity c").List<object[]>(); }
D、Distinct用法:distinct 和all 关键字的用法和语义与SQL 相同。
public IList<string> Distinct() { return _session.CreateQuery("select distinct c. CustomerName from CustomerEntity c").List<string>(); }
3、WHERE子句
A、数学运算符+, -, *, /;
public IList<CustomerEntity> GetCustomerByID() { return _session.CreateQuery("from CustomerEntity where CustomerID='48CBCDFC-1AAA-4A08-9C9B-4578C6F59E8C'").List<CustomerEntity>(); }
B、二进制比较运算符=, >=, <=,<>, !=, like;
public IList<CustomerEntity> GetCustomerByName() { return _session.CreateQuery("from CustomerEntity where CustomerName like 'h%'").List<CustomerEntity>(); }
C、逻辑运算符and, or, not;
public IList<CustomerEntity> GetCustomerByWhere() { return _session.CreateQuery("from CustomerEntity where CustomerID='48CBCDFC-1AAA-4A08-9C9B-4578C6F59E8C' AND CustomerName like 'h%'").List<CustomerEntity>(); }
D、判断比较符 in, not in, between, is null, is not null, is empty, is not empty;
public IList<CustomerEntity> GetCustomerByString() { return _session.CreateQuery("from CustomerEntity where CustomerName in ('huhai')").List<CustomerEntity>(); }
E、SQL 标量函数:upper(),lower();
public IList<CustomerEntity> GetCustomerByLower() { return _session.CreateQuery("select lower(CustomerName) from CustomerEntity ").List<CustomerEntity>(); }
F、位置参数:?
public IList<CustomerEntity> GetCustomerByID() { IQuery query = _session.CreateQuery("from CustomerEntity where CustomerID=?"); query.SetString(0, "48CBCDFC-1AAA-4A08-9C9B-4578C6F59E8C"); return query.List<CustomerEntity>(); }
G、命名参数::name,:x1;
public IList<CustomerEntity> GetCustomerByID() { IQuery query = _session.CreateQuery("from CustomerEntity where CustomerID=:fn"); query.SetString("fn", "48CBCDFC-1AAA-4A08-9C9B-4578C6F59E8C"); return query.List<CustomerEntity>(); }
4、ORDER BY 子句
A、DESC 降序
public IList<CustomerEntity> GetCustomerByOrder() { return _session.CreateQuery("from CustomerEntity c order by c.CustomerID desc").List<CustomerEntity>(); }
B、ASC 升序
public IList<CustomerEntity> GetCustomerByOrder() { return _session.CreateQuery("from CustomerEntity c order by c.CustomerName asc").List<CustomerEntity>(); }
5、GROUP BY 子句
public IList<object[]> GetCustomerByGroup() { return _session.CreateQuery("select c.CustomerName, count(c.CustomerName) from Customer c group by c.CustomerName").List<object[]>(); }
二、深入扩展
1、连接
A、inner join 内连接
public IList<object[]> GetCustomerInterJoin() { return _session.CreateQuery("select c from CustomerEntity as c inner join c.OrderForm").List<object[]>(); }
B、left outer join 左外连接
public IList<object[]> GetCustomerLeftJoin() { return _session.CreateQuery("select c from CustomerEntity c left outer join c.OrderForm by c.OrderForm.CustomerID").List<object[]>(); }
C、right outer join 右外连接
public IList<object[]> GetCustomerRightJoin() { return _session.CreateQuery("select c from CustomerEntity c right outer join c.OrderForm by c.OrderForm.CustomerID").List<object[]>(); }
D、full join 全连接,并不常用
2、聚集函数
public IList<object[]> AggregateFunction() { return _session.CreateQuery("select avg(c.CustomerID),sum(c.CustomerID),count(c) from CustomerEntity c").List<object[]>(); }
相关文章推荐
- NHibernate实践总结(三)HQL的theta-style join对无关联实体的连接与应用
- Nhibernate HQL 大全
- NHibernate进阶之HQL应用
- NHibernate实践总结(三)HQL的theta-style join对无关联实体的连接与应用
- ftp命令大全及应用实例!
- C语言库函数大全及应用实例四
- C语言库函数大全及应用实例九
- C语言库函数大全及应用实例七
- C语言库函数大全及应用实例三
- 在实际项目中应用NHibernate
- ActiveRecord学习(四):HQL应用
- C语言库函数大全及应用实例十三
- C语言库函数大全及应用实例十一
- 键盘控制事件应用教程大全
- NHibernate在Asp.net中的实际应用
- 关于Nhibernate中的hql与sql的转换问题
- C语言库函数大全及应用实例十
- C语言库函数大全及应用实例二
- C语言库函数大全及应用实例十二
- Eclipse插件下载、技术应用专题网址大全