zoukankan      html  css  js  c++  java
  • Nhibernate3循序渐进(一) 第一个NHibernate程序

    1. 建立Domain项目

      Product.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace NHibernate3.Domain
    {
        public class Product
        {
            /// <summary>
            
    /// ID
            
    /// </summary>
            public virtual Guid ID { getset; }

            /// <summary>
            
    /// 编号
            
    /// </summary>
            public virtual string Code { getset; }

            /// <summary>
            
    /// 名称
            
    /// </summary>
            public virtual string Name { getset; }

            /// <summary>
            
    /// 规格
            
    /// </summary>
            public virtual string QuantityPerUnit { getset; }

            /// <summary>
            
    /// 单位
            
    /// </summary>
            public virtual string Unit { getset; }

            /// <summary>
            
    /// 售价
            
    /// </summary>
            public virtual decimal SellPrice { getset; }

            /// <summary>
            
    /// 进价
            
    /// </summary>
            public virtual decimal BuyPrice { getset; }

            /// <summary>
            
    /// 备注
            
    /// </summary>
            public virtual string Remark { getset; }
        }
    }

    编写映射文件Product.hbm.xml

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

    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate3.Domain" namespace="NHibernate3.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="50"/>
        </property>

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

        <property name="Unit" type="string">
          <column name="Unit" length="50"/>
        </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="200"/>
        </property>

      </class>
    </hibernate-mapping>

    然后,将映射文件“Product.hbm.xml”的属性“生成方式”设置为“嵌入的资源

    2. 建立测试项目

      引用程序集“Antlr3.Runtime.dll”,“Iesi.Collections.dll”,“NHibernate.dll”,“Remotion.Data.Linq.dll”,  “nunit.framework.dll”, 添加对Domain项目的引用

      新建Config目录,复制配置文件模板

    hibernate.cfg.xml

    <?xml version="1.0" encoding="utf-8"?>
    <!-- 
    This template was written to work with NHibernate.Test.
    Copy the template to your NHibernate.Test project folder and rename it in hibernate.cfg.xml and change it 
    for your own use before compile tests in VisualStudio.
    -->
    <!-- This is the System.Data.dll provider for SQL Server -->
    <hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
        <session-factory name="NHibernate3.Test">
            <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
            <property name="connection.connection_string">
          server=.;database=NHibernateDemo;integrated security=SSPI;
        </property>
            <property name="adonet.batch_size">10</property>
            <property name="show_sql">true</property>
            <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
            <property name="use_outer_join">true</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>
            <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
        <mapping assembly="NHibernate3.Domain"/>
        </session-factory>
    </hibernate-configuration>

    修改该文件的属性为“始终复制

    复制proxyfactory类的程序集“LinFu.DynamicProxy.dll”和“NHibernate.ByteCode.LinFu.dll”到项目中,并修改生成方式

    创建“NHibernateInit.cs”类文件,用于初始化数据库的表结构

    NHibernateInit.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using NHibernate;
    using NHibernate.Cfg;

    //using NUnit.Framework;

    namespace NHibernate3.Test
    {
        public class NHibernateInit
        {
            public static void InitTest()
            {
                var cfg = new NHibernate.Cfg.Configuration().Configure("Config/hibernate.cfg.xml");
                using (ISessionFactory sessionFactory = cfg.BuildSessionFactory()) { }
            }
        }
    }

    3. 创建数据库结构

      在SQL Server2008中, 创建名为“NHibernateDemo”的数据库

      在控制台的Main方法中, 加入如下代码:

    NHibernateInit.InitTest();
    Console.WriteLine("数据库创建成功!");
     
    Console.Read();

     

    4. 运行程序

      运行后我们发现NHibernate为我们自动创建了T_Product数据表

    技术改变世界
  • 相关阅读:
    34.2 字节流 InputStreamReader OutputStreamWriter
    34.1 字符流-- FileRead FileWrite
    34 io流-- 打印流和对象流
    33.3 删除指定的目录(包含子目录)
    33.2 案例:输出指定目录下的所有java文件名(包含子目录)
    33.1 File 获取目录下的所有文件及子目录
    33 File 文件及目录操作
    32 递归
    31.3 自定义异常类 MyException
    31.2 try finally使用
  • 原文地址:https://www.cnblogs.com/davidgu/p/2544630.html
Copyright © 2011-2022 走看看