Castle+IBatis.Net分页
<整理:sure_huang 2008-09-23>
最近学习Castle+IBatisNet,分页的问题困扰我很久,在网上也有一些解决IBatisNet分页的思路,但却没有详细的代码,给初学者带来不便。现在把详细代码贴出,跟大家分享!
一.项目框架参考NPetShop,整个解决方案分为:
Domain -- 业务实体层
Persistence -- 持久层,负责数据访问
Service -- 服务层,调用持久层方法并实现业务逻辑
Web -- 最终展现给客户的Web界面,调用Service层方法
Componet -- 主要包括一些通用的方法等
二.Persistence:(BaseSqlMapDao类)
1.BaseSqlMapDao.cs
Code
1/**//// <summary>
2 /// 分页方法,得到分页的列表
3 /// </summary>
4 /// <param name="statementName">操作名称</param>
5 /// <param name="parameterObject">参数</param>
6 /// <param name="pageSize">每页记录数</param>
7 /// <returns></returns>
8 protected IPaginatedList ExecuteQueryForPaginatedList(string statementName, object parameterObject, int pageSize)
9 {
10 try
11 {
12 return sqlMap.QueryForPaginatedList(statementName, parameterObject, pageSize);
13 }
14 catch (Exception e)
15 {
16 throw new DataMapperException("Error executing query '" + statementName + "' for paginated list. Cause: " + e.Message, e);
17 }
18 }
19
1/**//// <summary>
2 /// 分页方法,得到分页的列表
3 /// </summary>
4 /// <param name="statementName">操作名称</param>
5 /// <param name="parameterObject">参数</param>
6 /// <param name="pageSize">每页记录数</param>
7 /// <returns></returns>
8 protected IPaginatedList ExecuteQueryForPaginatedList(string statementName, object parameterObject, int pageSize)
9 {
10 try
11 {
12 return sqlMap.QueryForPaginatedList(statementName, parameterObject, pageSize);
13 }
14 catch (Exception e)
15 {
16 throw new DataMapperException("Error executing query '" + statementName + "' for paginated list. Cause: " + e.Message, e);
17 }
18 }
19
2.UsersDB.cs
Code
1用于分页#region 用于分页
2 public IPaginatedList GetUserPagerList(int PageSize)
3 {
4 return ExecuteQueryForPaginatedList("SelectUsers", null, PageSize);
5 }
6 #endregion
7
1用于分页#region 用于分页
2 public IPaginatedList GetUserPagerList(int PageSize)
3 {
4 return ExecuteQueryForPaginatedList("SelectUsers", null, PageSize);
5 }
6 #endregion
7
三.Component(略)
四.Model(略)
五.Service:
1.IUsersService.cs(IUsersService接口)
Code
1 用于分页#region 用于分页
2 IPaginatedList GetUserPagerList(int PageSize);
3 #endregion
4
1 用于分页#region 用于分页
2 IPaginatedList GetUserPagerList(int PageSize);
3 #endregion
4
2.UsersService.cs(UsersService类,继承IUsersService接口)
Code
1 private UsersDB _users;
2 public UsersService(UsersDB users)
3 {
4 _users = users;
5 }
6 用于分页#region 用于分页
7 public IPaginatedList GetUserPagerList(int PageSize)
8 {
9 return _users.GetUserPagerList(PageSize);
10 }
11 #endregion
1 private UsersDB _users;
2 public UsersService(UsersDB users)
3 {
4 _users = users;
5 }
6 用于分页#region 用于分页
7 public IPaginatedList GetUserPagerList(int PageSize)
8 {
9 return _users.GetUserPagerList(PageSize);
10 }
11 #endregion
六.Web(UI层)
1.***.aspx:
Code
1<asp:GridView ID="GridView1" runat="server" Width="580px"
2 AutoGenerateColumns="False" DataKeyNames="users_ID"
3 onrowdatabound="GridView1_RowDataBound" onrowdeleting="GridView1_RowDeleting">
4 <Columns>
5 <asp:BoundField HeaderText="No." />
6 <asp:HyperLinkField DataNavigateUrlFields="users_ID"
7 DataNavigateUrlFormatString="UserInfo.aspx?users_ID={0}"
8 DataTextField="users_ID" HeaderText="用户ID" Target="_blank" />
9 <asp:BoundField DataField="users_Name" HeaderText="用户名" />
10 <asp:TemplateField HeaderText="性别">
11 <EditItemTemplate>
12 <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("users_Sex") %>'></asp:TextBox>
13 </EditItemTemplate>
14 <ItemTemplate>
15 <asp:Label ID="Label1" runat="server" Text='<%# Eval("users_Sex").ToString()=="0"?"女":"男" %>'></asp:Label>
16 </ItemTemplate>
17 </asp:TemplateField>
18 <asp:BoundField DataField="users_Add" HeaderText="地址" />
19 <asp:BoundField DataField="users_Email" HeaderText="Email" />
20 <asp:BoundField DataField="users_DateTime" HeaderText="开户时间" />
21 <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
22 </Columns>
23 </asp:GridView>
24<table style="width:580px;">
25 <tr>
26 <td align="left">
27 <asp:LinkButton ID="lbFrist" runat="server" CommandName="first"
28 oncommand="GridViewPaging">|<Frist</asp:LinkButton>
29 <asp:LinkButton ID="LinkbuttonPrev" runat="server" CommandName="prev"
30 oncommand="GridViewPaging"><<Prev</asp:LinkButton>
31 </td>
32 <td>
33 <asp:Label ID="Label16" runat="server" Text="Page:"></asp:Label>
34 <asp:Label ID="lbCurrentPage" Style="text-align:left;" runat="server"></asp:Label>
35 <asp:Label ID="Label17" runat="server" Text="/"></asp:Label>
36 <asp:Label ID="lbPageCount" Style="text-align:left;" runat="server"></asp:Label>
37 <asp:Label ID="Label10" runat="server" Text="Totle:"></asp:Label>
38 <asp:Label ID="lbRecordCount" Style="text-align:left;" runat="server"></asp:Label>
39 </td>
40 <td align="right">
41 <asp:LinkButton ID="LinkbuttonNext" runat="server" CommandName="next"
42 oncommand="GridViewPaging">Next>></asp:LinkButton>
43 <asp:LinkButton ID="lbLast" runat="server" CommandName="last"
44 oncommand="GridViewPaging">Last>|</asp:LinkButton>
45 </td>
46 </tr>
47 </table>
48
49
1<asp:GridView ID="GridView1" runat="server" Width="580px"
2 AutoGenerateColumns="False" DataKeyNames="users_ID"
3 onrowdatabound="GridView1_RowDataBound" onrowdeleting="GridView1_RowDeleting">
4 <Columns>
5 <asp:BoundField HeaderText="No." />
6 <asp:HyperLinkField DataNavigateUrlFields="users_ID"
7 DataNavigateUrlFormatString="UserInfo.aspx?users_ID={0}"
8 DataTextField="users_ID" HeaderText="用户ID" Target="_blank" />
9 <asp:BoundField DataField="users_Name" HeaderText="用户名" />
10 <asp:TemplateField HeaderText="性别">
11 <EditItemTemplate>
12 <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("users_Sex") %>'></asp:TextBox>
13 </EditItemTemplate>
14 <ItemTemplate>
15 <asp:Label ID="Label1" runat="server" Text='<%# Eval("users_Sex").ToString()=="0"?"女":"男" %>'></asp:Label>
16 </ItemTemplate>
17 </asp:TemplateField>
18 <asp:BoundField DataField="users_Add" HeaderText="地址" />
19 <asp:BoundField DataField="users_Email" HeaderText="Email" />
20 <asp:BoundField DataField="users_DateTime" HeaderText="开户时间" />
21 <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
22 </Columns>
23 </asp:GridView>
24<table style="width:580px;">
25 <tr>
26 <td align="left">
27 <asp:LinkButton ID="lbFrist" runat="server" CommandName="first"
28 oncommand="GridViewPaging">|<Frist</asp:LinkButton>
29 <asp:LinkButton ID="LinkbuttonPrev" runat="server" CommandName="prev"
30 oncommand="GridViewPaging"><<Prev</asp:LinkButton>
31 </td>
32 <td>
33 <asp:Label ID="Label16" runat="server" Text="Page:"></asp:Label>
34 <asp:Label ID="lbCurrentPage" Style="text-align:left;" runat="server"></asp:Label>
35 <asp:Label ID="Label17" runat="server" Text="/"></asp:Label>
36 <asp:Label ID="lbPageCount" Style="text-align:left;" runat="server"></asp:Label>
37 <asp:Label ID="Label10" runat="server" Text="Totle:"></asp:Label>
38 <asp:Label ID="lbRecordCount" Style="text-align:left;" runat="server"></asp:Label>
39 </td>
40 <td align="right">
41 <asp:LinkButton ID="LinkbuttonNext" runat="server" CommandName="next"
42 oncommand="GridViewPaging">Next>></asp:LinkButton>
43 <asp:LinkButton ID="lbLast" runat="server" CommandName="last"
44 oncommand="GridViewPaging">Last>|</asp:LinkButton>
45 </td>
46 </tr>
47 </table>
48
49
2.***.aspx.cs:
Code
1定义分页变量#region 定义分页变量
2 int CurrentPage;//当前页数
3 int PageSize=10; //每页条数
4 int PageCount; //总页数
5 int RecordCount;//总条数
6 #endregion
7
8 private IUsersService _org;
9 public IUsersService org
10 {
11 set { _org = value; }
12 }
13Page_Load事件#region Page_Load事件
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 if (!Page.IsPostBack)
17 {
18 分页所用#region 分页所用
19 CurrentPage = 0;//当前页习惯设为0
20 ViewState["PageIndex"] = 0;//页索引也设为0
21 RecordCount = _org.GetUserPageCount().PageCount;//总记录数
22 if (RecordCount % PageSize == 0)//计算总共有多少页
23 {
24 PageCount = RecordCount / PageSize;
25 }
26 else
27 {
28 PageCount = RecordCount / PageSize + 1;
29 }
30 ViewState["PageCount"] = PageCount;//会话session对整个application有效,而视图状态viewstate相当于某个页面的session
31 this.lbRecordCount.Text = RecordCount.ToString();//显示总记录数
32 this.lbPageCount.Text = PageCount.ToString();//显示总页数
33 #endregion
34
35 GridViewBind();
36 }
37 }
38 #endregion
39GridView绑定#region GridView绑定
40 private void GridViewBind()
41 {
42 int StartIndex = CurrentPage * PageSize;//设定导入的起终地址
43
44 IPaginatedList pagerlist = _org.GetUserPagerList(PageSize);
45 this.GridView1.DataSource = pagerlist;
46 this.GridView1.DataBind();
47
48 this.LinkbuttonPrev.Enabled = true;
49 this.LinkbuttonNext.Enabled = true;
50
51 if (CurrentPage == (PageCount - 1)) this.LinkbuttonNext.Enabled = false;//当为最后一页时,下一页链接按钮不可用
52 if (CurrentPage == 0) this.LinkbuttonPrev.Enabled = false;//当为第一页时,上一页按钮不可用
53 this.lbCurrentPage.Text = (CurrentPage + 1).ToString();//当前页数
54 }
55 #endregion
56分页事件#region 分页事件
57 protected void GridViewPaging(object sender, CommandEventArgs e)
58 {
59 IPaginatedList pagerlist = _org.GetUserPagerList(PageSize);
60 pagerlist.NextPage();
61
62 CurrentPage = (int)ViewState["PageIndex"];//获得当前页索引
63 PageCount = (int)ViewState["PageCount"];//获得总页数
64
65 string cmd = e.CommandName;
66 //判断cmd,以判定翻页方向
67 switch (cmd)
68 {
69 case "prev"://上一页
70 if (CurrentPage > 0) CurrentPage--;
71 break;
72
73 case "next":
74 if (CurrentPage < (PageCount - 1)) CurrentPage++;//下一页
75 break;
76
77 case "first"://第一页
78 CurrentPage = 0;
79 break;
80
81 case "last"://最后一页
82 CurrentPage = PageCount - 1;
83 break;
84 }
85 pagerlist.GotoPage(CurrentPage);//页面跳转
86 ViewState["PageIndex"] = CurrentPage;//获得当前页
87 GridViewBind();
88 this.GridView1.DataSource = pagerlist;
89 this.GridView1.DataBind();
90 }
91 #endregion
92
1定义分页变量#region 定义分页变量
2 int CurrentPage;//当前页数
3 int PageSize=10; //每页条数
4 int PageCount; //总页数
5 int RecordCount;//总条数
6 #endregion
7
8 private IUsersService _org;
9 public IUsersService org
10 {
11 set { _org = value; }
12 }
13Page_Load事件#region Page_Load事件
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 if (!Page.IsPostBack)
17 {
18 分页所用#region 分页所用
19 CurrentPage = 0;//当前页习惯设为0
20 ViewState["PageIndex"] = 0;//页索引也设为0
21 RecordCount = _org.GetUserPageCount().PageCount;//总记录数
22 if (RecordCount % PageSize == 0)//计算总共有多少页
23 {
24 PageCount = RecordCount / PageSize;
25 }
26 else
27 {
28 PageCount = RecordCount / PageSize + 1;
29 }
30 ViewState["PageCount"] = PageCount;//会话session对整个application有效,而视图状态viewstate相当于某个页面的session
31 this.lbRecordCount.Text = RecordCount.ToString();//显示总记录数
32 this.lbPageCount.Text = PageCount.ToString();//显示总页数
33 #endregion
34
35 GridViewBind();
36 }
37 }
38 #endregion
39GridView绑定#region GridView绑定
40 private void GridViewBind()
41 {
42 int StartIndex = CurrentPage * PageSize;//设定导入的起终地址
43
44 IPaginatedList pagerlist = _org.GetUserPagerList(PageSize);
45 this.GridView1.DataSource = pagerlist;
46 this.GridView1.DataBind();
47
48 this.LinkbuttonPrev.Enabled = true;
49 this.LinkbuttonNext.Enabled = true;
50
51 if (CurrentPage == (PageCount - 1)) this.LinkbuttonNext.Enabled = false;//当为最后一页时,下一页链接按钮不可用
52 if (CurrentPage == 0) this.LinkbuttonPrev.Enabled = false;//当为第一页时,上一页按钮不可用
53 this.lbCurrentPage.Text = (CurrentPage + 1).ToString();//当前页数
54 }
55 #endregion
56分页事件#region 分页事件
57 protected void GridViewPaging(object sender, CommandEventArgs e)
58 {
59 IPaginatedList pagerlist = _org.GetUserPagerList(PageSize);
60 pagerlist.NextPage();
61
62 CurrentPage = (int)ViewState["PageIndex"];//获得当前页索引
63 PageCount = (int)ViewState["PageCount"];//获得总页数
64
65 string cmd = e.CommandName;
66 //判断cmd,以判定翻页方向
67 switch (cmd)
68 {
69 case "prev"://上一页
70 if (CurrentPage > 0) CurrentPage--;
71 break;
72
73 case "next":
74 if (CurrentPage < (PageCount - 1)) CurrentPage++;//下一页
75 break;
76
77 case "first"://第一页
78 CurrentPage = 0;
79 break;
80
81 case "last"://最后一页
82 CurrentPage = PageCount - 1;
83 break;
84 }
85 pagerlist.GotoPage(CurrentPage);//页面跳转
86 ViewState["PageIndex"] = CurrentPage;//获得当前页
87 GridViewBind();
88 this.GridView1.DataSource = pagerlist;
89 this.GridView1.DataBind();
90 }
91 #endregion
92
七.效果图: