1、在CMS.App新增XML配置文件web_nhibernate.xml并设置为“嵌入的资源”

2、在Web.config中<spring> -> <resource>加入配置:

<resource uri="assembly://CMS.App/CMS.App/web_nhibernate.xml"/>

3、web_nhibernate.xml中的代码:

Code
Code
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
         xmlns:db="http://www.springframework.net/database">
  <!-- 用以我们在其它的应用程序中,配置数据访问 -->
  <object type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core">
    <property name="ConfigSections" value="databaseSettings"/>
  </object>

<!--SessionFactory对象,其中包括一些比较重要的属性 -->
  <object id="NHibernateSessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate20">
    <property name="DbProvider" ref="DbProvider"/>
    <property name="MappingAssemblies">
      <list>
         <!--NHibernate模型和相关配置文件所在的程序集-->
        <value>CMS.Model</value>
      </list>
    </property>
    <property name="HibernateProperties">
      <dictionary>
        <entry key="hibernate.connection.provider"
               value="NHibernate.Connection.DriverConnectionProvider"/>
        <entry key="dialect"
               value="NHibernate.Dialect.MsSql2000Dialect"/>
        <entry key="hibernate.connection.driver_class"
               value="NHibernate.Driver.SqlClientDriver"/>

</dictionary>
    </property>
    <property name="ExposeTransactionAwareSessionFactory" value="true" />
  </object>

<!--将id为NHibernateSessionFactory的对象注入到HibernateTemplate中-->
  <object id="HibernateTemplate" type="Spring.Data.NHibernate.HibernateTemplate">
    <property name="SessionFactory" ref="NHibernateSessionFactory" />
    <property name="TemplateFlushMode" value="Auto" />
    <property name="CacheQueries" value="true" />
  </object>
</objects>

在CMS.MvcWeb中加入对CMS.Model类库的引用(加入对模型层的引用)

NHibernate配置完成,下面是测试:
在CMS.Model新增以下文件:

User.cs:

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CMS.Model
{
    public class User
    {
        public int UserID
        { get; set; }

public string UserName
        { get; set; }

public string UserPwd
        { get; set; }
    }
}

User.hbm.xml:(记得要设置成“嵌入的资源”)

Code
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">

<class name="CMS.Model.User, CMS.Model" table="t_User"  lazy="false">

<id name="UserID" type="Int32" unsaved-value="0">
      <column name="tm_uid" sql-type="int" not-null="true" unique="true"/>
      <generator class="increment" />
    </id>

<property name="UserName"           column="tm_userName"        type="System.String"    length="20"     />
    <property name="UserPwd"            column="tm_userPwd"         type="System.String"    length="32"     />

</class>
</hibernate-mapping>

在CMS.IDAL类库中新增:

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CMS.IDAL
{
    public interface IUserDao
    {
        void Save(CMS.Model.User user);
    }
}

在CMS.DAL类库中新增对上面接口的实现:
先引用:
CMS.Model
CMS.IDAL
spring.core
spring.data
spring.data.nhibernate20

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMS.IDAL;
using Spring.Data.NHibernate.Support;

namespace CMS.DAL
{
    public class UserDao : HibernateDaoSupport, IUserDao
    {
        #region IUserDao 成员

public void Save(CMS.Model.User user)
        {
            this.HibernateTemplate.Save(user);
        }

#endregion
    }
}

web_business.xml:

Code
<?xml version="1.0" encoding="utf-8" ?>
<!-- 业务层的对像定义 DAO & Manager -->
<objects xmlns='http://www.springframework.net'>
  <object id="TestBLL" type="CMS.BLL.TestBLL, CMS.BLL">
  </object>
  
  <object id="UserDao" type="CMS.DAL.UserDao, CMS.DAL">
    <property name="HibernateTemplate" ref="HibernateTemplate"/>
  </object>
</objects>

web_web.xml:

Code
<?xml version="1.0" encoding="utf-8" ?>
<!-- WEB层的页面对像定义 -->
<objects xmlns='http://www.springframework.net'>
  <object id="Default" type="Default.aspx">
    <property name="ITestBLL" ref="TestBLL" />
    <property name="IUserDao" ref="UserDao" />
  </object>
</objects>

在CMS.Web中添加好DAL,IDAL,MODEL的引用

Default.aspx.cs

Code
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using CMS.IBLL;
using CMS.IDAL;

namespace CMS.MvcWeb
{
    public partial class _Default : Page
    {
        public ITestBLL ITestBLL
        { get; set; }

public IUserDao IUserDao
        { get; set; }

public void Page_Load(object sender, System.EventArgs e)
        {
            ITestBLL.Write();

CMS.Model.User user = new CMS.Model.User();
            user.UserName = "admin";
            user.UserPwd = "admin888";

IUserDao.Save(user);
            Response.Write("添加成功!");
            // Change the current path so that the Routing handler can correctly interpret
            // the request, then restore the original path so that the OutputCache module
            // can correctly process the response (if caching is enabled).

//string originalPath = Request.Path;
            //HttpContext.Current.RewritePath(Request.ApplicationPath, false);
            //IHttpHandler httpHandler = new MvcHttpHandler();
            //httpHandler.ProcessRequest(HttpContext.Current);
            //HttpContext.Current.RewritePath(originalPath, false);
        }
    }
}

