NHibernate应用五:HQL大全
一、基础内容
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[]>(); }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。