Castle+IBatisNet分页

最近学习Castle+IBatisNet,分页的问题困扰我很久,在网上也有一些解决IBatisNet分页的思路,但却没有详细的代码,给初学者带来不便。现在把详细代码贴出,跟大家分享!

一.项目框架参考NPetShop,整个解决方案分为:
Domain -- 业务实体层
Persistence -- 持久层,负责数据访问
Service -- 服务层,调用持久层方法并实现业务逻辑
Web -- 最终展现给客户的Web界面,调用Service层方法
Componet -- 主要包括一些通用的方法等

二.Persistence:(BaseSqlMapDao类)

     1.BaseSqlMapDao.cs  
        /// <summary>
        /// 分页方法,得到分页的列表
        /// </summary>
        /// <param name="statementName">操作名称</param>
        /// <param name="parameterObject">参数</param>
        /// <param name="pageSize">每页记录数</param>
        /// <returns></returns>
        protected IPaginatedList ExecuteQueryForPaginatedList(string statementName, object parameterObject, int pageSize)
        {
            try
            {
                return sqlMap.QueryForPaginatedList(statementName, parameterObject, pageSize);
            }
            catch (Exception e)
            {
                throw new DataMapperException("Error executing query '" + statementName + "' for paginated list. Cause: " + e.Message, e);
            }
        }

2.UsersDB.cs
        #region 用于分页
        public IPaginatedList GetUserPagerList(int PageSize)
        {
            return ExecuteQueryForPaginatedList("SelectUsers", null, PageSize);
        }
        #endregion

三.Component(略)

四.Model(略)

五.Service:

1.IUsersService.cs(IUsersService接口)
       region 用于分页
        IPaginatedList GetUserPagerList(int PageSize);
        #endregion

2.UsersService.cs(UsersService类,继承IUsersService接口)
        private UsersDB _users;
        public UsersService(UsersDB users)
        {
            _users = users;
        }
        #region 用于分页
        public IPaginatedList GetUserPagerList(int PageSize)
        {
            return _users.GetUserPagerList(PageSize);
        }
        #endregion

六.Web(UI层)

1.***.aspx:

<asp:GridView ID="GridView1" runat="server" Width="580px"
            AutoGenerateColumns="False" DataKeyNames="users_ID"
            onrowdatabound="GridView1_RowDataBound" onrowdeleting="GridView1_RowDeleting">
            <Columns>
                <asp:BoundField HeaderText="No." />
                <asp:HyperLinkField DataNavigateUrlFields="users_ID"
                    DataNavigateUrlFormatString="UserInfo.aspx?users_ID={0}"
                    DataTextField="users_ID" HeaderText="用户ID" Target="_blank" />
                <asp:BoundField DataField="users_Name" HeaderText="用户名" />
                <asp:TemplateField HeaderText="性别">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("users_Sex") %>'></asp:TextBox>
                    </EditItemTemplate>
                     <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("users_Sex").ToString()=="0"?"女":"男" %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="users_Add" HeaderText="地址" />
                <asp:BoundField DataField="users_Email" HeaderText="Email" />
                <asp:BoundField DataField="users_DateTime" HeaderText="开户时间" />
                <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
            </Columns>
        </asp:GridView>
