以前只听说过NHibernate,没有在实际的开发中使用过,现在做项目要用到,又要耗费不少脑细胞了,痛苦。。。。
Nhibernate是基于ms.net的O/R Mapping持久框架,它从基于Java的Hibernate项目移植而来。O/R Mapping就是把对象到映射关系数据库的记录,简单的说就是能实现把一个对象存储为数据表中的一条记录和由一条记录创建一个相应的对象,数据表中的数据就是对象的属性。还好以前对java平台下的Hibernate有所了解。。ok,go go go:
新建一个web项目(NHibernateDemo),引用NHibernate.dll组件(先做个简单的),在Web.config中添加配置文件,指定所要连接的数据库:
配置数据库信息
1 <configSections>
2 <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
3 </configSections>
4 <nhibernate>
5 <add key="hibernate.show_sql"
6 value="true"
7 />
8 <add key="hibernate.connection.provider"
9 value="NHibernate.Connection.DriverConnectionProvider"
10 />
11 <add key="hibernate.dialect"
12 value="NHibernate.Dialect.MsSql2000Dialect"
13 />
14 <add key="hibernate.connection.driver_class"
15 value="NHibernate.Driver.SqlClientDriver"
16 />
17 <add key="hibernate.connection.connection_string"
18 value="Initial Catalog=test;Data Source=COMPUTER;User Id=sa;Password=123"
19 />
20
21 </nhibernate>
1 <configSections>
2 <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
3 </configSections>
4 <nhibernate>
5 <add key="hibernate.show_sql"
6 value="true"
7 />
8 <add key="hibernate.connection.provider"
9 value="NHibernate.Connection.DriverConnectionProvider"
10 />
11 <add key="hibernate.dialect"
12 value="NHibernate.Dialect.MsSql2000Dialect"
13 />
14 <add key="hibernate.connection.driver_class"
15 value="NHibernate.Driver.SqlClientDriver"
16 />
17 <add key="hibernate.connection.connection_string"
18 value="Initial Catalog=test;Data Source=COMPUTER;User Id=sa;Password=123"
19 />
20
21 </nhibernate>
以上添加到<system.web>上面。
再新建一个类库项目(NHibernateDemoClass),用来配置业务数据域:
配置一个数据映射(新建一个xml文件:user.hbm.xml)
xml数据域
1<?xml version="1.0" encoding="utf-8" ?>
2<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
3 <class name="NHibernateDemoClass.NDUser,NHibernateDemoClass" table="Users">
4 <id name="Id" column="LogonId" type="String" length="20">
5 <generator class="assigned" />
6 </id>
7 <property name="UserName" column="Name" type="String" length="40" />
8 <property name="Password" type="String" length="20" />
9 <property name="EmailAddress" type="String" length="40" />
10 <property name="LastLogon" type="DateTime" />
11 </class>
12</hibernate-mapping>
1<?xml version="1.0" encoding="utf-8" ?>
2<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
3 <class name="NHibernateDemoClass.NDUser,NHibernateDemoClass" table="Users">
4 <id name="Id" column="LogonId" type="String" length="20">
5 <generator class="assigned" />
6 </id>
7 <property name="UserName" column="Name" type="String" length="40" />
8 <property name="Password" type="String" length="20" />
9 <property name="EmailAddress" type="String" length="40" />
10 <property name="LastLogon" type="DateTime" />
11 </class>
12</hibernate-mapping>
在解决方案资源管理器中,将该xml文件(user.hbm.xml)的属性生成操作修改为嵌入的资源(我可是没有看仔细,浪费了一个多小时的时间)。
创建一个.net的实体对应以上的xml文件(NDUser类)
定义一个实体类
1private string id;
2 private string name;
3 private string password;
4 private string emailAddress;
5 private DateTime lastLogon;
6
7 public string Id
8 {
9 get { return id; }
10 set { id = value; }
11 }
12 public string UserName
13 {
14 get { return name; }
15 set { name = value; }
16 }
17 public string Password
18 {
19 get { return password; }
20 set { password = value; }
21 }
22 public string EmailAddress
23 {
24 get { return emailAddress; }
25 set { emailAddress = value; }
26 }
27 public DateTime LastLogon
28 {
29 get { return lastLogon; }
30 set { lastLogon = value; }
31 }
1private string id;
2 private string name;
3 private string password;
4 private string emailAddress;
5 private DateTime lastLogon;
6
7 public string Id
8 {
9 get { return id; }
10 set { id = value; }
11 }
12 public string UserName
13 {
14 get { return name; }
15 set { name = value; }
16 }
17 public string Password
18 {
19 get { return password; }
20 set { password = value; }
21 }
22 public string EmailAddress
23 {
24 get { return emailAddress; }
25 set { emailAddress = value; }
26 }
27 public DateTime LastLogon
28 {
29 get { return lastLogon; }
30 set { lastLogon = value; }
31 }
通过以上的配置,现在在web项目中,进行对数据库的简单操作:
添加以下的引用:
using NHibernate;
using NHibernate.Cfg;
using NHibernateDemoClass;
using NHibernate.Expression;
添加数据
1//增加:
2 Configuration cfg = new Configuration();
3 cfg.AddAssembly( "NHibernateDemoClass");
4 ISessionFactory factory = cfg.BuildSessionFactory();
5 ISession session = factory.OpenSession();
6 ITransaction t=session.BeginTransaction();
7 NHibernateDemoClass.NDUser nUser = new NDUser();
8 nUser.Id = "123";
9 nUser.Password = "123" ;
10 nUser.LastLogon = DateTime.Now;
11 nUser.UserName = "test";
12 nUser.EmailAddress = @"56789@163.com";
13 session.Save(nUser);
14 t.Commit();
15 session.Close();
1//增加:
2 Configuration cfg = new Configuration();
3 cfg.AddAssembly( "NHibernateDemoClass");
4 ISessionFactory factory = cfg.BuildSessionFactory();
5 ISession session = factory.OpenSession();
6 ITransaction t=session.BeginTransaction();
7 NHibernateDemoClass.NDUser nUser = new NDUser();
8 nUser.Id = "123";
9 nUser.Password = "123" ;
10 nUser.LastLogon = DateTime.Now;
11 nUser.UserName = "test";
12 nUser.EmailAddress = @"56789@163.com";
13 session.Save(nUser);
14 t.Commit();
15 session.Close();
删除数据
1//删除:
2 Configuration cfg = new Configuration();
3 cfg.AddAssembly( "NHibernateDemoClass");
4 ISessionFactory factory = cfg.BuildSessionFactory();
5 ISession session = factory.OpenSession();
6 ITransaction t=session.BeginTransaction();
7 NDUser nUser=(NDUser) session.Load(typeof(NDUser),"21");//查找数据表中所要记录
8 session.Delete(nUser);//向数据库删除数据
9 t.Commit();
10 session.Close();
1//删除:
2 Configuration cfg = new Configuration();
3 cfg.AddAssembly( "NHibernateDemoClass");
4 ISessionFactory factory = cfg.BuildSessionFactory();
5 ISession session = factory.OpenSession();
6 ITransaction t=session.BeginTransaction();
7 NDUser nUser=(NDUser) session.Load(typeof(NDUser),"21");//查找数据表中所要记录
8 session.Delete(nUser);//向数据库删除数据
9 t.Commit();
10 session.Close();
修改数据
1//修改:
2 Configuration cfg = new Configuration();
3 cfg.AddAssembly( "NHibernateDemoClass");
4 ISessionFactory factory = cfg.BuildSessionFactory();
5 ISession session = factory.OpenSession();
6 ITransaction t=session.BeginTransaction();
7 NDUser nUser=(NDUser) session.Load(typeof(NDUser),"21");//查找数据表中所要记录
8 nUser.UserName = "test2";
9 session.Update(nUser);
10 t.Commit();
11 session.Close();
1//修改:
2 Configuration cfg = new Configuration();
3 cfg.AddAssembly( "NHibernateDemoClass");
4 ISessionFactory factory = cfg.BuildSessionFactory();
5 ISession session = factory.OpenSession();
6 ITransaction t=session.BeginTransaction();
7 NDUser nUser=(NDUser) session.Load(typeof(NDUser),"21");//查找数据表中所要记录
8 nUser.UserName = "test2";
9 session.Update(nUser);
10 t.Commit();
11 session.Close();
ok,以上只是简单的配置和操作而已。。接下来就研究多表关联了。。。