当前位置: 首页 > 工具软件 > NHibernate > 使用案例 >

NHibernate实例

卜瀚漠
2023-12-01

oracle users表:
CREATE TABLE "JCKFR"."USERS" ("LOGINID" VARCHAR2(20 byte) NOT
    NULL, "USERNAME" VARCHAR2(40 byte) NOT NULL, "PASSWORD"
    VARCHAR2(20 byte) NOT NULL, "EMAILADDRESS" VARCHAR2(40 byte)
    NOT NULL, "LASTLOGIN" DATE NOT NULL)   

app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </configSections>

  <nhibernate>
    <add
     key="hibernate.connection.provider"         
     value="NHibernate.Connection.DriverConnectionProvider"
  />
    <add
     key="hibernate.dialect"                     
     value="NHibernate.Dialect.OracleDialect"
  />
    <add
     key="hibernate.connection.driver_class"         
     value="NHibernate.Driver.OracleClientDriver"
  />
    <add
     key="hibernate.connection.connection_string"
     value="User ID=jckfr;Data Source=frk;Password=jckfr;"
  />
  </nhibernate>
</configuration>

hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.0" >
  <session-factory name="NHibernate.Test">
    <!-- properties -->
    <!--
         <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
         <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
         <property name="connection.connection_string">Server=.;initial catalog=nhibernate;Integrated Security=SSPI</property>
         <property name="show_sql">false/property>æ
         <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</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="NHibernate.Demo" />
  </session-factory>
</hibernate-configuration>

User.cs
using System;
namespace NHibernate.Demo.QuickStart
{
    public class User3
    {
        private string id;
        private string userName;
        private string password;
        private string emailAddress;
        private DateTime lastLogin;


        public User3()
        {
        }

        public string Id
        {
            get { return id; }
            set { id = value; }
        }

        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }

        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        public string EmailAddress
        {
            get { return emailAddress; }
            set { emailAddress = value; }
        }

        public DateTime LastLogin
        {
            get { return lastLogin; }
            set { lastLogin = value; }
        }

    }
}

Users.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <class name="NHibernate.Demo.QuickStart.User3, NHibernate.Demo" table="users">
    <id name="Id" column="LOGINID" type="String" length="20">
      <generator class="assigned" />
    </id>
    <property name="UserName" type="String" length="40" />
    <property name="Password" type="String" length="20" />
    <property name="EmailAddress" type="String" length="40" />
    <property name="LastLogin" type="DateTime" />
  </class>
</hibernate-mapping>

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

using System.Collections;
using NHibernate.Cfg;


namespace NHibernate.Demo.QuickStart
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Configuration cfg = new Configuration();
            cfg.AddAssembly("NHibernate.Demo");
            cfg.AddXmlFile("Users.hbm.xml"); 

            ISessionFactory factory = cfg.BuildSessionFactory();
            ISession session = factory.OpenSession();
            ITransaction transaction = session.BeginTransaction();

            User3 newUser = new User3();
            newUser.Id = "joe_cool3";
            newUser.UserName = "Joseph Cool";
            newUser.Password = "abc123";
            newUser.EmailAddress = "joe@cool.com";
            newUser.LastLogin = 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();
        }
    }
}

 类似资料: