码迷,mamicode.com
首页 > 编程语言 > 详细

Spring.Net2.0+NHibernate4.0 +Asp.Net Mvc4 一

时间:2015-05-08 14:39:28      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:

1.创建项目结构

   控制器:    SN.Controllers

   数据访问 :SN.Dao

   实体映射: SN.Models

   服务层:     SN.Servers

   视图层:   SN.Web

2.添加需要插件

  Tools=>Manage Nuget  Package

  A.  Spring.Net2.0

  B.  NHibernate4.0

  C.  Spring.Wb.Mvc4

3.SN.Models

  文件夹:Mappings(User.hbm.xml)

  文件:User.cs

User.hbm.xml

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
 3   <class name="SN.Models.User,SN.Models" table="SN_User" lazy="false">
 4     <id name="Id" column="Id" type="Int32">
 5       <generator class="native" />
 6     </id>
 7     <property name="Name" column="SName" type="String" length="20" />
 8     <property name="NickName" column="SNickName" type="String" length="20" />
 9     <property name="PassWord" column="SPassWord" type="String" length="30" />
10     <property name="IdentifyId" column="SIdentifyId" type="String" length="30" />
11     <property name="Phone" column="SPhone" type="String" length="50" />
12     <property name="Email" column="SEmail" type="String" length="50" />
13     <property name="CreateTime" column="DCreateDate" type="DateTime" />
14     <property name="Creator" column="SCreator" type="String" length="20" />
15     <property name="LastTimeLogOn" column="DLastTimeLogOn" type="DateTime" />
16   </class>
17 </hibernate-mapping>

User.cs

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 namespace SN.Models
  7 {
  8 
  9 
 10       [Serializable]
 11       public class User
 12       {
 13           /// <summary>
 14           /// 用户id
 15           /// </summary>
 16           public Int32 Id
 17           {
 18               get;
 19               set;
 20           }
 21 
 22           /// <summary>
 23           /// 用户名,登录所用的名字
 24           /// </summary>
 25           public string Name
 26           {
 27               get;
 28               set;
 29           }
 30 
 31           /// <summary>
 32           /// 真实姓名
 33           /// </summary>
 34           public string NickName
 35           {
 36               get;
 37               set;
 38           }
 39 
 40           /// <summary>
 41           /// 密码
 42           /// </summary>
 43           public string PassWord
 44           {
 45               get;
 46               set;
 47           }
 48 
 49           /// <summary>
 50           /// 创建日期
 51           /// </summary>
 52           public DateTime CreateTime
 53           {
 54               get;
 55               set;
 56           }
 57 
 58           /// <summary>
 59           /// 创建人
 60           /// </summary>
 61           public string Creator
 62           {
 63               get;
 64               set;
 65           }
 66 
 67           /// <summary>
 68           /// Email
 69           /// </summary>
 70           public string Email
 71           {
 72               get;
 73               set;
 74           }
 75 
 76           /// <summary>
 77           /// 联系电话
 78           /// </summary>
 79           public string Phone
 80           {
 81               get;
 82               set;
 83           }
 84 
 85           /// <summary>
 86           /// 身份证
 87           /// </summary>
 88           public string IdentifyId
 89           {
 90               get;
 91               set;
 92           }
 93 
 94           /// <summary>
 95           /// 最后一次登录时间
 96           /// </summary>
 97           public DateTime LastTimeLogOn
 98           {
 99               get;
100               set;
101           }
102       }
103     
104 }

4.SN.Dao

   文件夹:Config(Objects.xml  SpringNHibernate.xml)

  文件夹:Dao(UserDao.cs)

  文件夹:IDao(IUserDao.cs)

  文件:HibernateDao.cs

Objects.xml

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <objects xmlns="http://www.springframework.net"
 3          xmlns:tx="http://www.springframework.net/tx">
 4 
 5   <object id="UserDaoImpl" type="SN.Dao.UserDao, SN.Dao"    >
 6     <!-- ref 表示引用的对象 -->
 7     <property name="SessionFactory" ref="NHibernateSessionFactory"  />
 8   </object>
 9 
10 </objects>

SpringNHibernate.xml

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <objects xmlns="http://www.springframework.net"
 3          xmlns:db="http://www.springframework.net/database"
 4          xmlns:tx="http://www.springframework.net/tx"
 5          >
 6   <!--描述-->
 7   <description>
 8     数据访问的配置信息
 9   </description>
