手把手教你写第一个NHibernate小程序

为了工作要研究NHibernate,搜遍网上没找到一个可以直接运行,又能让人看的明白的代码。

自己研究了几天终于搞通了,在这里把所有得步骤描述一遍,免得大家再走弯路。

 1、创建一个数据库,数据库名字SQL

 2新建一个将要持久化.Net对象的表,创建数据表userinfor

     create table userinfor

    (

      userid char(10) primary key,

      username char(10),

      address  char(50),

      tel char(10),

      sex char(2)

   )

3构建一个需要被持久化的.Net

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

namespace example
{
    public class userinfor
    {
        private string _UserId;
        private string _UserName;
        private string _Tel;
        private string _Address;
        private string _Sex;

        public virtual string UserId
        {
            get { return _UserId; }
            set { _UserId = value; }
        }

        public virtual string UserName
        {
            get { return _UserName; }
            set { _UserName = value; }
        }

        public virtual string Tel
        {
            get { return _Tel; }
            set { _Tel = value; }
        }

        public virtual string Address
        {
            get { return _Address; }
            set { _Address = value; }
        }

        public virtual string Sex
        {
            get { return _Sex; }
            set { _Sex = value; }
        }
    }
}

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DomainModel" namespace="DomainModel">
 <class name ="example.userinfor,example" table="userinfor">
  <id name="UserId" column="userid" type="string" length="10" >
   <generator class ="assigned"></generator>
  </id>
 
  <property name="UserName" column="username" type="string" length="10"/>
  <property name="Tel" column="tel" type="string" length="10"/>
  <property name="Address" column="address" type="string" length="50"/>
  <property name="Sex" column="sex" type="string" length="2"/>
 </class>
</hibernate-mapping>

备注:

其中

 <class name ="example.userinfor,example" table="userinfor">name属性为“类名.表名,类名”,table的属性“表名”

 <id name="UserId" column="userid" type="string" length="10" >
   <generator class ="assigned"></generator>
  </id>

<generator class ="assigned">否则查询数据会不成功

5构建一个让NHibernate知道如何连接数据库的配置文件

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

 <configSections>

  <section name="hibernate-configuration"
    type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />

 </configSections>

 <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
   <!-- properties -->
   <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider </property>
   <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver </property>
   <property name="connection.connection_string">Server=localhost;initial catalog=SQL; user=sa;password=sasasa; </property>
   <!-- <property name="connection.connection_string">Data Source=./SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|aspnetdb.mdf </property> -->
   <property name="show_sql">true </property>
   <property name="dialect">NHibernate.Dialect.MsSql2005Dialect </property>
   <property name="use_outer_join">true </property>
   <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N' </property>
   <!-- mapping files -->
   <mapping assembly="Com.Vervidian.TIO.DataAccess"/>

  </session-factory>
 </hibernate-configuration>

</configuration>

备注:

添加引用:NHibernate.dlllog4net.Dll

创建一个按钮

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using NHibernate.Cfg;
using NHibernate;

namespace example
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Configuration cfg = new Configuration();
            cfg.AddAssembly("example");
            ISessionFactory factory = cfg.BuildSessionFactory();
            ISession session = factory.OpenSession();
            ITransaction transaction = session.BeginTransaction();

            userinfor newuser = new userinfor();
            newuser.UserId = textBox1.Text;
            newuser.UserName = textBox2.Text;
            newuser.Tel = textBox3.Text.ToString();
            newuser.Address = textBox4.Text.ToString();
            newuser.Sex = textBox5.Text.ToString();
        
            session.Save(newuser);
            transaction.Commit();
            session.Close();
        }
    }
}

 

该段是2.0配置,比1.0多了前半段。本段代码只需要根据数据库连接修改<property name="connection.connection_string">Server=localhost;initial catalog=SQL; user=sa;password=sasasa; </property>


6使用NHibernateAPI

   即 创建可调用的应用程序

创建一个winForm5lable5textbox,分别用来输入userid,username,address,tel,sex

其中类型必须加virtual,否则会报错提示改信息,与1.0不同
4
构建一个可以让NHibernate知道如何持久化对象属性的映射文件

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值