NHibernate条件查询(Criteria Query)
代码
典型用法:从ISession接口中创建ICriteria实例对象;在这个ICriteria实例对象上设置一个或多个表达式;要求ICriteria接口返回需要的列表,就是根据表达式从数据库中返回对象。
1.创建ICriteria实例
使用ISession接口的CreateCriteria方法创建了NHibernate.ICriteria接口一个特定的持久化类的查询实例,也可以说ISession是用来制造Criteria实例的工厂。
public IList<Customer> CreateCriteria()
{
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> LikeAndBetween()
{
IList<Customer> customers = _session.CreateCriteria(typeof(Customer))
.Add(Restrictions.Like("Firstname", "YJing%"))
.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.CreateCriteria(typeof(Customer))
.Add(Restrictions.Like("Firstname","Y%"))
.AddOrder(new NHibernate.Criterion.Order("Firstname", false))
.AddOrder(new NHibernate.Criterion.Order("Lastname", true))
.List<Customer>();
}
4.条件查询
/// 利用CriteriaAPI获取顾客ID大于lId小于gId的顾客 public IList<Customer> GreaterAndLetter(int lId,int gId)
{
return _session.CreateCriteria(typeof(Customer))
.Add(Restrictions.Gt("CustomerId", lId))//大于
.Add(Restrictions.Eq("CustomerId", lId))//等于
.Add(Restrictions.Or(Restrictions.Eq("CustomerId", gId), Restrictions.Lt("CustomerId", gId)))//小于等于
.List<Customer>();
}
测试:
[TestFixture]
public class QueryCriteriaAPIFixture
{
private SessionManager _helper;
private ISession _session;
private QueryCriteriaAPI _queryCriteriaAPI;
[TestFixtureSetUp]
public void TestFixtureSetup()
{_helper = new SessionManager();}
[SetUp]
public void Setup()
{
_session = _helper.GetSession();
_queryCriteriaAPI = new QueryCriteriaAPI(_session);
}
[Test]
public void CreateCriteriaTest()
{
IList<Customer> customers = _queryCriteriaAPI.CreateCriteria();
Assert.AreEqual(5, customers.Count);
}
[Test]
public void LikeAndBetweenTest()
{
IList<Customer> customers = _queryCriteriaAPI. LikeAndBetween();
Assert.AreEqual(1, customers.Count);
}
[Test]
public void OrderTest()
{
IList<Customer> customers = _queryCriteriaAPI.Order();
Assert.AreEqual(1, customers.Count);
}
[Test]
public void GreaterAndLetterTest()
{
IList<Customer> customers= _queryCriteriaAPI.GreaterAndLetter (1,4);
foreach (Customer c in customers)
{
Assert.GreaterOrEqual(c.CustomerId, 1);//大于等于
Assert.LessOrEqual(c.CustomerId, 4);//小于等于
}
}
}
1.创建ICriteria实例
使用ISession接口的CreateCriteria方法创建了NHibernate.ICriteria接口一个特定的持久化类的查询实例,也可以说ISession是用来制造Criteria实例的工厂。
public IList<Customer> CreateCriteria()
{
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> LikeAndBetween()
{
IList<Customer> customers = _session.CreateCriteria(typeof(Customer))
.Add(Restrictions.Like("Firstname", "YJing%"))
.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.CreateCriteria(typeof(Customer))
.Add(Restrictions.Like("Firstname","Y%"))
.AddOrder(new NHibernate.Criterion.Order("Firstname", false))
.AddOrder(new NHibernate.Criterion.Order("Lastname", true))
.List<Customer>();
}
4.条件查询
/// 利用CriteriaAPI获取顾客ID大于lId小于gId的顾客 public IList<Customer> GreaterAndLetter(int lId,int gId)
{
return _session.CreateCriteria(typeof(Customer))
.Add(Restrictions.Gt("CustomerId", lId))//大于
.Add(Restrictions.Eq("CustomerId", lId))//等于
.Add(Restrictions.Or(Restrictions.Eq("CustomerId", gId), Restrictions.Lt("CustomerId", gId)))//小于等于
.List<Customer>();
}
测试:
[TestFixture]
public class QueryCriteriaAPIFixture
{
private SessionManager _helper;
private ISession _session;
private QueryCriteriaAPI _queryCriteriaAPI;
[TestFixtureSetUp]
public void TestFixtureSetup()
{_helper = new SessionManager();}
[SetUp]
public void Setup()
{
_session = _helper.GetSession();
_queryCriteriaAPI = new QueryCriteriaAPI(_session);
}
[Test]
public void CreateCriteriaTest()
{
IList<Customer> customers = _queryCriteriaAPI.CreateCriteria();
Assert.AreEqual(5, customers.Count);
}
[Test]
public void LikeAndBetweenTest()
{
IList<Customer> customers = _queryCriteriaAPI. LikeAndBetween();
Assert.AreEqual(1, customers.Count);
}
[Test]
public void OrderTest()
{
IList<Customer> customers = _queryCriteriaAPI.Order();
Assert.AreEqual(1, customers.Count);
}
[Test]
public void GreaterAndLetterTest()
{
IList<Customer> customers= _queryCriteriaAPI.GreaterAndLetter (1,4);
foreach (Customer c in customers)
{
Assert.GreaterOrEqual(c.CustomerId, 1);//大于等于
Assert.LessOrEqual(c.CustomerId, 4);//小于等于
}
}
}