NHibernate学习总结:(一)NHibernate的使用和配置

本文来源:http://www.cnblogs.com/virusswb/archive/2010/01/08/1642402.html

 

在数据库中新建如下图的数据库结构,数据库的名称为NHibernate,包括Customer、Order、Product、OrderProduct四张表,其中的Id为int,不是自增列,其他列是varchar(50),Cost为float具体结构如下图

 

NHibernate1.1

 

1、实体类代码

public  class Customer
    {
        public virtual int Id { get; set; }
        public virtual string Firstname{get;set;}
        public virtual string Version { get; set; }
        public virtual string Lastname { get; set; }
    }

2、对应的映射文件Customer.hbm.xml

 

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateSample.Domain"
                    namespace="NHibernateSample.Domain.Entities">
  <class name="NHibernateSample.Domain.Entities.Customer, NHibernateSample.Domain" table="Customer" lazy="false">
    <id name="Id" column="CustomerId" type="System.Int32">
      <generator class="assigned"></generator>
    </id>
    <property name="Version" column="Version"/>
    <property name="Firstname" column="Firstname"/>
    <property name="Lastname" column="Lastname"/>

  </class>
</hibernate-mapping>

 

 

3、web.config中的配置

 

<?xml version="1.0"?>
<configuration>
    <configSections>
        <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
    </configSections>
    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
        <session-factory name="NHibernate.Test">
            <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
            <property name="connection.connection_string">
        Data Source=SHIWB\SQL2005DEV;Database=NHibernate;User ID=as;Password=123456;
      </property>
            <property name="adonet.batch_size">10</property>
            <property name="show_sql">false</property>
            <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
            <property name="use_outer_join">true</property>
            <property name="command_timeout">60</property>
            <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
            <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
            <mapping assembly="NHibernateSample.Domain"/>
        </session-factory>
    </hibernate-configuration>
</configuration>

 

今天只是一个简单的内容读取。

NHibernate1.2

Technorati 标签: NHibernate

代码可以从http://nhibernatedemo.codeplex.com/下载。

 

posted @ 2010-01-08 22:06  code_flyer  阅读(182)  评论(0编辑  收藏  举报