<table style="width:580px;">
                       <tr>
                           <td align="left">
                               <asp:LinkButton ID="lbFrist" runat="server" CommandName="first"
                                   oncommand="GridViewPaging">|&lt;Frist</asp:LinkButton>
                               <asp:LinkButton ID="LinkbuttonPrev" runat="server" CommandName="prev"
                                   oncommand="GridViewPaging">&lt;&lt;Prev</asp:LinkButton>
                           </td>
                           <td>
                               <asp:Label ID="Label16" runat="server" Text="Page:"></asp:Label>
                               <asp:Label ID="lbCurrentPage" Style="text-align:left;" runat="server"></asp:Label>
                               <asp:Label ID="Label17" runat="server" Text="/"></asp:Label>
                               <asp:Label ID="lbPageCount" Style="text-align:left;" runat="server"></asp:Label>
                               <asp:Label ID="Label10" runat="server" Text="Totle:"></asp:Label>
                               <asp:Label ID="lbRecordCount" Style="text-align:left;" runat="server"></asp:Label>
                           </td>
                           <td align="right">
                               <asp:LinkButton ID="LinkbuttonNext" runat="server" CommandName="next"
                                   oncommand="GridViewPaging">Next&gt;&gt;</asp:LinkButton>
                               <asp:LinkButton ID="lbLast" runat="server" CommandName="last"
                                   oncommand="GridViewPaging">Last&gt;|</asp:LinkButton>
                           </td>
                       </tr>
                   </table>

2.***.aspx.cs:

#region 定义分页变量
        int CurrentPage;//当前页数
        int PageSize=10;   //每页条数
        int PageCount; //总页数
        int RecordCount;//总条数
        #endregion

        private IUsersService _org;
        public IUsersService org
        {
            set { _org = value; }
        }
#region Page_Load事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                #region 分页所用
                CurrentPage = 0;//当前页习惯设为0
                ViewState["PageIndex"] = 0;//页索引也设为0
                RecordCount = _org.GetUserPageCount().PageCount;//总记录数
                if (RecordCount % PageSize == 0)//计算总共有多少页
                {
                    PageCount = RecordCount / PageSize;
                }
                else
                {
                    PageCount = RecordCount / PageSize + 1;
                }
                ViewState["PageCount"] = PageCount;//会话session对整个application有效,而视图状态viewstate相当于某个页面的session
                this.lbRecordCount.Text = RecordCount.ToString();//显示总记录数
                this.lbPageCount.Text = PageCount.ToString();//显示总页数
                #endregion

                GridViewBind();
            }
        }
        #endregion
#region GridView绑定
        private void GridViewBind()
        {
            int StartIndex = CurrentPage * PageSize;//设定导入的起终地址

            IPaginatedList pagerlist = _org.GetUserPagerList(PageSize);
            this.GridView1.DataSource = pagerlist;
            this.GridView1.DataBind();

            this.LinkbuttonPrev.Enabled = true;
            this.LinkbuttonNext.Enabled = true;
           
            if (CurrentPage == (PageCount - 1)) this.LinkbuttonNext.Enabled = false;//当为最后一页时,下一页链接按钮不可用
            if (CurrentPage == 0) this.LinkbuttonPrev.Enabled = false;//当为第一页时,上一页按钮不可用
            this.lbCurrentPage.Text = (CurrentPage + 1).ToString();//当前页数
        }
        #endregion
#region 分页事件
        protected void GridViewPaging(object sender, CommandEventArgs e)
        {
            IPaginatedList pagerlist = _org.GetUserPagerList(PageSize);
            pagerlist.NextPage();

            CurrentPage = (int)ViewState["PageIndex"];//获得当前页索引
            PageCount = (int)ViewState["PageCount"];//获得总页数

            string cmd = e.CommandName;
            //判断cmd,以判定翻页方向
            switch (cmd)
            {
                case "prev"://上一页
                    if (CurrentPage > 0) CurrentPage--;
                    break;

                case "next":
                    if (CurrentPage < (PageCount - 1)) CurrentPage++;//下一页
                    break;

                case "first"://第一页
                    CurrentPage = 0;
                    break;

                case "last"://最后一页
                    CurrentPage = PageCount - 1;
                    break;
            }
            pagerlist.GotoPage(CurrentPage);//页面跳转
            ViewState["PageIndex"] = CurrentPage;//获得当前页
            GridViewBind();
            this.GridView1.DataSource = pagerlist;
            this.GridView1.DataBind();
        }
        #endregion

 

Castle+IBatisNet分页

你可能感兴趣的:(ibatis)