Nhibernate开发专题博客

Nhibernate开发专题博客,为您精选Nhibernate开发教程,助您开发愉快!

公告信息
欢迎光临Nhibernate开发专题博客,祝您开发愉快!
文章档案

基础增删改查-NHibernate入门到精通系列3

从今天开始,我们将进入NHibernate开发学习中,首先来一些NHibernate的增删改查操作,把这几个学会了,基础就打好了。
有时间也可以学学国产的CYQ.Data 数据框架,也是相当优秀的一款框架!

下面我们按以下步骤进行操作:

1.实体类与Xml映射

2.NHibernate工具生成对应的表结构

3.编写数据库访问对象DAO

4.单元测试一下增、删、该、查方法


一:新建项目,如下图:

  


 二:编写实体类

    public class Product
    {
        public virtual Guid ID { get; set; }
        public virtual string Code { get; set; }
        public virtual string Name { get; set; }
        public virtual string QuantityPerUnit { get; set; }
        public virtual string Unit { get; set; }
        public virtual decimal SellPrice { get; set; }
        public virtual decimal BuyPrice { get; set; }
        public virtual string Remark { get; set; }
    }
三:映射到Xml中:

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Domain" namespace="Domain">
  <class name="Product" table="T_Product" lazy="true" >
    <id name="ID" column="ID" type="Guid" >
      <generator class="assigned" />
    </id>

    <property name="Code" type="string">
      <column name="Code" length="51"/>
    </property>

    <property name="Name" type="string">
      <column name="Name" length="52"/>
    </property>
   
    <property name="QuantityPerUnit" type="string">
      <column name="QuantityPerUnit" length="53"/>
    </property>

    <property name="Unit" type="string">
      <column name="Unit" length="54"/>
    </property>


    <property name="SellPrice" type="decimal">
      <column name="SellPrice" precision="14" scale="2"/>
    </property>

    <property name="BuyPrice" type="decimal">
      <column name="BuyPrice" precision="14" scale="2"/>
    </property>

    <property name="Remark" type="string">
      <column name="Remark" length="191"/>
    </property>

  </class>
</hibernate-mapping>

四:将xml当成资源嵌入,如下图:


 五:建立Web测试项目,如下图:


六:引用相关的程序集,如下图:

 

 

七:将项目复制粘贴NHibernate配置模板到项目中并修改该文件的属性为“始终复制”,如下图:




八:准备初始化数据库表结构[NHibernateInit.cs]

[TestFixture]
    public class NHibernateInit
    {
        [Test]
        public void InitT()
        {
            var cfg = new NHibernate.Cfg.Configuration().Configure("Config/hibernate.cfg.xml");
            using (ISessionFactory sessionFactory = cfg.BuildSessionFactory()) { }
        }
    }

九:LinFu.DynamicProxy.dll”和“NHibernate.ByteCode.LinFu.dll”拷贝到项目中并修改生成方式,如下图:

 

    

十:调试并启动外部程序,如下图:


十一:新建数据库,如下图:

 

 

十一:定位到“NHibernateTest.dll”的程序集,启动单元测试,如下图:


十二:数据库表默认被创建好了!

 

 
十二:新建数据库访问对象项目,如下图:


 

十三:引用项目dll,实现IProductDao接口和 ProductDao类

  
ProductDao
 public interface IProductDao
    {
        object Save(Product entity);

        void Update(Product entity);

        void Delete(Product entity);

        Product Get(object id);

        Product Load(object id);

        IList<Product> LoadAll();
    }


 public class ProductDao : IProductDao
    {
        private ISessionFactory sessionFactory;

        public ProductDao()
        {
            var cfg = new NHibernate.Cfg.Configuration().Configure("Config/hibernate.cfg.xml");
            sessionFactory = cfg.BuildSessionFactory();
        }

        public object Save(Domain.Product entity)
        {
            using (ISession session = sessionFactory.OpenSession())
            {
                var id = session.Save(entity);
                session.Flush();
                return id;
            }
        }

        public void Update(Domain.Product entity)
        {
            using (ISession session = sessionFactory.OpenSession())
            {
                session.Update(entity);
                session.Flush();
            }
        }

        public void Delete(Domain.Product entity)
        {
            using (ISession session = sessionFactory.OpenSession())
            {
                session.Delete(entity);
                session.Flush();
            }
        }

        public Domain.Product Get(object id)
        {
            using (ISession session = sessionFactory.OpenSession())
            {
                return session.Get<Domain.Product>(id);
            }
        }

        public Domain.Product Load(object id)
        {
            using (ISession session = sessionFactory.OpenSession())
            {
                return session.Load<Domain.Product>(id);
            }
        }

        public IList<Domain.Product> LoadAll()
        {
            using (ISession session = sessionFactory.OpenSession())
            {
                return session.Query<Domain.Product>().ToList();
            }
        }
}

十四:编写测试类[ProductDaoTest]:

[TestFixture]
    public class ProductDaoTest
    {
        private IProductDao productDao;

        [SetUp]
        public void Init()
        {
            productDao = new ProductDao();
        }

        [Test]
        public void SaveTest()
        {
            var product = new Domain.Product
            {
                ID = Guid.NewGuid(),
                BuyPrice = 10M,
                Code = "ABC123",
                Name = "电脑",
                QuantityPerUnit = "20x1",
                SellPrice = 11M,
                Unit = "台"
            };

            var obj = this.productDao.Save(product);

            Assert.NotNull(obj);
        }

        [Test]
        public void UpdateTest()
        {
            var product = this.productDao.LoadAll().FirstOrDefault();
            Assert.NotNull(product);

            product.SellPrice = 12M;

            Assert.AreEqual(12M, product.SellPrice);
        }

        [Test]
        public void DeleteTest()
        {
            var product = this.productDao.LoadAll().FirstOrDefault();
            Assert.NotNull(product);

            var id = product.ID;
            this.productDao.Delete(product);
            Assert.Null(this.productDao.Get(id));
        }

        [Test]
        public void GetTest()
        {
            var product = this.productDao.LoadAll().FirstOrDefault();
            Assert.NotNull(product);

            var id = product.ID;
            Assert.NotNull(this.productDao.Get(id));
        }

        [Test]
        public void LoadTest()
        {
            var product = this.productDao.LoadAll().FirstOrDefault();
            Assert.NotNull(product);

            var id = product.ID;
            Assert.NotNull(this.productDao.Get(id));
        }

        [Test]
        public void LoadAllTest()
        {
            var count = this.productDao.LoadAll().Count;
            Assert.True(count > 0);
        }
}
十五,运行单元测试,大功告成,如下图:


经过以上十五步之后,终于把这基本的操作给折腾好了,反正NHibernate就是这么复杂又强大的,喜欢折腾的赶紧试试吧!
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"

2011/9/7 1:47:20 | Nhibernate从入门到精通系列 | |

  • 发表评论