nhibernate.cfg.xml配置
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="Dao"><property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<!--数据库用户密码-->
<property name="connection.connection_string">Server=.; database=NHibernate;uid=sa;pwd=123456;</property>
<!--是否显示sql语句--><property name="show_sql">true</property>
<!--nhibernate方言--><property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="command_timeout">60</property><!--数据库操作的方式-->
<property name="hbm2ddl.auto">update</property><property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<!--找到对应的实体对象--><mapping assembly ="Model"/></session-factory>
</hibernate-configuration>
Entity中的实体类:
public class Customer
{
public virtual Guid id { get; set; }
public virtual string name { get; set; }
public virtual string phone { get; set; }
public virtual string city { get; set; }
public virtual string address { get; set; }
}
Mapping中每个实体类对应的nhibernate映射文件:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Model" namespace="Model">
<class name="Customer" table="Customer" lazy="true">
<id name="id" column="id" type="Guid">
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="name" length="50"/>
</property>
<property name="city" type="string">
<column name="city" length="50"/>
</property>
<property name="phone" type="string">
<column name="phone" length="50"/>
</property>
<property name="address" type="string">
<column name="address" length="50"/>
</property>
</class>
</hibernate-mapping>
//NHibernateHelper帮助类 实例session工厂的单例对象
public class NHibernateHelper
{
private static ISessionFactory _sessionFactory;
public static ISession GetSession()
{
if (_sessionFactory == null)
_sessionFactory = new Configuration().Configure(
AppDomain.CurrentDomain.SetupInformation.ApplicationBase
+ "../Model/hibernate.cfg.xml").BuildSessionFactory();
return _sessionFactory.OpenSession();
}
}
//BLL层
public class CustomerBussiness
{
private CustomerData _customerData;
public CustomerBussiness()
{
_customerData = new CustomerData();
}
public IList<Customer> GetCustomerList(Expression<Func<Customer,bool>> where)
{
return _customerData.GetCustomerList(where);
}
}
//DAL层
public class CustomerData
{
public IList<Customer> GetCustomerList(Expression<Func<Customer,bool>> where)
{
try
{
ISession session = NHibernateHelper.GetSession();
return session.Query<Customer>().Select(x => new Customer
{
name = x.name,
city = x.city,
address = x.address,
phone = x.phone
}).Where(where).ToList();
}
catch (Exception e)
{
throw e;
}
}
}
//UI层Controller
public class indexController : Controller
{
// GET: index
public ActionResult Index()
{
CustomerBussiness customerBussiness = new CustomerBussiness();
var result = customerBussiness.GetCustomerList(c=>1==1);
foreach (var re in result)
Response.Write(re.name+" "+re.phone+" "+re.city+" "+re.address);
return View();
}
}