📅  最后修改于: 2023-12-03 15:33:07.484000             🧑  作者: Mango
NHibernate是一个ORM框架,可以将对象映射到数据库表。在开发过程中,我们需要通过条件查询数据,NHibernate提供了多种方式来实现条件查询。
IQuery query = session.CreateQuery("from User u where u.Name = :name");
query.SetParameter("name", "张三");
IQuery query = session.CreateQuery("from User u where u.Age > :age");
query.SetParameter("age", 18);
IQuery query = session.CreateQuery("from User u where u.Age < :age");
query.SetParameter("age", 30);
IQuery query = session.CreateQuery("from User u where u.Age >= :age");
query.SetParameter("age", 18);
IQuery query = session.CreateQuery("from User u where u.Age <= :age");
query.SetParameter("age", 30);
IQuery query = session.CreateQuery("from User u where u.Name <> :name");
query.SetParameter("name", "张三");
IQuery query = session.CreateQuery("from User u where u.Name in (:names)");
query.SetParameterList("names", new[]{"张三", "李四", "王五"});
IQuery query = session.CreateQuery("from User u where u.Name like :name");
query.SetParameter("name", "张%");
IQuery query = session.CreateQuery("from User u where u.Name = :name and u.Age = :age");
query.SetParameter("name", "张三");
query.SetParameter("age", 18);
IQuery query = session.CreateQuery("from User u where u.Age between :minAge and :maxAge");
query.SetParameter("minAge", 18);
query.SetParameter("maxAge", 30);
IQuery query = session.CreateQuery("from User u where u.Department in (select d from Department d where d.Name = :name)");
query.SetParameter("name", "研发部门");
IQuery query1 = session.CreateQuery("select d.Id from Department d where d.Name = :name");
query1.SetParameter("name", "研发部门");
IQuery query2 = session.CreateQuery("from User u where u.Department.Id in (:deptIds)");
query2.SetParameterList("deptIds", query1.List<int>());
NHibernate提供了多种方式来实现条件查询,开发者可以根据实际需求来选择合适的方式。在条件查询中,需要注意参数化查询的使用,避免SQL注入攻击。