首页 > 代码库 > NHibernate的使用

NHibernate的使用

hibernate.cfg.xml

 1 <?xml version="1.0" encoding="utf-8" ?> 2 <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" > 3     <session-factory name="NHibernate.Test"> 4         <property name="connection.connection_string"> 5             Data Source=.;database=NHibernate;Integrated Security=True;Pooling=False 6         </property> 7         <property name="show_sql">true</property> 8         <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> 9         <mapping assembly="NHibernateSpringTest"/>10     </session-factory>11 </hibernate-configuration>

User.hbm.xml

 1 <?xml version="1.0" encoding="utf-8" ?> 2 <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> 3     <class name="NHibernateSpringTest.User, NHibernateSpringTest" table="my_users"> 4         <id name="LoginId" column="LogonId" type="String" length="20"> 5             <generator class="assigned" /> 6         </id> 7         <property name="UserName" column="UserName" type="String" length="40"/> 8         <property name="Age" column="Age" type="Int32"/> 9     </class>10 </hibernate-mapping>

Program.cs

1             Configuration cfg = new Configuration().Configure();2             ISessionFactory factory = cfg.BuildSessionFactory();3             ISession session = factory.OpenSession();4             //1.Search5             User singleUser = (User)session.Get(typeof(User), "andy");6             Console.WriteLine("name:{0},age:{1}", singleUser.UserName, singleUser.Age);