在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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中的代码: 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:
CMS.Model
{ public class User { public int UserID { get; set; } public string UserName { get; set; } public string UserPwd { get; set; } } } User.hbm.xml:(记得要设置成“嵌入的资源”) <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类库中新增:
CMS.IDAL
{ public interface IUserDao { void Save(CMS.Model.User user); } } 在CMS.DAL类库中新增对上面接口的实现: 先引用: CMS.Model CMS.IDAL spring.core spring.data spring.data.nhibernate20
CMS.DAL
{ public class UserDao : HibernateDaoSupport, IUserDao { #region IUserDao 成员 public void Save(CMS.Model.User user) { this.HibernateTemplate.Save(user); } #endregion } } web_business.xml: <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: <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
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); } } } 运行效果: 源码下载 |
请发表评论