。net中使用NHibernate

用java开发项目的时候,一直用着SSH,本人的感觉还是挺不错的。

可是还从来还没有的。NET项目中使用NHibernate,一直听朋友说和Hibernate差不多

。查了相关的资料,其实都离不开四点的中心思想。

1建立实类

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

namespace SqlHelp
{
    public class Users
    {
        private int uid;
        private string name;
        private string password;
       
        public Users()
        {
        }
       
        public int Uid
        {
            get { return uid; }
            set { uid = value; }
        }
       
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
       
        public string Password
        {
            get { return password; }
            set { password = value; }
        }
    }
}

 

 

2.给实体配置一个影射文件例如:Users.hbm.xml

 

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false"  assembly="SqlHelp" namespace="SqlHelp">
  <class name="Users" table="T_users" >
    <id name="Uid" column="I_id" type="Int32">
      <generator class="native" />
    </id>
    <property name="Name" column="S_name " type="String"></property>
    <property name="Password" column="S_password" type="String"></property>
  </class>
</hibernate-mapping>

 

3.web.config配置相关的连接数据信息

 

 <configSections>
    <!--nhibernate的配置-->
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate" />
 </configSections>
 <appSettings/>
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <!--This is the dll provider for Access DB-->
    <session-factory>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider, NHibernate</property>
      <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
      <property name="connection.connection_string">server=localhost;uid=qin;pwd=;database=test</property>
      <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
      <property name="show_sql">true</property>
      <!--<property name="current_session_context_class">thread_static</property>-->
      <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
      <mapping assembly="SqlHelp"/>
    </session-factory>
  </hibernate-configuration>

 

4、加入必要的DLL

    (1)NHibernate.dll

    (2)NHibernate.ByteCode.LinFu.dll

    (3)NHibernate.ByteCode.Castle.dll

 

5、使用

 

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

namespace SqlHelp
{
    public class BaseClass
    {
        public bool SaveUsers(SqlHelp.Users  users)
        {
            bool falg=false;
            try
            {
            NHibernate.Cfg.Configuration configuration = new NHibernate.Cfg.Configuration();
            //加载程序集
            //System.Reflection.Assembly assembly = System.Reflection.Assembly.Load("SqlHelp.Model");
            configuration.AddAssembly("SqlHelp");
            //得到工厂接口
            NHibernate.ISessionFactory sessionFactiory = configuration.BuildSessionFactory();
            //得到会话
            NHibernate.ISession session = sessionFactiory.OpenSession();
            //开始事务
            NHibernate.ITransaction transaction = session.BeginTransaction();
            session.Save(users);
            //提交事务
            transaction.Commit();
                falg=true;
            }
            catch
            {
                falg = false;
            }
            return falg;
        }
    }
}

 

当然这些例子大把了。这里只是重复一下,呵呵。

本人还遇到一个奇怪的问题。no persister for。。类型的错误。

其实是这样的,1要么是全局的配置文件没有配好。2要么是*.hbm.xml没设置成嵌入资源。右击文件属性,生成操作改成嵌入资源就OK了

如果想详细学习,最好去看官方的API

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值