1、在CMS.App新增XML配置文件web_nhibernate.xml并设置为“嵌入的资源”
2、在Web.config中<spring> -> <resource>加入配置:
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>
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; }
}
}
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>
<?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);
}
}
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
}
}
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>
<?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>
<?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);
}
}
}
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);
}
}
}
运行效果:
源码下载