建立项目

开发工具:VS2013,Nhibernate:v2.2。构建C#控制台程序,项目名称为:NHibernateTest。如下图所示:

../../../../_images/image591.png

新建App.config文件(放置在项目目录中)

修改如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
 <bytecode-provider type="lcg"/>
  <reflection-optimizer use="true"/>
   <session-factory name="Domain">
   <property name="connection.driver_class"> NHibernate.Driver.OscarDriver,OscarNHibernate,Version=1.0.0.0, Culture=neutral, PublicKeyToken=daa534eb8c753bf3</property>
   <property name="connection.connection_string">
     Host=localhost;initial catalog=osrdb;User Id=sysdba;Password=szoscar55
   </property>
   <property name="show_sql">true</property>
   <property name="dialect">NHibernate.Dialect.OscarDialect,OscarNHibernate, Version=1.0.0.0, Culture=neutral, PublicKeyToken=daa534eb8c753bf3</property>
   <property name="use_outer_join">true</property>
   <property name="command_timeout">444</property>
   <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
   <property name="adonet.wrap_result_sets">false</property>
  </session-factory>
 </hibernate-configuration>
</configuration>

添加引用

程序运行需要以下库:

Castle.Core.dll

Castle.DynamicProxy2.dll

Iesi.Collections.dll

log4net.dll

NHibernate.dll

OscarNHibernate.dll

System.Data.OscarClient.dll

Mono.Security.dll

NHibernate.dll是官方nhibernate库,为2.2版本;OscarNHibernate.dll为神通数据库方言驱动;System.Data.OscarClient.dll是神通.net provider库。 因目前神通提供的方言包对nhibernate的版本有要求(2.2),因此只能有我们提供的相关库。其他版本的库使用可能会遇到一定问题。

在项目中只需要引用3个dll库,其他库放在和这3个库同一目录即可,需要引入的3个库:

NHibernate.dll

OscarNHibernate.dll

System.Data.OscarClient.dll

如下图:

../../../../_images/image601.png

添加表对应的类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NHibernateTest.Domain
{
    public class User
    {
        public virtual int Id{ get; set; }
        public virtual string Loginid { get; set; }
        public virtual string Passwd { get; set; }
        public virtual string Name { get; set; }
    }
}

创建DAO类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NHibernate;
using NHibernate.Cfg;
using System.Data.OscarClient;
namespace NHibernateTest.Domain
{
    class NHibernateHelper
    {
        private static ISessionFactory _sessionFactory;
        private static ISessionFactory SessionFactory
        {
            get
            {
                if (_sessionFactory == null)
                {
                    var configuration = new Configuration();
                    configuration.Configure();
                    configuration.AddAssembly(typeof(User).Assembly);
                    _sessionFactory = configuration.BuildSessionFactory();
                }
                return _sessionFactory;
            }
        }

        public static ISession OpenSession()
        {
            return SessionFactory.OpenSession();
        }

    }
}

创建Mapping.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<!--<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateTest.Domain" assembly="NHibernateTest">-->
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="NHibernateTest.Domain.User,NHibernateTest" table="USERS">
    <id name="Id"
  column="ID">
      <generator class="assigned"/>
    </id>
    <property name="Loginid"
    column="LOGINID"
    />
    <property name="Passwd"
    column="PASSWD"
    />
    <property name="Name"
    column="NAME"
    />
  </class>
</hibernate-mapping>

注解

  • 因nhibernate使用的是2.2版本,因此这里必须设置为2.2,如:<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  • class name="NHibernateTest.Domain.User,NHibernateTest”, NHibernateTest.Domain表示namespace;User表示类名。
  • table="USERS",表示制定数据库中的表名。
  • generator class="assigned",assigned代表将id主键列作为唯一键值。

调用代码

在main函数中调用如下代码:

using (ISession session = NHibernateHelper.OpenSession())
       {
           //删除表中所有数据
           ITransaction transaction = session.BeginTransaction();
           session.Delete("from User");
           transaction.Commit();

           ITransaction transaction1 = session.BeginTransaction();

           //新加一条数据
           User user = new User { Id = 1,Name = "xxxx", Passwd = "xxxx", Loginid = "1" };
           session.Save(user);
           User user1 = new User { Id = 2, Name = "222222", Passwd = "22222", Loginid = "2" };
           session.Save(user1);

           //查询
           List<User> users = (List<User>)session.CreateQuery("from User where id = 1").List<User>();
           System.Console.WriteLine(users[0].Name + "," + users[0].Loginid);

           //更新
           user = users[0];
           user.Loginid = "3";
           session.Update(user);

           //删除
           session.Delete(user1);
           transaction1.Commit();
           session.Close();

       }