NHibernate(一)

6 篇文章 0 订阅
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();
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值