1、引用NHibernate.dll、Log4net.dll
2、在VS2005IDE中获得NHibernate配置文件的代码提示,请将你的$NHibernate/src/NHibernate下的nhibernate-configuration-2.0.xsd、nhibernate-mapping-2.0.xsd拷贝到/Program Files/Microsoft Visual Studio 8/Xml/Schemas下,这样当你编辑配置文件或者映射文件时,你将得到完整的代码提示,VS2003请拷贝到/Program Files/Microsoft Visual Studio .NET 2003/Common7/Packages/schemas/xml下
3、配置Web.config、App.config(非标准配置,更多信息可参考NHibernate自身的配置描述)NHibernat内部使用log4net来进行日志操作:http://logging.apache.org/log4net/
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> </configSections> <nhibernate> <add key="show_sql" value="true" /> <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" /> <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" /> <add key="hibernate.connection.connection_string" value="Server=localhost;Initial Catalog=NHibernate;Integrated Security=SSPI" /> <add key="hibernate.connection.isolation" value="ReadCommitted"/> <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" /> </nhibernate> <log4net> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" > <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%d [%t] %-5p %c [%x] - %m%n" /> </layout> </appender> <root> <level value="ALL" /> <appender-ref ref="ConsoleAppender" /> </root> </log4net> </configuration>
4、Data OP
(1)获取记录 Person person = (Person)session.Get(typeof(Person),1);
(2)保存记录 session.Save(person);
(3)删除记录Person person = (Person)session.Get(typeof(Person),1);session.Delet(person);
(4)Load Person person = (Person)session.Load(typeof(Person),1);Get在找不到对象时会返回一个null引用,而Load则会抛出一个ObjectNotFoundException,所以,不应该用Load来判断记录是否存在。另外还有两条非常重要的区别,我们将放在高级话题讨论
(5)Query(HQL)
IList list = session.CreateQuery(“from Person”).List(); Person person = (Person)list[0]; orIQuery query = session.CreateQuery(“from Person”); IList list = query.List();
“from Person ”意思是取出Person类映射的person表中的所有记录,对应的SQL是HQL写法(基本通用):
select p.Name from Person p where p.Age>20 and p.Name like “Jaki%” select p.Name from Person as p where p.Id=1 select Name from Person where Age>20 select Id,Name from Person where Id>1 and Age>20
注意:字段大小写应该尽量跟属性名称保持一致,尤其是在使用别名的情况下
5、生成实体配置文件采用codesmith简洁的方法就是内嵌资源的方式到DLL中(注意各个版本的差别)
6、criteria//创建关联到某个类的查询对象
ICriteria criteria = session.CreateCriteria(typeof(Person));//添加表达式 criteria.Add(Expression.Eq("Name","Jackie Chan")); IList list = criteria.List(); orICriteria criteria = session.CreateCriteria(typeof(Person)); Person person = new Person(); person.Name = "Jackie Chan"; person.Age = 50;//创建一个Example对象 criteria.Add(Example.Create(person)); IList list = criteria.List();
我们想对返回的list进行排序,该怎么办呢?如下:
ICriteria criteria = session.CreateCriteria(typeof(Person)); criteria.Add(Expression.Gt("Age", 20)); //添加一个排序对象 criteria.AddOrder(Order.Asc("Age")); IList list = criteria.List();
限制记录范围在显示大量的记录时,我们常常采用的方法就是分页,如果用NHibernate来做,该怎么办呢?如下代码:
ICriteria criteria = session.CreateCriteria(typeof(Person));//从第10条记录开始取 criteria.SetFirstResult(10);//取20条记录 criteria.SetMaxResults(20); IList list = criteria.List();
Refer:http://tech.it168.com/zt/nhibernate/index.html
http://www.hibernate.org/363.html
http://www.yoda.arachsys.com/csharp/singleton.html