用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