nibernate基础示例

//

//hibernate.cfg.xml

//

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">
      NHibernate.Connection.DriverConnectionProvider
    </property>
    <property name="connection.driver_class">
      NHibernate.Driver.OscarDriver,OscarNHibernate, Version=1.0.0.0, Culture=neutral, PublicKeyToken=daa534eb8c753bf3
    </property>
    <property name="connection.connection_string">
      server=localhost;port=2003;database=osrdb;username=sysdba;password=szoscar55;
    </property>
    <property name="dialect">
      NHibernate.Dialect.OscarDialect,OscarNHibernate, Version=1.0.0.0, Culture=neutral, PublicKeyToken=daa534eb8c753bf3
    </property>
    <property name="show_sql">true</property>
    <property name="use_outer_join">true</property>
    <!--<mapping assembly="tryone"/>-->
  </session-factory>
</hibernate-configuration>

//

//tone.hbm.xml

//

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="tryone.tone,tryone" table="table1" lazy="false">
    <id name="Id" column="Id" unsaved-value="0">
      <generator class="native" />
    </id>
    <property name="Name" column="Name" type="string" length="64" not-null="true" unique="true"></property>
    <property name="Pwd" column="Pwd" type="string" length="64" not-null="true"></property>
  </class>
</hibernate-mapping>

//

//tone.cs

//


using System;
using System.Collections.Generic;
using System.Text;
namespace tryone
{
    public class tone
    {
        private int _id;
        private string _name;
        private string _pwd;
        /**/
        /// <summary>
        /// 编号
        /// </summary>
        public virtual int Id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }
        /**/
        /// <summary>
        /// 名称
        /// </summary>
        public virtual string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        /**/
        /// <summary>
        /// 密码
        /// </summary>
        public virtual string Pwd
        {
            get
            {
                return _pwd;
            }
            set
            {
                _pwd = value;
            }
        }
    }
}

//

//program.cs

//

using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using System.Collections;


namespace tryone
{
    public class Program
    {
        static void Main(string[] args)
        {
            Configuration cfg = new Configuration().Configure("nhibernate.cfg.xml");
//             cfg.Configure("nhibernate.cfg.xml");
//             cfg.AddXmlFile("tryone.tone.hbm.xml");
//             cfg.AddFile("tryone.tone");
            cfg.AddAssembly("tryone");//就这个可以运行
            ISessionFactory isf = cfg.BuildSessionFactory();
            ISession isession = isf.OpenSession();
            ITransaction it = isession.BeginTransaction();
            //创建表
            SchemaExport se = new SchemaExport(cfg);
            se.Create(false, true);
            //插入数据
            tone newone = new tone();
            newone.Name = "zhanghui";
            newone.Pwd = "123";
            isession.Save(newone);
            it.Commit();
            isession.Close();
            //查询数据

            //普通查询
            isession = isf.OpenSession();
            IQuery iq = isession.CreateQuery("from tone");
            IList ls = iq.List();

            foreach (tone t in ls)
            {
                Console.WriteLine(t.Id+"/n"+t.Name+"/n"+t.Pwd);
            }

//

//             在子查询中使用limit

//
//             DetachedCriteria dc = DetachedCriteria.For(typeof(tone))
//             .SetFirstResult(8)
//             .SetMaxResults(2)
//             .AddOrder(Order.Asc("Id"))
//             .SetProjection(Property.ForName("Id"));
//             IList ls=isession.CreateCriteria(typeof(tone))
//             .Add(Subqueries.PropertyEqAll("Id", dc)).List();

//            foreach (tone t in ls)
//            {
//                Console.WriteLine(t.Id+"/n"+t.Name+"/n"+t.Pwd);
//            }

//

//         带有for update of 和limit的子句

//

//            IQuery iq = isession.CreateQuery("from tone t,tone y");
//            iq.SetFirstResult(8);
//            iq.SetMaxResults(2);
//            iq.SetLockMode("t", LockMode.Upgrade);

//             object[] result = (object[])iq.List()[0];
//             foreach (tone t in result)
//             {
//                 Console.WriteLine(t.Id+"/t/t"+t.Name+"/t/t"+t.Pwd);
//             }
            isession.Close();
            isf.Close();
        }
    }
}

 

需要注意的问题:

xml文件的属性都要设置成嵌入的资源,并选择始终复制。

命名空间的名字和程序集的名字最好一样,这个可以在项目属性里看到。可以不一样我不知道怎么改。

对于在写XML文件的时候需要写nhibernate-configuration-2.2 版本 这里2个XML文件都要写一样且和引用的NHibernate的版本一致。一般现在有2.0和2.2版本。

加载持久化类有好几种方式,可以再nhibernate.cfg.xml里<mapping assembly="tryone">或者在主程序里cfg.AddAssembly("tryone")这里要写整个程序集,它会加载程序集下的所有持久化类。也可单独加载,偶不会。

特别说明:

上述程序里用的是OSCAR数据库,所以在DLL的引用上除了NHibernate以外,还有OSCARNHibernate方言类,其他的数据库方言类在NHibernate里都自带的有。


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值