NHibernate-2.1.2 配置部分以及初始化连接

1.添加引用,添加如下引用,使用的


2.创建数据库对应实体类,格式如下,需要添加virtual 和[serializable]

  [Serializable]    
public class demo
    {
        public virtual string item1 { get; set; }
        public virtual string item2 { get; set; }
    }

3.每个实体类对应其映射文件,其命名为 demo.hbm.xml其中 *.hbm.xml为必需,其中WindowsFormsApplication13为程序集的名称,在连接的时候会用到

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
	<class name="WindowsFormsApplication13.userinfo, WindowsFormsApplication13" table="demo">

		<id name="item1" column="item1" type="string" unsaved-value="0">
			<generator class="native"/>
		</id>
		<property name="item2" type="string" column="item2" />

	</class>
</hibernate-mapping>
4.配置hibernate.cfg.xml文件,名字固定。可以放在根目录或者其他地方,主要是注意它所在程序集的位置,下面默认放在程序根目录进行

    "NHibernate.Demo"是名称可以随便改,其中"Data Source=localhost;Initial Catalog=XINGZHAO;Integrated Security=True;"是连接你数据库的语句,"NHibernate.Dialect.MsSql2008Dialect"可以按照你数据库版本更改,<mapping assembly="WindowsFormsApplication13"/>是你当前程序集的名称

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
    <session-factory name="NHibernate.Demo">
        <property name="connection.driver_class">
            NHibernate.Driver.SqlClientDriver
        </property>
        <property name="connection.connection_string">
			Data Source=localhost;Initial Catalog=XINGZHAO;Integrated Security=True;
		</property>
        <property name="adonet.batch_size">10</property>
        <property name="show_sql">true</property>
        <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
        <property name="use_outer_join">true</property>
        <property name="command_timeout">10</property>
        <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
        <property name='proxyfactory.factory_class'>
            NHibernate.ByteCode.LinFu.ProxyFactoryFactory,NHibernate.ByteCode.LinFu
        </property>
        <mapping assembly="WindowsFormsApplication13"/>
    </session-factory>
</hibernate-configuration>

5.配置好之后可以测试下连接,下面是初始化连接的代码,其中"WindowsFormsApplication13.hibernate.cfg.xml"是你存放上面所说的hibernate.cfg.xml绝对位置

            Configuration config = new Configuration();
            config.Configure(this.GetType().Assembly, "WindowsFormsApplication13.hibernate.cfg.xml");
            ISessionFactory factory = config.BuildSessionFactory();
            ISession session = factory.OpenSession();
            ITransaction trans = session.BeginTransaction();
查询语句

IList<object> list = session.CreateQuery("from userinfo where userid = '1'").List<object>();
如果list有数据,则证明成功。该获取数据方法第一次会有延迟,接下来就比较快了






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值