当前位置:编程学习 > C#/ASP.NET >>

nhibernate 使用高手指教

最近项目中涉及到 nhibernate  ,哪位高手可否分享一下经验?
多谢! --------------------编程问答-------------------- NHibernate是一个面向.NET环境的对象/关系数据库映射工具。
NHibernate管理.NET类到数据库表的映射,还提供数据查询和获取数据的方法,可以大幅度减少开发时人工使用SQL和ADO.NET处理数据的时间。
NHibernate的目标主要是用于与数据持久化相关的编程任务
还有spring.net,LINQ
NHibernate --------------------编程问答-------------------- 谢谢分享! --------------------编程问答-------------------- 看我博客goodhelper.cnblogs.com --------------------编程问答-------------------- 对象/关系数据库映射类库
简单的就是实体类+xml+sessionFactory 结合实现对象持久化到应用程序 --------------------编程问答-------------------- 用映射对应实体查询数据。一般你要先在项目建立一个NHibernate.cfg.xml文件,里面做映射过程的设置。
然后为每个一表格做一个 Entity.cs   Entity.hbm.xml的对应实体。那你就可以利用SessionFactory查询实体的数据。例子:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Model.GoodsEntity,Model" table="Goods">//注意实体对表格的对应
    <id name="Id" column="Id" type="int">
      <generator class="sequence">
        <param name="sequence">SEQ_GOODS</param>
      </generator>
    </id>
    <property name="Name" column="Name" type="string" length="200" not-null="false"></property>
    <property name="Description" column="Description" type="string" length="1000" not-null="false"></property>
    <property name="Brand" column="Brand" type="string" length="500" not-null="false"></property>
    <property name="Price" column="Price" type="double"></property>
    <property name="BasePrice" column="BasePrice" type="double"></property>
  </class>
</hibernate-mapping>

 public class GoodsEntity
    {
        private int _id;
        private string _name;
        private string _description;
        private string _brand;
        private double _price;
        private double _basePrice;

        public virtual int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public virtual string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public virtual string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        public virtual string Brand
        {
            get { return _brand; }
            set { _brand = value; }
        }

        public virtual double Price
        {
            get { return _price; }
            set { _price = value; }
        }

        public virtual double BasePrice
        {
            get { return _basePrice; }
            set { _basePrice = value; }
        }

    }

 public class BookFactory:IBook
 {
        #region IBook 成员
        /// <summary>
        /// 增加书籍
        /// </summary>
        /// <param name="model">书籍实体类</param>
        /// <returns></returns>
        public bool Add(Model.BookEntity model)
        {
            if (model == null) return false;
            ISession session = SessionFactory.GetSession();
            ITransaction transaction = session.BeginTransaction();
            try
            {
                object obj = session.Save(model);
                transaction.Commit();
                return true;
            }
            catch (Exception)
            {
                transaction.Rollback();
                return false;
            }
        }
      
        public bool Update(Model.BookEntity model)
        {
            if (model == null) return false;
            ISession session = SessionFactory.GetSession();
            ITransaction tansaction = session.BeginTransaction();
            try
            {
                session.Update(model);
                tansaction.Commit();
                return true;
            }
            catch (Exception)
            {
                tansaction.Rollback();
                return false;
            }
        }
        .................................
       .................................
} --------------------编程问答-------------------- 最麻烦的是,每做一个表格、视图、甚至是存储过程,都要建立对应的 *.hbm.xml 和类文件。
我的感觉就是要多做好多麻烦事。都是用LINQ好,方便许多! --------------------编程问答-------------------- 直接代码生成工具都帮你解决 了  你还写什么 --------------------编程问答-------------------- 用工具生成不一定全都对的,好多都还是要改一改的。
我用smart code生成的,除了实体类可以正确生成以外,其他的都要在生成做修改,一样那么麻烦。 --------------------编程问答-------------------- --------------------编程问答-------------------- 这个东西有优点 有缺点,用之前先弄清楚了。 --------------------编程问答-------------------- NHibernate都让我郁闷了很久,要是用Linq to SQL这样子开发倒很容易解决,但是NHibernate要
1、在数据库中创建把.net持久化的对应表
2、创建需要持久化的.net类
3、创建映射文件,告诉NH怎样持久化这些类的属性
4、创建NH的配置文件,以告诉NH怎样连接数据库
5、使用NH提供的API
晕啰,郁闷极了!~~~~~~~ --------------------编程问答-------------------- 先把jsp hibernate框架看回来 这都好弄了 \(^o^)/~
补充:.NET技术 ,  .NET技术前瞻
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,