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.Search
5             User singleUser = (User)session.Get(typeof(User), "andy");
6             Console.WriteLine("name:{0},age:{1}", singleUser.UserName, singleUser.Age);

 

NHibernate的使用,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。