oracle分页存储过程

oracle分页存储过程: 
CREATE OR REPLACE PROCEDURE FGK.prc_page
       (p_tableName        in  varchar2,   --表名
        p_strWhere         in  varchar2,   --查询条件
        p_orderColumn      in  varchar2,   --排序的列
        p_orderStyle       in  varchar2,   --排序方式
        p_curPage          in out Number,  --当前页
        p_pageSize         in out Number,  --每页显示记录条数
        p_totalRecords     out Number,     --总记录数
        p_totalPages       out Number,     --总页数
        v_cur              out pkg_page.cur_page)   --返回的结果集
IS
   v_sql VARCHAR2(3000) := '';      --sql语句
   v_startRecord Number(4);         --开始显示的记录条数
   v_endRecord Number(4);           --结束显示的记录条数
BEGIN
   --记录中总记录条数
   v_sql := 'SELECT TO_NUMBER(COUNT(*)) FROM ' || p_tableName || ' WHERE 1=1';
   IF p_strWhere IS NOT NULL or p_strWhere <> '' THEN
       v_sql := v_sql || p_strWhere;
   END IF;
   EXECUTE IMMEDIATE v_sql INTO p_totalRecords;
   
   --验证页面记录大小
   IF p_pageSize < 0 THEN
       p_pageSize := 0;
   END IF;
   
   --根据页大小计算总页数
   IF MOD(p_totalRecords,p_pageSize) = 0 THEN
       p_totalPages := p_totalRecords / p_pageSize;
   ELSE
       p_totalPages := p_totalRecords / p_pageSize + 1;
   END IF;
   
   --验证页号
   IF p_curPage < 1 THEN
       p_curPage := 1;
   END IF;
   IF p_curPage > p_totalPages THEN
       p_curPage := p_totalPages;
   END IF;
   
   --实现分页查询
   v_startRecord := (p_curPage - 1) * p_pageSize + 1;
   v_endRecord := p_curPage * p_pageSize;
   v_sql := 'SELECT * FROM (SELECT A.*, rownum r FROM ' ||
            '(SELECT * FROM ' || p_tableName;
   IF p_strWhere IS NOT NULL or p_strWhere <> '' THEN
       v_sql := v_sql || ' WHERE 1=1' || p_strWhere;
   END IF;
   IF p_orderColumn IS NOT NULL or p_orderColumn <> '' THEN
       v_sql := v_sql || ' ORDER BY ' || p_orderColumn || ' ' || p_orderStyle;
   END IF;
   v_sql := v_sql || ') A WHERE rownum <= ' || v_endRecord || ') B WHERE r >= '
            || v_startRecord;
   DBMS_OUTPUT.put_line(v_sql);
   OPEN v_cur FOR v_sql;
END prc_page;
/
Model层代码:

 public class PageInfo
    {

        // 定义
        private string p_tableName;  //-表名
        private string p_strWhere;    //  --查询条件
        private string p_orderColumn; //  --排序的列
        private string p_orderStyle;  // --排序方式
        private int p_curPage;       //--当前页
        private int p_pageSize;      // --每页显示记录条数
        private int p_totalRecords;   // --总记录数
        private int p_totalPages;     // --总页数

        /// <summary>
        /// 定义函数
        /// </summary>
        public PageInfo() { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fgmasterid"></param>
        /// <param name="fgmastertile"></param>
        public PageInfo(string p_tableName, string p_strWhere, string p_orderColumn, string p_orderStyle, int p_curPage, int p_pageSize, int p_totalRecords, int p_totalPages)
        {
            this.p_tableName = p_tableName;
            this.p_strWhere = p_strWhere;
            this.p_orderColumn = p_orderColumn;
            this.p_orderStyle = p_orderStyle;
            this.p_curPage = p_curPage;
            this.p_pageSize = p_pageSize;
            this.p_totalRecords = p_totalRecords;
            this.p_totalPages = p_totalPages;

        }

        /// <summary>
        /// 查询的表名
        /// </summary>
        public string P_tableName
        {
            get { return p_tableName; }
            set { p_tableName = value; }
        }
        /// <summary>
        /// 查询条件
        /// </summary>
        public string P_strWhere
        {
            get { return p_strWhere; }
            set { p_strWhere = value; }
        }
        /// <summary>
        /// 排序的列
        /// </summary>
        public string P_orderColumn
        {
            get { return p_orderColumn; }
            set { p_orderColumn = value; }
        }
        /// <summary>
        /// 排序方式
        /// </summary>
        public string P_orderStyle
        {
            get { return p_orderStyle; }
            set { p_orderStyle = value; }
        }
        /// <summary>
        /// 当前页
        /// </summary>
        public int P_curPage
        {
            get { return p_curPage; }
            set { p_curPage = value; }
        }
        /// <summary>
        /// 每页显示记录条数
        /// </summary>
        public int P_pageSize
        {
            get { return p_pageSize; }
            set { p_pageSize = value; }
        }
        /// <summary>
        /// 总记录数
        /// </summary>
        public int P_totalRecords
        {
            get { return p_totalRecords; }
            set { p_totalRecords = value; }
        }
        /// <summary>
        /// 总页数
        /// </summary>
        public int P_totalPages
        {
            get { return p_totalPages; }
            set { p_totalPages = value; }
        }
    }

 

posted @ 2015-01-21 21:53  coolsundy  阅读(186)  评论(0编辑  收藏  举报