一、我们用之前的例子作为基础,现在做一个符合下面条件的查询并显示出来:1、IsActive=1(激活状态)。2、ClassID=参数。3、按CreatedDate和Priority进行降序排列。因此我们在Announcement.hbm.xml中加入我们的搜索信息(为何是加在这里?因为它是使用NHibernate来进行查询的),下面是Announcement.hbm.xml的完成代码:
<?
xml version="1.0" encoding="utf-8"
?>
<
hibernate-mapping
xmlns
="urn:nhibernate-mapping-2.2"
>
<
class
name
="DZ_Portal.App.Model.Announcement,DZ_Portal.App"
table
="PT_Announcement"
lazy
="false"
>
<
id
name
="ItemID"
column
="ItemID"
type
="Int32"
unsaved-value
="0"
>
<
generator
class
="native"
/>
</
id
>
<
property
column
="ModuleID"
type
="Int32"
name
="ModuleID"
/>
<
property
column
="CreateByUser"
type
="String"
name
="CreateByUser"
not-null
="true"
length
="50"
/>
<
property
column
="CreatedDate"
type
="DateTime"
name
="CreatedDate"
not-null
="true"
/>
<
property
column
="Title"
type
="String"
name
="Title"
not-null
="true"
length
="100"
/>
<
property
column
="MoreLink"
type
="String"
name
="MoreLink"
not-null
="true"
length
="150"
/>
<
property
column
="ExpireDate"
type
="DateTime"
name
="ExpireDate"
not-null
="true"
/>
<
property
column
="Description"
type
="String"
name
="Description"
not-null
="true"
length
="2147483647"
/>
<
property
column
="Frequency"
type
="Int32"
name
="Frequency"
not-null
="true"
/>
<
property
column
="IsActive"
type
="Int32"
name
="IsActive"
not-null
="true"
/>
<
property
column
="Priority"
type
="Int32"
name
="Priority"
not-null
="true"
/>
<
property
column
="ClassID"
type
="Int32"
name
="ClassID"
not-null
="true"
/>
</
class
>
<
query
name
="GetAnnouncementsOfClassID"
>
<![CDATA[
from Announcement announcement WHERE announcement.IsActive=1 AND announcement.ClassID=:param1 ORDER BY announcement.CreatedDate DESC,announcement.Priority DESC
]]>
</
query
>
</
hibernate-mapping
>
<!--
1。HBM映射配置文件中“urn:nhibernate-mapping-2.0 ”需改为“urn:nhibernate-mapping-2.2”
2。 <class name="类名,程序集名" table="Archives" >需改为<class name="类名,程序集名
" table="Archives" lazy="false">,需要添加“lazy="false"”,不然会出现错误:
he following types may not be used as proxies: xxxxx: method get_CreateDate should be virtual
3。Spring.Data.NHibernate程序集变更为:Spring.Data.NHibernate12 (注意:Spring.Data.NHibernate
程序集仍然存在,是对NHibernate1.0X的支持,Spring.Data.NHibernate12 为新增的支持NHibernate1.2.0GA的程序集)
-->
二、查询工作属于业务逻辑层,因为你的业务就是负责显示最新有用的新闻消息,所以你必须要有一个Announcement的逻辑层对象:AnnouncementManager。因此,我们要在Index页面中使用该对象,我们就需要在Index页面创建时创建该对象,因此我们对Index的Spring的配置需要修改,我们打开spring.net_bean_Index.xml看一下,下面是它的所有代码:
<?
xml version="1.0" encoding="utf-8"
?>
<
objects
xmlns
='http://www.springframework.net'>
<!-- DAO定义--
>
<
object
id
="AnnouncementDAO"
type
="DZ_Portal.App.DAL.AnnouncementDAO,DZ_Portal.App"
parent
="baseDao"
>
</
object
>
<!--
Manager定义
-->
<
object
id
="AnnouncementManager"
type
="DZ_Portal.App.BLL.AnnouncementManager,DZ_Portal.App"
>
<
property
name
="AnnouncementDAO"
ref
="AnnouncementDAO"
/>
</
object
>
<!--
事务
<object id="AnnouncementManagerTrans" parent="BaseTransactionManager">
<property name="Target" ref="AnnouncementManager"/>
<property name="ProxyInterfaces" value="DZ_Portal.App.BLL.IAnnouncementManager"/>
<property name="TransactionAttributes">
<name-values>
<add key="TestTransAction*" value="PROPAGATION_REQUIRED,-TransActionException"/>
</name-values>
</property>
</object>
-->
<!--
页面定义
-->
<
object
type
="~/Index.aspx"
parent
="adminPageM"
>
<
property
name
="AnnouncementManager"
ref
="AnnouncementManager"
/>
<!--
<property name="AnnouncementManager" ref="AnnouncementManagerTrans"/>
-->
</
object
>
</
objects
>
解说:我们采用的是属性注入的方式来实现Index页面创建时创建AnnouncementManager,而AnnouncementManager创建的时候又以属性注入的方式来创建AnnouncementDAO。(一个完整的对象是有3层的:分别是:显示层、业务逻辑层、业务实体,现在我们的显示层为Index页面,逻辑层就是AnnouncementManager,我们不能没有业务实体层,因此,创建逻辑层时必须创建业务实体层,而创建业务实体的,正好是这个AnnouncementDAO,它采用工厂模式来生产我们需要的业务实体),这就完整地解释了这个页面的相关配置。(我有个想法,就是,这些不同层的对象基本上都是一起的,如果其它页面需要实现他们时,同样又要定义一次?或者不定义--可以不定义,那样,以后维护的时候会很乱的,我有个想法是把他们放到另一个单元叫:Spring.Net_bean_Announcement,意思是说:Announcement对象的Spring配置,这样就必须在web.config中加入该spring的配置,现在我们暂时不这样做了,就让它这样吧)。
三、查询配置好、查询执行对象已在spring中配置好、现在就要在显示层中使用这些已做好的东西了,我把所有代码列出来,在后面会解说的:
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
FastSpring.Web.Base;
using
DZ_Portal.App.BLL;
using
DZ_Portal.App.Model;
public
partial
class
Index : BasePageSession
...
{
protected void Page_PreInit(Object sender, EventArgs e)
...{
if (!IsPostBack)
DoShowAnnouncements();
}
protected void Page_Load(object sender, EventArgs e)
...{
this.Response.Redirect("~/SystemAdmin/fsLogin.aspx", false);
//IList Announcements = m_AnnouncementManager.FindEntityListByNamedQueryOfRecord("GetAnnouncementsOfClassID", "101", 0, 10);
}
AnnouncementManager#region AnnouncementManager
private IAnnouncementManager m_AnnouncementManager;
public IAnnouncementManager AnnouncementManager
...{
set
...{
m_AnnouncementManager = value;
}
}
#endregion
protected void DoShowAnnouncements()
...{
IList Announcements = m_AnnouncementManager.FindEntityListByNamedQueryOfRecord("GetAnnouncementsOfClassID", "101", 0, 10);
WebControlDataBind(this.FSFindControl("FSPagerList"), Announcements);
int i = Announcements.Count;
}
VL_DataBind#region VL_DataBind
public virtual void WebControlDataBind(System.Web.UI.Control WebControl, IList DataSource)
...{
try
...{
if ((WebControl == null) | (DataSource == null))
...{
//没有找到列表显示控件
PrintLog("WebControl is null or DataSource is null");
return;
}
//根据不同的列表显示控件,进行数据绑下
//DataGrid
if (WebControl is System.Web.UI.WebControls.DataGrid)
...{
System.Web.UI.WebControls.DataGrid _control = WebControl as System.Web.UI.WebControls.DataGrid;
_control.DataSource = DataSource;
_control.DataBind();
}
//Repeater
else if (WebControl is System.Web.UI.WebControls.Repeater)
...{
System.Web.UI.WebControls.Repeater _control = WebControl as System.Web.UI.WebControls.Repeater;
_control.DataSource = DataSource;
_control.DataBind();
}
//DataList
else if (WebControl is System.Web.UI.WebControls.DataList)
...{
System.Web.UI.WebControls.DataList _control = WebControl as System.Web.UI.WebControls.DataList;
_control.DataSource = DataSource;
_control.DataBind();
}
//GridView
else if (WebControl is System.Web.UI.WebControls.GridView)
...{
System.Web.UI.WebControls.GridView _control = WebControl as System.Web.UI.WebControls.GridView;
_control.DataSource = DataSource;
_control.DataBind();
}
}
catch ...{ }
}
#endregion
}
解说:spring属性注入,因此该类中必须有一个相关的对象,我们使用接口的编程模式,定义了这个IAnnouncementManager m_AnnouncementManager接口,并使用该接口对象。好了,我们要在页面初始化的时候显示我们的符合条件的搜索结果,因此我做了个过程:DoShowAnnouncements。DoShowAnnouncements过程分2步完成:1、搜索出数据记录:IList Announcements = m_AnnouncementManager.FindEntityListByNamedQueryOfRecord("GetAnnouncementsOfClassID", "101", 0, 10);2、数据绑定到页面控件:WebControlDataBind(this.FSFindControl("FSPagerList"), Announcements);(该方法我是参照FastSpring底层的数据绑定方式进行改写的,该方法比较通用,以后会考虑重载或者直接加入底层基类,以方便以后使用)。
四、aspx页面文件的修改,下面是所有代码:
<%
...
@ Page Language="C#" MasterPageFile="~/FSMasterPage.master" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" Title="Untitled Page"
%>
<
asp:Content
ID
="Content1"
ContentPlaceHolderID
="body"
Runat
="Server"
>
<
form
id
="form1"
runat
="server"
target
="_top"
method
="post"
>
<
div
id
="Div_Body"
style
="overflow-x: auto; width: 776; height: 386px"
align
="left"
>
<
asp:Repeater
ID
="FSPagerList"
runat
="server"
EnableViewState
="true"
>
<
ItemTemplate
>
<
ul
>
<
li
><
a
class
="0"
href
="http://www.163.com"
target
="_blank"
>
<%
...
#DataBinder.Eval(Container.DataItem, "ClassID")
%>
</
a
>
<
a
class
="0"
href
="http://www.163.com"
target
="_blank"
>
<%
...
#DataBinder.Eval(Container.DataItem, "Title")
%>
</
a
>
</
li
>
</
ul
>
</
ItemTemplate
>
</
asp:Repeater
>
</
div
>
</
form
>
</
asp:Content
>
解说:我从www.pconline.com.cn中的源码看到,他们是使用div和table相结合的方式来显示,我这里就采用了div来进行显示,有关div和table的页面布局,可以自己去搜索相关的文章(粗略说一下:table是表格,用于显示数据用,div才叫布局,如果整个页面用table布局,那么,它在table结束完它才会显示页面,用户会觉得网站很慢,影响用户的兴致,而采用div布局,它处理数据是从顶部到底部一个个显示出来的,可以在网速慢的情况看一下www.pconline.com.cn的网站就知道原因了,但是sina网站使用table布局,那有它们的原因的,我就不说了,自己可以去搜索相关的资料吧,可以搜索:“div table 区别”)。WebControlDataBind函数有2个参数,一个是需要绑定的页面控件,一个是数据源,所以,这个aspx页面中有一个Repeater对象的定义:<asp:Repeater ID="FSPagerList" runat="server" EnableViewState="true">,至于这个页面的代码解说,我就不说了,我对html代码也不熟悉,我只是抄过来的哦。大家如果是一步步跟我学的,不知道能否正常运行,我怕有哪些地方遗留忘记说了,因为我也是新手,我还不能很熟悉整个流程,有缺少的地方,不能运行可以告诉我,我再改正一下。
原创作品出自努力偷懒,转载请说明文章出处:http://blog.csdn.net/kfarvid或 http://www.cnblogs.com/kfarvid/