多谢!
NHibernate管理.NET类到数据库表的映射,还提供数据查询和获取数据的方法,可以大幅度减少开发时人工使用SQL和ADO.NET处理数据的时间。
NHibernate的目标主要是用于与数据持久化相关的编程任务
还有spring.net,LINQ
NHibernate
简单的就是实体类+xml+sessionFactory 结合实现对象持久化到应用程序
然后为每个一表格做一个 Entity.cs Entity.hbm.xml的对应实体。那你就可以利用SessionFactory查询实体的数据。例子:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Model.GoodsEntity,Model" table="Goods">//注意实体对表格的对应
<id name="Id" column="Id" type="int">
<generator class="sequence">
<param name="sequence">SEQ_GOODS</param>
</generator>
</id>
<property name="Name" column="Name" type="string" length="200" not-null="false"></property>
<property name="Description" column="Description" type="string" length="1000" not-null="false"></property>
<property name="Brand" column="Brand" type="string" length="500" not-null="false"></property>
<property name="Price" column="Price" type="double"></property>
<property name="BasePrice" column="BasePrice" type="double"></property>
</class>
</hibernate-mapping>
public class GoodsEntity
{
private int _id;
private string _name;
private string _description;
private string _brand;
private double _price;
private double _basePrice;
public virtual int Id
{
get { return _id; }
set { _id = value; }
}
public virtual string Name
{
get { return _name; }
set { _name = value; }
}
public virtual string Description
{
get { return _description; }
set { _description = value; }
}
public virtual string Brand
{
get { return _brand; }
set { _brand = value; }
}
public virtual double Price
{
get { return _price; }
set { _price = value; }
}
public virtual double BasePrice
{
get { return _basePrice; }
set { _basePrice = value; }
}
}
public class BookFactory:IBook
{
#region IBook 成员
/// <summary>
/// 增加书籍
/// </summary>
/// <param name="model">书籍实体类</param>
/// <returns></returns>
public bool Add(Model.BookEntity model)
{
if (model == null) return false;
ISession session = SessionFactory.GetSession();
ITransaction transaction = session.BeginTransaction();
try
{
object obj = session.Save(model);
transaction.Commit();
return true;
}
catch (Exception)
{
transaction.Rollback();
return false;
}
}
public bool Update(Model.BookEntity model)
{
if (model == null) return false;
ISession session = SessionFactory.GetSession();
ITransaction tansaction = session.BeginTransaction();
try
{
session.Update(model);
tansaction.Commit();
return true;
}
catch (Exception)
{
tansaction.Rollback();
return false;
}
}
.................................
.................................
}
我的感觉就是要多做好多麻烦事。都是用LINQ好,方便许多!
我用smart code生成的,除了实体类可以正确生成以外,其他的都要在生成做修改,一样那么麻烦。
1、在数据库中创建把.net持久化的对应表
2、创建需要持久化的.net类
3、创建映射文件,告诉NH怎样持久化这些类的属性
4、创建NH的配置文件,以告诉NH怎样连接数据库
5、使用NH提供的API
晕啰,郁闷极了!~~~~~~~
VC++的编译器能够产生两种目标文件吗?书上是这么写的,怎么回事? 数据库未录入信息时,能否不显示NULL mysql乱码 spring应用开发完全手册 明日科技 李钟尉 冯东庆编著 人民邮电出版社源代码 本人遇到的一个MSSQL问题..请各位大大指教..... 字符串处理问题(统计数组中的相同值) 有谁对离婚官司了解的吗? 环行区域物资运转代价最小问题(实在不知道该用什么标题,请看下面的详细问题吧,先谢了) JSP接收表单提交时,checkform问题 CELL组件单元格类型的问题。 C#获得其他程序中的选项 我在母版页添加的login控件,在下一级目录下引用母版页不显示 得不到想要的结果,行转列的日期问题 struts2中2个iterator叠代的问题 速结,快来 php如何在一个文件中调用另一个类(.php)的方法? 想求教关于micaps第四类数据用grads画图的源程序 winform问题请教~~谢谢 华为 解决方案测试部 Servlet中调用某个类的2个方法出现NullPointerException,急 ASP.net封装类导出数据到EXCEL ++有需要的看看 VS2008项目建立失败 查了半天没查到,后台 怎么控制前台checkbox 选择或不选择 为什么linux中的web浏览器会打不开呢 而且打开却是这种现象 参照图 关于数组赋值给vector的问题 华为 解决方案测试部 多个存储过程,一个调用一个 怎么得知SQL语句搜索到结果~~~~???? 小弟刚学jsp..想问个关于数据库的问题!望高手进来解答下! 如何计算给定字节对齐方式下计算结构体的大小