C#.NET编程----Spring.NET NHibernate整合
添加Spring.NET為NHibernate的容器配置
現在就可以在Spring.NET的容器中添加Nhibernate的配置了。
如下Spring_nhibernate.xml:
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns='http://www.springframework.net'>
<!-- NHibernate初始化的 -->
<object id="DbProvider" type="OKEC.Sample.Spring.SQLProvider,SpringNHibernateSample">
????<property name="ConnectionString" value="Data Source=192.168.88.15;Database=liluhua;User ID=sa;Password=sa;Trusted_Connection=False"/>
</object>
<object id="SessionFactory"
type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate">
????<property name="DbProvider" ref="DbProvider"/>
????<property name="MappingAssemblies">
????????<list>
????????????<value>SpringNhibernateSample</value>
????????</list>
????</property>
????<property name="HibernateProperties">
????????<dictionary>
????????????<entry
key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
????????????<!--entry
key="hibernate.connection.connection_string" value="Data Source=192.168.188.188;Database=Test;User ID=satest;Password=satest;Trusted_Connection=False"/-->
????????????<entry key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
????????????<entry
key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>??
????????</dictionary>
????</property>
</object>
<object id="HibernateTransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate">
????<property name="DbProvider" ref="DbProvider"/>
????<property name="sessionFactory" ref="SessionFactory"/>
</object>
<object id="TransactionInterceptor"
type="Spring.Transaction.Interceptor.TransactionInterceptor, Spring.Data">
????<property name="TransactionManager" ref="HibernateTransactionManager"/>
????<property name="TransactionAttributeSource">
????????<object
type="Spring.Transaction.Interceptor.AttributesTransactionAttributeSource, Spring.Data"/>
????</property>
</object>
<!-- 以下是業務相關的 -->
<object id="UserDao"
type="OKEC.Sample.NHibernate.NHibernateTest.UserDao, SpringNHibernateSample">
????????<property name="SessionFactory" ref="SessionFactory"/>
</object>
</objects>
我們現在對上面的加以細解:
下面這幾行,是配置Nhibernate所需的數據庫的DbProvider
<object id="DbProvider" type="OKEC.Sample.Spring.SQLProvider,SpringNHibernateSample">
????<property name="ConnectionString" value="Data Source=192.168.88.15;Database=liluhua;User ID=sa;Password=sa;Trusted_Connection=False"/>
</object>
下面的是對Nhibernate的SessionFactory的封裝的對像的定義
<object id="SessionFactory"
type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate">
????<property name="DbProvider" ref="DbProvider"/>
????<property name="MappingAssemblies">
????????<list>
????????????<value>SpringNhibernateSample</value>
????????</list>
????</property>
????<property name="HibernateProperties">
????????<dictionary>
????????????<entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
????????????<!--entry key="hibernate.connection.connection_string" value="Data Source=192.168.188.188;Database=Test;User ID=satest;Password=satest;Trusted_Connection=False"/-->
????????????<entry key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
????????????<entry key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>??
????????</dictionary>
????</property>
</object>
下面的是對Nhibernate中的Transaction封裝對像
<object id="HibernateTransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate">
????<property name="DbProvider" ref="DbProvider"/>
????<property name="sessionFactory" ref="SessionFactory"/>
</object>
<object id="TransactionInterceptor" type="Spring.Transaction.Interceptor.TransactionInterceptor, Spring.Data">
????<property name="TransactionManager" ref="HibernateTransactionManager"/>
????<property name="TransactionAttributeSource">
????????<object type="Spring.Transaction.Interceptor.AttributesTransactionAttributeSource, Spring.Data"/>
????</property>
</object>
下面是對NHibernate業務操作對像的定義
<object id="UserDao"
type="OKEC.Sample.NHibernate.NHibernateTest.UserDao, SpringNHibernateSample">
????????<property name="SessionFactory" ref="SessionFactory"/>
</object>
總結
以上是生活随笔為你收集整理的C#.NET编程----Spring.NET NHibernate整合的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 速度最快的动物前十名(速度最快的动物)
- 下一篇: 转 Spring.NET 与 NHibe