留言本环境:access2003+ vs2005
整个留言本的类视图:
1.新建一个空的解决方案 NHibernateMessage
依次建立项目Model,DAL,BLL,Command四个项目
2.Model项目处理
删除自动生成的class1.cs文件,新建message.cs文件其代码如下
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
namespace Model
{
public class message
{
public message() { }
字段#region 字段
private string _id= Guid.NewGuid().ToString();
private string _title;
private string _username;
private string _email;
private string _ip;
private string _content;
private DateTime _addtime;
private string _recontent;
private DateTime _retime;
private int _isshow;
#endregion
属性#region 属性
[XmlElement(ElementName = "id")]
public virtual string id
{
get { return this._id; }
set { this._id=value;}
}
[XmlElement(ElementName = "title")]
public virtual string title
{
get { return this._title; }
set { this._title = value; }
}
[XmlElement(ElementName = "username")]
public virtual string username
{
get { return this._username; }
set { this._username = value; }
}
[XmlElement(ElementName = "email")]
public virtual string email
{
get { return this._email; }
set { this._email = value; }
}
[XmlElement(ElementName = "ip")]
public virtual string ip
{
get { return this._ip; }
set { this._ip = value; }
}
[XmlElement(ElementName = "content")]
public virtual string content
{
get { return this._content; }
set { this._content = value; }
}
[XmlElement(ElementName = "addtime")]
public virtual DateTime addtime
{
get { return this._addtime; }
set { this._addtime = value; }
}
[XmlElement(ElementName = "recontent")]
public virtual string recontent
{
get { return this._recontent; }
set { this._recontent = value; }
}
[XmlElement(ElementName = "retime")]
public virtual DateTime retime
{
get { return this._retime; }
set { this._retime = value; }
}
[XmlElement(ElementName = "isshow")]
public virtual int isshow
{
get { return this._isshow; }
set { this._isshow = value; }
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
namespace Model
{
public class message
{
public message() { }
字段#region 字段
private string _id= Guid.NewGuid().ToString();
private string _title;
private string _username;
private string _email;
private string _ip;
private string _content;
private DateTime _addtime;
private string _recontent;
private DateTime _retime;
private int _isshow;
#endregion
属性#region 属性
[XmlElement(ElementName = "id")]
public virtual string id
{
get { return this._id; }
set { this._id=value;}
}
[XmlElement(ElementName = "title")]
public virtual string title
{
get { return this._title; }
set { this._title = value; }
}
[XmlElement(ElementName = "username")]
public virtual string username
{
get { return this._username; }
set { this._username = value; }
}
[XmlElement(ElementName = "email")]
public virtual string email
{
get { return this._email; }
set { this._email = value; }
}
[XmlElement(ElementName = "ip")]
public virtual string ip
{
get { return this._ip; }
set { this._ip = value; }
}
[XmlElement(ElementName = "content")]
public virtual string content
{
get { return this._content; }
set { this._content = value; }
}
[XmlElement(ElementName = "addtime")]
public virtual DateTime addtime
{
get { return this._addtime; }
set { this._addtime = value; }
}
[XmlElement(ElementName = "recontent")]
public virtual string recontent
{
get { return this._recontent; }
set { this._recontent = value; }
}
[XmlElement(ElementName = "retime")]
public virtual DateTime retime
{
get { return this._retime; }
set { this._retime = value; }
}
[XmlElement(ElementName = "isshow")]
public virtual int isshow
{
get { return this._isshow; }
set { this._isshow = value; }
}
#endregion
}
}
然后在与message.cs同目录下我们新建文件message.hbm.xml一便系统能够关联
message.hbm.xml代码如下【注意:关联xml文件的文件名应该为 关联类名.hbm.xml】
Code
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Model.message, Model" table="tb_message">
<id name="id" column="id" type="System.String" >
<generator class="assigned" />
</id>
<property name="title" type="System.String" />
<property name="username" type="System.String" />
<property name="email" type="System.String" />
<property name="ip" type="System.String" />
<property name="content" type="System.String" />
<property name="addtime" type="System.DateTime" />
<property name="recontent" type="System.String" />
<property name="retime" type="System.DateTime" />
<property name="isshow" type="System.Int32"/>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Model.message, Model" table="tb_message">
<id name="id" column="id" type="System.String" >
<generator class="assigned" />
</id>
<property name="title" type="System.String" />
<property name="username" type="System.String" />
<property name="email" type="System.String" />
<property name="ip" type="System.String" />
<property name="content" type="System.String" />
<property name="addtime" type="System.DateTime" />
<property name="recontent" type="System.String" />
<property name="retime" type="System.DateTime" />
<property name="isshow" type="System.Int32"/>
</class>
</hibernate-mapping>
在message.hbm.xml文件配置好后需要注意的是:
message.hbm.xml---右键属性--生成操作--设置为:嵌入的资源
3.web.config配置可以参教demo
demo下载地址:https://files.cnblogs.com/rhythmK/NHibernateMessage.rar