NHibernate

1、首先最基本的dll

    NHibernate.dll

    NHibernate.dll

    Iesi.Collections.dll

注意:这只是针对这个小程序的基本dll


2、实体类

[Serializable]

    public class User

    {

        public virtual int userId{get;set;}

        public virtual string userName { get; set; }

        public virtual string password { get; set; }

        public virtual string nickName { get; set; }

        public virtual string imageName { get; set; }

        public virtual string mood { get; set; }

        

    }

注意:1、实体的属性与数据表里的字段是对应的

      2、必须要是virtual 

      3、必须要有get/set方法


3、实体的映射文件

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

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">

  <class name="命名空间.实体类, 命名空间" table="表名">

    <id name="userId" type="int" unsaved-value="null">

      <column name="id" length="4" sql-type="int" not-null="true" unique="true" />

      <generator class="native" />

    </id>

    <property name="userName" column= "UserName" type="String" length="50"/>

    <property name="password" column= "Password" type="String" length="50"/>

    <property name="nickName" column= "NickName" type="String" length="50"/>

    <property name="imageName" type="String" length="50"/>

    <property name="mood" type="String" length="50"/>

  </class>

</hibernate-mapping>

注意:1、映射文件名必须是是实体名.hbm.xml

      2、映射文件必须是嵌入的资源

      错误信息:如果没有设置为嵌入的资源,那么就会报错No persister for:***


4、在web.config中配置hibernate.cfg,xml

<configSections>

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

  </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="default_schema">dbo</property>

      <property name="connection.connection_string">连接数据库字符串(Data Source=.;Initial Catalog=db_diary;Integrated Security=True)</property>

      <property name="show_sql">false</property>

      <property name="dialect">数据库方言(NHibernate.Dialect.MsSql2005Dialect)</property>

      <property name="use_outer_join">true</property>

      <property name="proxyfactory.factory_class"> NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle</property>

      <!-- mapping files -->

      <mapping assembly="命名空间(ClassLibraryNhibernate)" />

    </session-factory>

  </hibernate-configuration>

注意:1、 <mapping assembly="命名空间(ClassLibraryNhibernate)" />

      错误信息:如果没有设置这个配置节点,那么就会报错No persister for:***


5、Nhibernate的Helper

public  class NhibernateHelper

    {

        private ISessionFactory _sessionFactory;

        public NhibernateHelper()

        {

            _sessionFactory = GetSessionFactory();

        }

        private ISessionFactory GetSessionFactory()

        {

            //获取配置文件

            return (new Configuration()).Configure().BuildSessionFactory();

        }

        public ISession GetSession()

        {

            //开启Session

            return _sessionFactory.OpenSession();

        }

}

6、操作

NhibernateHelper hrl = new NhibernateHelper();

            //下一步,创建一个 Session 对象 

            ISession session = hrl.GetSession();

            ITransaction transaction = session.BeginTransaction();


            User newUser = new User();

            newUser.userName = "Joseph Cool";

            newUser.password = "abc123";

            newUser.nickName = "[email protected]";

            newUser.imageName = "abcs123";

            newUser.mood = "jool.com";

            session.Save(newUser);

            transaction.Commit();

            session.Close();    


本文出自 “与耐力赛跑” 博客,请务必保留此出处http://webxi199321.blog.51cto.com/9128235/1577438

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。