Nhibernate分页测试续(附源码)
时间:2011-01-03 来源:john chen
1.配置
<?xml version="1.0" encoding="utf-8"?><hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">
Data Source=.;Initial Catalog=NhibernateDemoDB;Persist Security Info=True;Integrated Security=SSPI;
</property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="use_outer_join">true</property>
<property name="command_timeout">10</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<property name="proxyfactory.factory_class">
NHibernate.ByteCode.Castle.ProxyFactoryFactory,
NHibernate.ByteCode.Castle
</property>
<mapping assembly="NhibernateDemo.Data"/>
</session-factory>
</hibernate-configuration>
配置想必接触过Nhibernate的人或多或少都有所了解,在这就不多加说明了。
2.映射
传统对象和关系数据库之间的映射是用一个XML文档(XML document)来定义的,这个映射文档被设计为易读的,并且可以手工修改。而我更喜欢的是用Fluent Nhibernate一个开源的框架,有了它就可以脱离传统的Nhibernate配置文件配置映射文件的方式,而采用强类型方式映射。如下:
public class CustomerMap:ClassMap<Customer>{
public CustomerMap()
{
Id(m => m.Id);
Map(m => m.Name);
Map(m => m.Tel);
Map(m => m.Address);
Map(m => m.CreateDate);
Map(m => m.Sex);
}
}
3.创建ISession
Nhibernate操作数据库要通过ISession(NHibernate的工作单元),框架Fluent Nhibernate下创建ISession可看Fluent NHibernate的初识。如下
public class NHibernateHelper{
private static Configuration configuration = null;
private static ISessionFactory sessionFactory = null;
public static void CreateConfiguration()
{
configuration = new Configuration().Configure();
}
public static Configuration Configuration
{
get
{
if (configuration == null)
{
CreateConfiguration();
}
return configuration;
}
set { configuration = value; }
}
public static ISessionFactory SessionFactory
{
get
{
if (sessionFactory == null)
{
if (Configuration == null)
{
CreateConfiguration();
}
FluentConfiguration fluentConfiguration = Fluently.Configure(Configuration);
string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
string assemblyFile = Path.Combine(path, "bin/NhibernateDemo.Data.dll");
fluentConfiguration.Mappings(delegate(MappingConfiguration m)
{
Assembly assembly = Assembly.LoadFrom(assemblyFile);
m.HbmMappings.AddFromAssembly(assembly);
m.FluentMappings.AddFromAssembly(assembly).Conventions.AddAssembly(assembly);
});
return fluentConfiguration.BuildSessionFactory();
}
else
{
return sessionFactory;
}
}
}
public static ISession CreateSession()
{
return SessionFactory.OpenSession();
}
}
CreateSession方法创建了一个ISession。
4.分页方法
/// <summary>/// 获取分页
/// </summary>
/// <param name="pageStart"></param>
/// <param name="pageLimit"></param>
/// <returns></returns>
public IList<Customer> GetCustomerPageModel(int pageStart, int pageLimit)
{
//HQL查询
IList<Customer> customerList = Session.CreateQuery("from Customer")
.SetFirstResult(pageStart * pageLimit)
.SetMaxResults(pageLimit)
.List<Customer>();
return customerList;
//条件查询
//return Session.CreateCriteria(typeof(Customer))
// .SetProjection(Projections.ProjectionList()
// .Add(Projections.Property("Id"), "Id")
// .Add(Projections.Property("Name"), "Name")
// .Add(Projections.Property("Tel"), "Tel")
// .Add(Projections.Property("Address"), "Address")
// .Add(Projections.Property("Sex"), "Sex")
// .Add(Projections.Property("CreateDate"), "CreateDate"))
// .AddOrder(Order.Asc("Id"))
// .SetFirstResult(pageStart * pageLimit)
// .SetMaxResults(pageLimit)
// .SetResultTransformer(Transformers.AliasToBean(typeof(Customer)))
// .List<Customer>();
//SQL查询
//IList<Customer> customerList = Session.CreateSQLQuery("SELECT * FROM Customer")
// .SetFirstResult(pageStart*pageLimit)
// .SetMaxResults(pageLimit)
// .SetResultTransformer(Transformers.AliasToBean<Customer>()).List<Customer>();
//return customerList;
}
在Nhibernate里实现分页用 SetFirstResult 和SetMaxResults实现,SetFirstResult 简单的理解就是从第几条记录开始,SetMaxResults是取几条记录。如上代码,列出了Nhibernate三种查询分页的方式,使用哪种方式就看个人爱好了。
以下是运行后的截图:
客户端采用JQuery,自己写的一个Grid控件。
源码:NhibernateDemo.rar
相关阅读 更多 +