欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

NHibernate

程序员文章站 2022-06-18 09:53:12
create table t_user(id int primary key auto_increment, name char(20), pwd char(20)); <...

create table t_user(id int primary key auto_increment, name char(20), pwd char(20));

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <!-- isessionfactory实例 -->
  <session-factory>
    <property name="hbm2ddl.keywords">none</property>
    <!-- 属性 -->
    <!-- 设置connection provider,nhibernate将用之连接 -->
    <property name="connection.provider">nhibernate.connection.driverconnectionprovider</property>
    <!-- 设置所用driver,在此,我们为mysql数据库选择mysqldatadriver -->
    <property name="connection.driver_class">nhibernate.driver.mysqldatadriver</property>
    <!-- 设置连接到数据库的connection string -->
    <property name="connection.connection_string">server=127.0.0.1;database=test;uid=root;pwd=root;</property>


    <!-- 为特定数据库选择dialect,这里选择使用mysql5dialect -->
    <property name="dialect">nhibernate.dialect.mysql5dialect</property>
    <!-- 映射文件 -->
    <mapping assembly="test"></mapping>
  </session-factory>


</hibernate-configuration>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="mynamespace.data.tuser, test" table="t_user" lazy="true">
    <id name="id"  type="int" >
      <column name="id"></column>
      <generator class="native" />
    </id>
    <property name="name" type="string">
      <column name="name" length="20" sql-type="char" not-null="false"/>
    </property>
    <property name="pwd" type="string">
      <column name="pwd" length="20" sql-type="char" not-null="false"/>
    </property>
  </class>
</hibernate-mapping>

 

using system;
using system.collections;


namespace mynamespace.data
{
    #region tuser


    /// <summary>
    /// tuser object for nhibernate mapped table 't_user'.
    /// </summary>
    public class tuser
    {
        #region member variables


        protected int _id;
        protected string _name;
        protected string _pwd;


        public tuser() { }


        public virtual int id
        {
            get { return _id; }
            set { _id = value; }
        }


        public virtual string name
        {
            get { return _name; }
            set
            {
                if (value != null && value.length > 10)
                    throw new argumentoutofrangeexception("invalid value for name", value, value.tostring());
                _name = value;
            }
        }


        public virtual string pwd
        {
            get { return _pwd; }
            set
            {
                if (value != null && value.length > 10)
                    throw new argumentoutofrangeexception("invalid value for pwd", value, value.tostring());
                _pwd = value;
            }
        }


        #endregion


    }


    #endregion
}

using system;
using system.collections.generic;
using system.linq;
using system.text;
using nhibernate.cfg;
using mynamespace.data;

 


namespace nhibernate
{
    class program
    {
        static void main(string[] args)
        {
            tuser user = new tuser();
            user.name = "111";www.2cto.com
            user.pwd = "222";
            configuration cfg = new configuration();
            isessionfactory factory = cfg.configure().buildsessionfactory();
            isession session = factory.opensession();
           console.writeline("success" + session.gettype());
           session.saveorupdate(user);
           iquery q = session.createquery("from tuser");
           console.writeline(q.list().count);
           console.readkey();
        }
    }
}
作者:gjl5787