zoukankan      html  css  js  c++  java
  • 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>

  • 相关阅读:
    [翻译]JavaScript Scoping and Hoisting
    SVN服务器的本地搭建和使用(一)
    SVN服务器搭建和使用(二)
    优酷站内获取m3u8地址(转)
    jQuery 获取屏幕高度、宽度
    5. Eclipse下SVN应用
    echo print printf() sprintf()区别
    URL 中,查询字符串与HTML实体冲突,可能带来的问题.
    完美解决failed to open stream: HTTP request failed!
    解决xml_parse(): input conversion failed due to input error
  • 原文地址:https://www.cnblogs.com/soundcode/p/1911847.html
Copyright © 2011-2022 走看看