初学搭建NHibernate
来源:互联网 发布:西科软件实习骗局 编辑:程序博客网 时间:2024/04/24 12:13
NHibernate是一个面向.NET环境的对象/关系数据库映射工具。 之前面试的时候有了解到,现在有空了,学着搭建了一下。一下是搭建步骤:
1、首先下载NHibernate,地址:https://sourceforge.net/projects/nhibernate/ 。
2、编写hibernate.cfg.xml,可以根据下载的文件下的Configuration_Templates,找到你所使用的数据库进行更改。
具体如下:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="NHibernateTest">
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">
server=.;database=NHibernateDemo;User ID=sa;Password=123456;
</property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="command_timeout">60</property>
<property name="hbm2ddl.auto">update</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<mapping assembly="Dom"/>
</session-factory>
</hibernate-configuration>
3.编写类和类的cfg.xml
代码如下
{
private ISessionFactory sessionFactory;
public ProductDao()
{
var cfg = new NHibernate.Cfg.Configuration().Configure("Config/hibernate.cfg.xml");
sessionFactory = cfg.BuildSessionFactory();
}
public object Save(Product entity)
{
using (ISession session = sessionFactory.OpenSession())
{
var id = session.Save(entity);
session.Flush();
return id;
}
}
public void Update(Product entity)
{
using (ISession session = sessionFactory.OpenSession())
{
session.Update(entity);
session.Flush();
}
}
public void Delete(Product entity)
{
using (ISession session = sessionFactory.OpenSession())
{
session.Delete(entity);
session.Flush();
}
}
public Product Get(object id)
{
using (ISession session = sessionFactory.OpenSession())
{
return session.Get<Product>(id);
}
}
public Product Load(object id)
{
using (ISession session = sessionFactory.OpenSession())
{
return session.Load<Product>(id);
}
}
public IList<Product> LoadAll()
{
using (ISession session = sessionFactory.OpenSession())
{
return session.Query<Product>().ToList();
}
}
}
- 初学搭建NHibernate
- NHibernate初学
- NHibernate搭建教程
- NHibernate的初学几点错误记录
- NHibernate初学 之 增删改查
- 初学搭建SSH框架
- APUE初学 环境搭建
- 初学Java 环境搭建
- 初学Android_0_Android环境搭建
- appium初学环境搭建
- kafka 初学搭建
- 初学搭建SSM框架
- NHibernate初学遇到的问题及解决方案之一
- NHibernate
- nhibernate
- NHibernate
- NHibernate
- NHibernate
- 1
- 逻辑回归函数Softmax()
- 三位数乘以一位数
- 排序算法用链表汇总
- Android中传递对象的三种方法
- 初学搭建NHibernate
- 关于OpenCV的那些事——相机标定
- 实验名称:在VB中对于随机数ab的比较大小
- 01-复杂度2 Maximum Subsequence Sum
- Android SDK更新问题
- JVM的内存区域划分
- android应用程序如何调用支付宝接口
- 浅谈GCD
- 关于performSelector调用和直接调用区别