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[]>();
    }

 

转载于:https://www.cnblogs.com/huhai121/p/3994944.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值