C# 中使用NHibernate的经验
第一步:创建User实体类
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace NHibernateTest
6{
7 [Serializable]
8 public class User
9 {
10 private int _id;
11
12 public int Id
13 {
14 get { return _id; }
15 set { _id = value; }
16 }
17 private string _name;
18
19 public string Name
20 {
21 get { return _name; }
22 set { _name = value; }
23 }
24 private string _sex;
25
26 public string Sex
27 {
28 get { return _sex; }
29 set { _sex = value; }
30 }
31 }
32}
33
第二步:创建该实体类的数据映射User.hbm.xml文件(xml文件)
1<?xml version="1.0" encoding="utf-8" ?> 2<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false"> 3 <class name="NHibernateTest.User,NHibernateTest" table="Users"> 4 <id name="Id" type="Int32"> 5 <generator class="identity"/> 6 </id> 7 <property name="Name" type="String"/> 8 <property name="Sex" type="String"/> 9 </class> 10</hibernate-mapping>
第三步:创建hibernate.cfg.xml配置文件(xml文件)
1<?xml version="1.0" encoding="utf-8"?> 2<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" > 3 <session-factory name="NHibernate.Test"> 4 <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> 5 <property name="connection.connection_string"> 6 Data Source=IMMENSITY\SQLEXPRESS;Initial Catalog=NHibernateTest;uid=sa;pwd=welcome;Pooling=False 7 </property> 8 <property name="adonet.batch_size">10</property> 9 <property name="show_sql">false</property> 10 <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> 11 <property name="use_outer_join">true</property> 12 <property name="command_timeout">10</property> 13 <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property> 14 <mapping assembly="NHibernateTest"/> 15 </session-factory> 16</hibernate-configuration> 17 18
更改属性(同上):
赋值到输出目录选择 始终复制
生成操作改成 嵌入的资源
相关属性解释如下:
hibernate-configuration: NHibernate的版本信息
session-factory: 对应的类名称
connection.provider: 连接提供器的名称
connection.driver_class: 连接驱动类的名称
connection.connection_string: 数据库连接字符串
show_sql: 是否显示SQL语句
Dialect: NHibernate方言(Dialect)的类名
use_outer_join: 是否允许使用外连接
query.substitutions: 把NHibernate查询中的一些短语替换为SQL短语
assembly: 程序集名称
最后在button的onClick事件中加入如下代码:
需导入以下两个命名空间
using NHibernate.Cfg;
using NHibernate;
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8using NHibernate.Cfg;
9using NHibernate;
10
11namespace NHibernateTest
12{
13 public partial class Form1 : Form
14 {
15 public Form1()
16 {
17 InitializeComponent();
18 }
19
20 private void button1_Click(object sender, EventArgs e)
21 {
22 //会话工厂
23 ISessionFactory factory = null;
24 Configuration cfg = new Configuration().Configure();
25 //构建配置文件的对象
26 factory = cfg.BuildSessionFactory();
27 //实例化会话工厂
28 ISession session=factory.OpenSession();
29 //设定HQL查询语句,HQL语句只能用于查询
30 //注意,这里的表名不是数据库的表名,是你的实体类的名称(User)
31 IQuery query = session.CreateQuery("from User");
32 //得到结果集
33 IList<User> list = query.List<User>();
34 //关闭会话对象
35 session.Close();
36 this.dataGridView1.DataSource = list;
37
38 }
39 }
40}
其中第一步和第二步,文件的生成 推荐使用mygeneration小工具来生成,这样省事,简单很多。如果有需要,我在以后可以加入这个工具的使用

浙公网安备 33010602011771号