NHibernate 学习1 11月20日
总算成功了。。。基本入门了。。能用session.save() 了~~~
中间碰到了无数问题。。。自己总结一下。。。。。
主要是 Could not compile the mapping document 这个问题困扰我很久。。原来是根据NHibernnate的版本不同 设置的App.config就不同.
我用的是最新版本 NHibernate 2.1.1
下面的代码是标准的 ~~ 经过测试绝对不会错了!!
首先创建一个类库 ClassLibrary这是我取的名字
1.创建个App.config 如果是web.config的话也差不多的 把下面的复制过去就可以了
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate"/>
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</property>
<property name="dialect">
NHibernate.Dialect.MsSql2005Dialect
</property>
<property name="connection.driver_class">
NHibernate.Driver.SqlClientDriver
</property>
<property name="connection.connection_string">
Data Source=.;Integrated Security=True; initial catalog=test ;User ID=sa;Password=123456
</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
用上面这行代码的话 要引用一些DLL文件LinFu 还有个castle 不写这个属性会报一个错误
</session-factory>
</hibernate-configuration>
</configuration>
2 。配置文件好了。那然后是 映射文件 (红字是注释)
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="ClassLibrary.User, ClassLibrary" table="users" lazy="false">
name属性里面 "命名空间.类,命名空间" table是数据库表
<id name="Id" column="LogonId" type="int" length="50">
<generator class="identity" />
class属性里面设置成identity 表示自动增长, (不是SQL的话用其他的) 这东西害我弄了很久很久。。设置错了会在事务提交的时候报错
</id>
<property name="UserName" column="Name" type="String" length="50"/>
里面name的值 是User类里面那些 字段的属性名 不是字段名哦~~~,这问题困扰了大概1小时多~~~ length要设置正确
column是数据库表的字段 不要写错了。。~~~ 只有一个name属性 代表 数据库表和类的字段名称是一样的
<property name="Password" type="String" length="50"/>
<property name="EmailAddress" type="String" length="50"/>
<property name="LastLogon" type="DateTime"/>
</class>
</hibernate-mapping>
重要的一点就是要把这个映射文件设置成 嵌入的资源
3.类和数据库我就不写了,对应这个映射文件就可以了
4.编写一个Form1.cs
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using NHibernate;
using NHibernate.Cfg;
using ClassLibrary;
using System;
namespace Nhibernate1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Configuration cfg = new Configuration().AddAssembly("ClassLibrary");
//ISessionFactory factory = cfg.BuildSessionFactory();
//ISession session = factory.OpenSession();
ISession session = new Configuration().AddAssembly("ClassLibrary").BuildSessionFactory().OpenSession();
//我这里直接获得session对象了,上面那3个//是步骤
ITransaction transaction = session.BeginTransaction();
User newUser = new User();
newUser.UserName = "Joseph Cool";
newUser.Password = "abc123";
newUser.EmailAddress = "joe@cool.com";
newUser.LastLogon = DateTime.Now;
// Tell NHibernate that this object should be saved
session.Save(newUser);
// commit all of the changes to the DB and close the ISession
transaction.Commit();
session.Close();
MessageBox.Show("OK!");
}
}
}
感觉代码跟jave 里是一样的 但配置还真的没java里舒服
7个步骤
1. 创建一个Configuration
2. 设置它的映射文件路径 cfg.AddAssembly("命名空间");
3. 获得一个ISessionFactory
4. 获得一个session
5. 创建一个事务ITransaction
6 . 然后就是你要做的事
7 . 提交事物和关闭Session对象

浙公网安备 33010602011771号