在Mapping中建立customer.hbm.xml文件
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="SysBase.Entities" assembly="SysBase">
<class name="customer" table="customer">
<id name="Id" column="id">
<generator class="native" />
</id>
<property name="Name">
<column name="name" />
</property>
<property name="Age">
<column name="age" />
</property>
</class>
</hibernate-mapping>
继续建立customer.cs
public class customer
{
private int id;
private string name;
private int age;
public virtual int Id
{
get
{
return this.id;
}
set
{
this.id = value;
}
}
public virtual string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
public virtual int Age
{
get
{
return this.age;
}
set
{
this.age = value;
}
}
}
至此已经可以对数据库表customer进行相应操作,操作示例程序如下:
ISession session;
ISessionFactory sessionFactory;
try
{
var cfg = new NHibernate.Cfg.Configuration().Configure("./cfg/hibernate.cfg.xml");
sessionFactory = cfg.BuildSessionFactory();
session = sessionFactory.OpenSession();
SysBase.Entities.customer cs = new SysBase.Entities.customer();
cs.Id = 4;
cs.Name = "xyj";
cs.Age = 19;
Nhibnet_test.Dao.customer ns = new Nhibnet_test.Dao.customer(session);
SysBase.Entities.customer cs_get = new SysBase.Entities.customer();
IQuery query= session.CreateQuery("from customer");
IList<SysBase.Entities.customer> lcs = new List<SysBase.Entities.customer>();
lcs= ns.GetTcustomerByselfdefin(query, true);
for (int i = 0; i < lcs.Count;i++ )
{
Console.WriteLine("name:{0},age:{1}", lcs[i].Name,lcs[i].Age);
}
Console.WriteLine("finish");
}
catch (Exception ex)
{
Console.WriteLine("ERR:"+ex);
}
Console.ReadKey();
输出:
对于Nhibernate示例程序如上,一些文件路径需要自己布置。
有什么问题可以留言讨论,我也在学习中。