1 连接数据库
MySQL:
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
......
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="NHibernate.Test">
<property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
<property name="connection.connection_string">Database=数据库名称;Data Source=数据库所在服务器(localhost或IP地址);User Id=root;Password=密码; allow zero datetime=true</property>
<property name="dialect">NHibernate.Dialect.MySQLDialect</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
SQL Server 2005:
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
......
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="NHibernate.Test">
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver </property>
<property name="connection.connection_string">initial catalog=数据库名;Data Source=数据库服务器;Integrated Security=SSPI; </property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
2 映射文件XX.hbm.xml配置
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Model" assembly="Model">
<class name="Model.DBModel.Tbstudent,Model" table="Tbstudent" lazy="true">
<id name="ID" column="ID" type="int">
<generator class="native">
<param name="sequence"></param>
</generator>
</id>
<property type="int" name="Sex" />
<property type="DateTime" length="30" name="Birthday" />
<property type="string" name="name" />
<!--多对一-->
<many-to-one name="Tbbanji" cascade="none" column="Tbbanji" not-null="true" lazy="false" />
<!--一对多 inverse设为false的话删除该表的时候,学生对应得选课表不删除,但引用此表的外键字段自动设为NULL-->
<bag name="Fkxuanke" inverse="true" lazy="false" cascade="delete">
<key column="Tbxuanke" />
<one-to-many class="Model.DBModel.Tbxuanke,Model" />
</bag>
</class>
</hibernate-mapping>
666

被折叠的 条评论
为什么被折叠?