运行效果:

源码下载

转载于:https://www.cnblogs.com/cjnmy36723/archive/2009/08/08/1541958.html

ASP.NET MVC 1.0 + spring.net 1.2.0.20288 + NHibernate 2.0.1.4000整合笔记(三)——NHibernate配置...相关推荐

  1. ASP.NET MVC 1.0 + spring.net 1.2.0.20288 + NHibernate 2.0.1.4000整合笔记(四)——整合asp.net mvc...

    1.新增加一个类库,命名为CMS.Common 引用: Spring.Core System.Web.MVC System.Web.Abstractions System.Web.Routing; 在 ...

  2. EFMVC - ASP.NET MVC 3 and Entity Framework 4.1 Code First 项目介绍

    项目概述 使用ASP.NET MVC 3.Razor.EF Code First.Unity 2.0 等等技术,演示如何创建一个ASP.NET MVC 3 的范例应用程序. 相关技术帖子: 中文: 使 ...

  3. c .net ajax,Asp.net mvc 2中使用Ajax的三种方式

    在Asp.net MVC中,我们能非常方便的使用Ajax.这篇文章将介绍三种Ajax使用的方式,分别为原始的Ajax调用.Jquery.Ajax Helper.分别采用这三种方式结合asp.net m ...

  4. ASP.NET MVC如何使用Ajax的辅助方法

    前言:前面我们已经简单的介绍过了MVC如何Jquery,因为我们如果使用Ajax的话必须要了解Jquery,这篇博客我们将大致了解一下ASP.NET MVC如何使用Ajax的辅助方法,此博客是我的读书 ...

  5. 一起谈.NET技术,ASP.NET MVC 3 Beta初体验之超酷的Chart

    前面一篇文章:ASP.NET MVC 3 Beta初体验之WebGrid介绍了WebGrid控件的使用,ASP.NET MVC 3 Beta中才内置Chart的.这篇文章中将介绍Chart的使用.包括 ...

  6. asp.net+mvc+html辅助,ASP.NET MVC使用Ajax的辅助的解决方法

    前言:前面我们已经简单的介绍过了MVC如何Jquery,因为我们如果使用Ajax的话必须要了解Jquery,这篇博客我们将大致了解一下ASP.NET MVC如何使用Ajax的辅助方法,此博客是我的读书 ...

  7. ASP.NET MVC入门到精通——Spring.net-业务层仓储

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 上一节,我们已经把项目框架的雏形搭建好了,那么现在我来开始业务实现,在业务实现的过程当中,不断的来完善我们现有的框架. 1.假设我们来做一个 ...

  8. Spring.NET 1.3.1 新特性探索系列1——ASP.NET MVC的依赖注入

    Spring.NET 1.3.1的程序集Spring.Web.Mvc提供对ASP.NET MVC程序的整合.其中SpringControllerFactory类继承自DefaultController ...

  9. Spring.net与Asp.net Mvc结合示例《转载》

    一.介绍 因为项目要用到Ioc框架,所以要为Ioc框架选型,优秀的Ioc框架好几款,例如:sping.net,castle,unity--当然还不止三款,还有其它的Ioc框架,castle跟unity ...

最新文章

  1. MySQL建立的索引看_MYSQL索引问题:索引在查询中如何使用?看了很多资料都只说索引的建立。是否建立了就不用再理会?...
  2. Java写js的Ajax代码_用JS写的一个Ajax库(实例代码)
  3. snmp error on SnmpMgrRequest 40
  4. domino Format函数详解
  5. java中抽象工厂模式_抽象工厂模式(详解版)
  6. 举个栗子!Tableau技巧(96):离线激活和停用 Linux 版 Tableau Server
  7. Hadoop去掉格,换行符,制表符,回车符,换页符【好吧,其实用正则表达式一下子就搞定了】
  8. Deepin 自定义修改窗口圆角大小
  9. Spring中的依赖注入(10级学员 韩晓爽课堂总结)
  10. 抖音直播可以看全场回放了?怎么进行有效复盘?
  11. PAT 乙级 1072 开学寄语 (20分)
  12. uniapp 安卓 长按app 快捷方式 shortcut
  13. java 图片 转像素_使用Java改变图片的像素
  14. 一条命令搞定黑苹果双系统时差
  15. 计算机的发展与什么息息相关,第一单元第二课《计算机的发展与应用》.doc
  16. 鸿蒙系统支持高清通话吗,电信VoLTE开通方法介绍 所有注意点全在这了
  17. GlidedSky爬虫-验证码1
  18. opencv04:鼠标作为画笔以及轨迹栏作为调色板
  19. 一小时上手昇思MindSpore
  20. 第九届蓝桥杯(省赛)美斯坦福共建专业学子捷报频传

热门文章

  1. [bzoj4003][JLOI2015]城池攻占_左偏树
  2. Python安装scikit-learn包
  3. cocos2d-x返回Android游戏黑屏解决办法
  4. 一站式 Java Web 框架 firefly-2.0_07发布
  5. js(Dom+Bom)第三天(1)
  6. 【PHP】xampp配置多个监听端口和不同的网站目录(转)
  7. weblogic12.1.3安装
  8. 【初赛】概率与期望学习笔记
  9. (转)CentOS分区操作详解
  10. iOS 将16进制颜色转换成UIColor