用到的表
Create table UserInfo
(
id int identity(1,1) not null primary key,
userName varchar(50) not null,
pwd varchar(50) not null,
age int not null
)
DEMO项目结构:
DomainModel层
UserInfo.cs
using System;
namespace DomainModel
{
public class UserInfo
{
public virtual int Id { get; set; }
public virtual string UserName { set; get; }
public virtual string Pwd { set; get; }
public virtual int Age { set; get; }
}
}
UserInfo.hbm.xml(设置生产操作为嵌入的资源)
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="DomainModel" namespace="DomainModel">
<class name ="DomainModel.UserInfo,DomainModel" table="UserInfo">
<id name="Id" column="id" type="Int32">
<generator class ="native"></generator>
</id>
<property name="UserName" column ="userName" type="String" length="50" not-null="true"/>
<property name ="Pwd" column="pwd" type="String" length="50" not-null="true"/>
<property name ="Age" column="age" type="Int32" not-null="true"/>
</class>
</hibernate-mapping>
DAL 层引入
Castle.Core.dll
Castle.DynamicProxy2.dll
Iesi.Collections.dll
NHibernate.dll
NHibernateHelper.cs
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
namespace DAL
{
public static class NHibernateHelper
{
private static readonly ISessionFactory sessionFactory;
private static ISession session = null;
static NHibernateHelper()
{
sessionFactory = new Configuration().Configure().BuildSessionFactory();
}
public static ISession GetCurrentSession()
{
if (session == null)
{
session = sessionFactory.OpenSession();
}
return session;
}
public static void CloseSessionFactory()
{
if (sessionFactory != null)
{
sessionFactory.Close();
}
}
}
}
UserService.cs
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using DomainModel;
namespace DAL
{
public class UserService
{
//根据ID 获得该用户
public UserInfo GetUserInfoById(int id)
{
return NHibernateHelper.GetCurrentSession().Get<UserInfo>(id);
}
}
}
DALTest层
引入nunit.framework.dll
hibernate.cfg.xml(在该层的bin目录下)
<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory >
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Data Source=.\SQLEXPRESS;initial catalog=demodb;User Id=sa;Password=sa;</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="use_outer_join">true</property>
<mapping assembly="DomainModel" />
</session-factory>
</hibernate-configuration>
DALTest.cs(该类查看是否运行成功)
using System;
using System.Collections.Generic;
using System.Text;
using DomainModel;
using NUnit.Framework;
using DAL;
namespace DALTest
{
[TestFixture]
public class DALTestCase
{
public UserService userService = null;
[SetUp]
public void SetUp()
{
userService = new UserService();
}
[Test]
public void TestMethod()
{
//数据库中ID为1的用户名为zhang
var userInfo = userService.GetUserInfoById(1);
Assert.AreEqual(userInfo.UserName, "zhang");
}
}
}
控制台输出:
------ Test started: Assembly: DALTest.dll ------
NHibernate: SELECT userinfo0_.id as id0_0_, userinfo0_.userName as userName0_0_, userinfo0_.pwd as pwd0_0_, userinfo0_.age as age0_0_ FROM UserInfo userinfo0_ WHERE userinfo0_.id=@p0; @p0 = '1'
1 passed, 0 failed, 0 skipped, took 14.02 seconds (NUnit 2.4).
可以看出该断言成功 并且有sql语句打印出来 说明配置成功!