10 
11   <!-- 通过主应用程序的上下文配置文件引用 -->
12   <object type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core">
13     <property name="ConfigSections" value="databaseSettings"/>
14   </object>
15   <!--  connectionString=""-->
16   <!-- 数据库的配置 -->
17   <db:provider id="DbProvider"
18                   provider="System.Data.SqlClient"
19                   connectionString="server=${db.server};Database=${db.database};uid=${db.user};pwd=${db.password}"
20               />
21 
22   <!-- NHibernate 配置 -->
23 
24   <!-- 可以通过 name 为其指定别名 name="SessionFactory" -->
25   <object id="NHibernateSessionFactory"
26           type="Spring.Data.NHibernate.LocalSessionFactoryObject,Spring.Data.NHibernate4" >
27 
28     <!-- 关于数据库连接的配置,直接使用 DbProvider 中的设置,这样,不需要为 Hibernate 再提供连接串和驱动 -->
29     <property name="DbProvider" ref="DbProvider"/>
30 
31     <!-- 包含有映射文件的程序集,需要分析的hbm程序集名称 -->
32     <property name="MappingAssemblies">
33       <list>
34         <value>SN.Models</value>
35       </list>
36     </property>
37 
38     <!-- 其他的参数 -->
39     <property name="HibernateProperties">
40       <dictionary>
41         <!-- 方言 -->
42         <entry key="dialect" value="NHibernate.Dialect.MsSql2008Dialect"/>
43         <entry key="use_proxy_validator" value="false" />
44         <entry key="show_sql" value="true"/>
45       </dictionary>
46     </property>
47 
48     <!-- 必须增加此项说明,与 Spring 的声明式事务集成 -->
49     <property name="ExposeTransactionAwareSessionFactory" value="true" />
50     
51   </object> 
52 </objects>

UserDao.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using Spring.Stereotype;
 6 using Spring.Transaction.Interceptor;
 7 using SN.Models;
 8 
 9 namespace SN.Dao
10 {
11      [Repository]
12     public class UserDao : HibernateDao, IUserDao
13     {
14 
15        public IList<User> GetAll()
16        {
17            return GetAll<User>();
18        }
19        public IList<User> GetAllUsers()
20        {
21          return GetAll<User>();
22          //return new List<User> { new User { Id = 1, Name = "sulin" } };
23        }
24     }
25 }

IUserDao.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using SN.Models;
 6 namespace SN.Dao
 7 {
 8    public interface IUserDao
 9     {
10        IList<User> GetAllUsers();
11     }
12 }

HibernateDao.cs

 1 using NHibernate;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 
 7 namespace SN.Dao
 8 {
 9     public abstract class HibernateDao
10     {
11         private ISessionFactory sessionFactory;
12 
13         /// <summary>
14         /// Session factory for sub-classes.
15         /// </summary>
16         public ISessionFactory SessionFactory
17         {
18             protected get { return sessionFactory; }
19             set { sessionFactory = value; }
20         }
21 
22         /// <summary>
23         /// Get‘s the current active session. Will retrieve session as managed by the 
24         /// Open Session In View module if enabled.
25         /// </summary>
26         protected ISession CurrentSession
27         {
28             get { return sessionFactory.GetCurrentSession(); }
29         }
30         protected IList<T> GetAll<T>() where T : class
31         {
32             ICriteria criteria = CurrentSession.CreateCriteria<T>();
33             return criteria.List<T>();
34         }
35     }
36 }

5.SN.Servers

  文件夹:Config(Servers.xml)

  文件夹:Services(UserService.cs)

  文件夹:IServices(IUserService.cs)

Servers.xml

1 <?xml version="1.0" encoding="utf-8" ?>
2 <objects xmlns="http://www.springframework.net">
3 
4     <object id="UserServiceImpl" type="SN.Servers.UserService,SN.Servers"    >
5         <!-- ref 表示引用的对象 -->
6         <property name="IUser" ref="UserDaoImpl"   />
7     </object>
8 
9 </objects>

IUserService.cs

 1 using SN.Dao;
 2 using SN.Models;
 3 using System;
 4 using System.Collections.Generic;
 5 using System.Linq;
 6 using System.Text;
 7 
 8 namespace SN.Servers
 9 {
10     public interface IUserService
11     {
12         IUserDao IUser { set; get; }
13         IList<User> GetAllUsers();
14     }
15 }

UserService.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace SN.Servers
 7 {
 8 
 9   public  class UserService:IUserService
10     {
11 
12         public Dao.IUserDao IUser
13         {
14             get;
15             set;
16         }
17 
18         public IList<Models.User> GetAllUsers()
19         {
20             return IUser.GetAllUsers();
21         }
22     }
23 }

 

  

Spring.Net2.0+NHibernate4.0 +Asp.Net Mvc4 一

标签:

原文地址:http://www.cnblogs.com/linsu/p/4487529.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!