学习使用NHibernate2.1.0Beta1(五)— 创建站点测试
1、首先,创建一个ASP.NET站点项目
2、添加引用:
1)引用DALayer.dll程序集
2)把OrmCodeGenerator输出目录下的hibernate.cfg.xml添加到bin目录下。我下载的OrmCodeGenerator默认输出的默认文件名是hibernate.cfg.config,你可以将其扩展名更改为XML或者在OrmCodeGenerator源码中修改输出文件的扩展名。
3、在Default.aspx的前台界面拖放一个GridView控件,然后在Page_Load中写如下代码:
Code
测试通过!
4、配置数据库连接字符串
1)在Web.config中配置
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Test.DebugConnectionProvider, NHibernate.Test</property>
<property name="cache.provider_class">NHibernate.Cache.HashtableCacheProvider, NHibernate</property>
<property name="cache.use_query_cache">true</property>
<property name="connection.isolation">ReadCommitted</property>
<!-- This is the System.Data.dll provider for MSSQL Server -->
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">
Server=(local);initial catalog=demo;uid=sa;pwd=123
</property>
<property name="show_sql">false</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="use_outer_join">true</property>
<property name="command_timeout">444</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
</session-factory>
</hibernate-configuration>
<!-- This section contains the log4net configuration settings -->
<log4net debug="false">
<!-- Define some output appenders -->
<appender name="trace" type="log4net.Appender.TraceAppender, log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="console" type="log4net.Appender.ConsoleAppender, log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="log.txt" />
<param name="AppendToFile" value="false" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy.MM.dd" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default priority -->
<root>
<priority value="WARN" />
<appender-ref ref="console" />
</root>
</log4net>
</configuration>
<configuration>
<configSections>
<section name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Test.DebugConnectionProvider, NHibernate.Test</property>
<property name="cache.provider_class">NHibernate.Cache.HashtableCacheProvider, NHibernate</property>
<property name="cache.use_query_cache">true</property>
<property name="connection.isolation">ReadCommitted</property>
<!-- This is the System.Data.dll provider for MSSQL Server -->
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">
Server=(local);initial catalog=demo;uid=sa;pwd=123
</property>
<property name="show_sql">false</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="use_outer_join">true</property>
<property name="command_timeout">444</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
</session-factory>
</hibernate-configuration>
<!-- This section contains the log4net configuration settings -->
<log4net debug="false">
<!-- Define some output appenders -->
<appender name="trace" type="log4net.Appender.TraceAppender, log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="console" type="log4net.Appender.ConsoleAppender, log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="log.txt" />
<param name="AppendToFile" value="false" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy.MM.dd" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default priority -->
<root>
<priority value="WARN" />
<appender-ref ref="console" />
</root>
</log4net>
</configuration>
修改你的数据库版本和连接字符串。
更改DAL项目中所有文件中的如下一行代码
ISessionFactory factory = new Configuration().Configure().BuildSessionFactory();
为
ISessionFactory factory = new Configuration().AddAssembly("AppModel").Configure().BuildSessionFactory();
站点中添加对NHibernate.Test.dll的引用。”AppModel“是Model项目程序集名称。
2)使用单独配置文件
使用代码生成器生成的hibernate.cfg.xml,已拷贝到站点的bin目录下,修改其中的数据库连接字符串就可以了,DAL层中依然使用
ISessionFactory factory = new Configuration().Configure().BuildSessionFactory();
创建ISessionFactory实例。<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory >
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Server=.;initial catalog=Demo;User Id=sa;Password=123;</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="use_outer_join">true</property>
<mapping assembly="AppModel" />
</session-factory>
</hibernate-configuration>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory >
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Server=.;initial catalog=Demo;User Id=sa;Password=123;</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="use_outer_join">true</property>
<mapping assembly="AppModel" />
</session-factory>
</hibernate-configuration>