因为最近无意中学了AOP ,所以想一探究竟,看看.net里这个Spring.Net 到底是怎么回事,请有需要的童鞋往下,不需要的请alt+w。因为是先配置的 Nhibernate 所以就从这个开始。开发环境:VS2012

1. 下载Nhibernate 我这里用的是 NHibernate-3.3.2.GA-bin

2. 添加映射文件 *.hbm.xml 当然 你可以使用映射或者其他一些方式 不过这个比较常用 需要注意的地方有如下几点:

    1.这个映射文件需要在属性中设置为 嵌入的资源,如果丢了这一步 可能会异常。

    2. 在映射文件里, 程序集以及命名空间很重要。

        <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="TestNHISpringIntegral.Core" namespace="TestNHISpringIntegral.Core.Model">

       这个对应的是model的。

   3. 列名一定要对应上,主键外键这些的设置也要注意,在这里就不再赘述。 例子如下:

   

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="TestNHISpringIntegral.Core" namespace="TestNHISpringIntegral.Core.Model">
 3 
 4   <class name="Author" table="Authors" lazy="false" >
 5      <cache usage="read-write"/>
 6      <id name="Id" column="Id" type="Int32">
 7       <generator class="identity"/>
 8     </id>
 9     <property name="FirstName" length="50"  type="String" column="FirstName"  />
10     <property name="LastName" length="50"  type="String" column="LastName" />
11   
12   </class>
13 </hibernate-mapping>

 

3.添加 NHibernate 配置文件 从配置模板里复制出对应的配置文件,我这里是sql2005,请童鞋参照自己的数据库进行设置,注意 将此文件放到bin里面,这样方便读取,还要在app.config 或者web.config 里设置下section,因为顺序是先单独配置,然后进行整合所以可能在后续的内容里会重新设置,请童鞋注意。section 必须是放在最前面 否则依然会报错

   1.在app.config 或者web.config里添加如下内容:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
  </configSections>
</configuration>

   2.修改数据库相关配置:

     

 1 <hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
 2   <session-factory>
 3     <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
 4     <property name="connection.connection_string">Server=.\SQLEXPRESS;Database=BookDemo;Integrated Security=SSPI</property>
 5 
 6     <!--<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
 7     <property name="adonet.batch_size">10</property>
 8     <property name="show_sql">false</property>
 9     <property name="use_outer_join">true</property>
10     <property name="command_timeout">60</property>
11     <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
12     <property name="proxyfactory.factory_class">NHibernate.ByteCode.Spring.ProxyFactoryFactory, NHibernate.ByteCode.Spring</property>-->
13 
14     <!-- mapping files 关键配置,否则出异常:No persister for -->
15     <mapping assembly="TestNHISpringIntegral.DAO" />
16   </session-factory>
17 </hibernate-configuration>

      上面注释掉的内容都是在配置完成之后正式使用的时候才需要的内容,所以我这里全部注释掉了。

4.到这里 NHibernate 就配置完成了,童鞋们可以写代码自己测试下 我的代码如下:

   

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using NHibernate;
 6 using NHibernate.Cfg;
 7 
 8 namespace TestNhibreateWithConsole
 9 {
10     public  class ConfigurationHelper
11     {
12         private Configuration _config;
13 
14         private static ISessionFactory _sessionFactory;
15         public ConfigurationHelper()
16         {
17              _config=new Configuration();
18         }
19 
20        
21 
22         private  ISessionFactory SessionFactory
23         {
24             get
25             {
26                 if (_sessionFactory == null)
27                 {
28                                  _config= new Configuration().Configure();
29                     _sessionFactory = _config.BuildSessionFactory();
30                 }
31                 return _sessionFactory;
32             }
33         }
34         public  ISession OpenSession()
35         {
36             return SessionFactory.OpenSession();
37         }
38       
39        
40     }
41 }

 页面上如下:

    

 1 static void Main(string[] args)
 2         {
 3             using (var sessionFactory = new ConfigurationHelper().OpenSession())
 4             {
 5                  
 6                 var user = sessionFactory.CreateCriteria<R.Books>();
 7                 var auList = user.List<R.Books>();
 8                 auList.ToList().ForEach(au => Trace.WriteLine(au.Publisher));
 9           
10             }
11             
12         }

 忘记说明一点 因为这个NHibernate 依赖其他程序集 请一并进行引用。到这里NHibernate 配置就完成了,下面是spring.net 的配置

5. 引用spring.net 相关程序集,因为我这里是使用的web测试的 所以将一些文件都分开了,我的文件目录如图:

   Spring.NET 整合Nhibernate

 6.各个程序集的配置以及使用,我将分步描述

     1.在DAO里面添加Mapping,并添加映射文件。

     2.在core里面添加model 并且添加相应的类,注意 这里spring 使用的是castle 所以记住一定要用virtual 修饰 例子如下

        

 1 using System;
 2 
 3 namespace TestNHISpringIntegral.Core.Model
 4 {
 5     [Serializable]
 6     public class Author
 7     {
 8         public virtual Int32 Id { get; set; }
 9         public virtual String FirstName { get; set; }
10         public virtual String LastName { get; set; }
11     }
12 }

      3.添加IRepository 泛型类 所有的实体都将继承这个。 代码如下
          

1   public  interface IRepository<T>
2     {
3         void Delete(T entity);
4         T Get(object id);
5         object Save(T entity);
6         void Update(T entity);
7     }
View Code

相关文章: