NHibernate条件查询(Criteria Query)
来源:互联网 发布:男士发型软件 编辑:程序博客网 时间:2024/05/22 04:55
NHibernate条件查询(Criteria Query)
条件查询(Criteria Query)
1)创建ICriteria实例
2)结果集限制
3)结果集排序
4)说明
条件查询(Criteria Query)
HQL极为强大,但是有些人希望能够动态的使用一种面向对象API创建查询,尾部是在.NET代码中嵌入字符串。在NHibernate中,提供了一种直观的、可扩展的Criteria API。
在我们键入查询语句的时候,提供了变异时的语法检查,VS提供了强大的智能提示。如果你对HQL的语法感觉不是很舒服的话,用这种方法可能更容易。这种API也比HQL更可扩展。
典型用法:从ISession接口中创建ICriteria实例对象;在这里ICriteria实例对象上设置一个活多个表达式;要求ICriteria接口返回需要的列表,就是根据表达式从数据库中返回对象。
1.创建ICriteria实例
使用ISession接口的CreateCriteria方法创建了NHibernate.ICriteria接口一个特定的持久化类的查询实例,也可以说ISession是用来制造Criteria实例的工厂。
Public IList<Customer>VreateCriteria()
{
ICriteria crit=_session.CreateCriteria(typeof(Customer));
crit.SetMaxResults(50);
IList<Customer>customers=crit.List<Customer>();
Return customers;
}
上面的例子返回Customer对象集合,设置对打的集合数量为50条。
2.结果集限制
使用ICriteria接口提供的Add方法添加Restrictions类中约束表达式可以限制一些结果集的作用。
Public IList<Customer>Narrowing()
{
IList<Customer>customers=_session.CreateCriteria(typeof(Customer)).Add(Restrictions.Like(“Firstname”,”young%”)).Add(Restrictions.Between(“Lastname”,”A%”,”Y”%)).List<Customer>();
Return customers;
}
3.结果集排序
使用ICriteria.Order对结果集排序,第二个参数true代表asc,false代表desc。
查询Customer对象按FirstName降序、Lastname升序。
Public IList<Customer>Order()
{
Return _session.CreateCreteria(typeof(Customer)).Add(Restrictions.Like(“Firstname”,”y%”)).AddOrder(new NHIbernate.Criterion.Order(“Firstname”,false)).AddOrder(new NHibernate.Criterion.Order(“Lastname”,true)).List<Customer>();
}
条件查询同样支持关联查询、动态关联抓取,投影、聚合和分组,离线查询和子查询。
根据示例查询(Query By Example)
根据示例查询是条件查询的一种特殊情况,NHibernate.Criterion.Example类是根据你指定的实例创造查询条件。其典型的用法:创建一个Example实例:在Example实例上设置值:根据Example和设置NHibernate返回其对象集合。
Public IList<Cutomer>Query()
{
Customer customerSample=new Customer(){Firstname=”young”,Lastname=”y”};
Return _sessiom.CreateCriteria(typeof(Customer)).Add(Example.Create(customerSample)).List<Customer>();
}
你可以自行调整Example使之更实用:
Public IList<Customer>UserQueryByExample_GetCustomer(Customer customerSample)
{
Example example=newExample.Create(customerSample).IgnoreCase().EnableLike().SetEscapeCharacter(‘&’);
Return _session.CreateCritera(typeof(Customer)).Add(example).List<Cutomer>();
}
实例分析
1)利用CriteriaAPI按Firstname和Lastname查询顾客。
Public IList<Customer>GetCustomerByFirstnmaeAndLastname(string firstname,string lastname)
{
Return _session.CreateCriteria(typeof(Customer)).Add(Restrictions.Eq(“Firstname”,firstname)).Add(Restrictions.Eq(“Lastname”,lastname)).List<Customer>();
}
测试:调用GetCustomerByFirstnameAndLastname方法,查询Firstname为”young”,Lastname为”Y”的顾客,判断查询结果数量是否为1.。
[Test]
Public void GetCustomerByFirstnameAndLastnameTest()
{
IList<Customer>customers=new _queryCriteriaAPI.GetCustomersBYFirstnameAndLastname(“young”,”Y”);
Assert.AreEqual(1,customers.Count);
}
2)利用CriteriaAPI获取顾客ID大于CustomerId的顾客。
Publick IList<Customer>GetCustomersWithIdGreaterThan(int customerId)
{
Return _session.CreateCriteria(typeof(Customer)).Add(Restrictions.Get(“CustomerId”,customerId)).List<Customer>();
}
- NHibernate条件查询(Criteria Query)
- Hibernate条件查询(Criteria Query)
- Hibernate条件查询(Criteria Query)
- Hibernate条件查询(Criteria Query)
- Hibernate条件查询(Criteria Query)
- Hibernate条件查询(Criteria Query)
- NHibernate之旅(4):探索查询之条件查询(Criteria Query)
- NHibernate之旅(4):探索查询之条件查询(Criteria Query)
- NHibernate之旅(4):探索查询之条件查询(Criteria Query)
- hibernate参考手册之---条件查询(Criteria Query)
- [转]Hibernate条件查询(Criteria Query)
- Hibernate动态条件查询(Criteria Query)
- NHibernate说探——Criteria条件查询
- Nhibernate Criteria 多个or条件以及Like查询
- NHibernate查询之HQL&Criteria
- NHibernate中Criteria查询表达式(各种条件查询方法) 添加,修改,删除
- NHibernate Step by Step (五)Criteria Query
- NHibernate Step by Step (五)Criteria Query
- porting r70(on going)
- NHibernate查询语句(HQL)
- Chrome内部协议列表
- Fx2_InsWinUSB.inf
- QML基础——UI布局管理
- NHibernate条件查询(Criteria Query)
- 搜到几篇关于模式识别的文章,做摘录
- 单片机之UIP--TCP 校验和计算
- 常用linux命令学习总结
- JS数字分隔符的方法
- python端口扫描(扫描0到1023端口)
- 类MSN的右下角消息提示效果
- OpenCV2.4.4 图像仿射变换
- QML基础——入门教程