存储过程实现真分页效果

Posted 贾梦阳1024

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了存储过程实现真分页效果相关的知识,希望对你有一定的参考价值。

  最近, 有几个接口是列表页面, 以前的后台做的是假分页, 就是数据库的数据都返回了, 然后让前端和移动端自己做假分页, 出于对数据库优化的考虑, 决定使用存储过程实现.项目使用.net mvc webapi

  model代码

 #region 教学活动观摩列表
    public class EduObAndVideoListPara
    {
        public string UserCode { set; get; }
        public string KindCode { set; get; }
        public PagingInfo PageInfo { set; get; }
        public EduObAndVideoListPara()
        {
            PageInfo = new PagingInfo();
        }
    }
    public class EduObAndVideoListRet
    {
        public string Status { set; get; }
        public string Msg { set; get; }
        public List<EduObAndVideoLMain> EduObAndVideoLMain;
        public PagingInfo PageInfo { set; get; } 
        public EduObAndVideoListRet()
        {
            EduObAndVideoLMain = new List<EduObAndVideoLMain>();
            PageInfo = new PagingInfo();
        }

    }
    public class EduObAndVideoLMain
    {
        public string EduObAndVideoID { set; get; }
        public string Title { set; get; }
        public string CreatePerson { set; get; }
        public string CreateTime { set; get; }
        public string ClassName { set; get; }
        public string ActivityTypeName { set; get; }
    }
    #endregion

模型仓库代码

 #region 教学活动观摩列表
        public EduObAndVideoListRet EduObAndVideoObList(EduObAndVideoListPara para)
        {
            EduObAndVideoListRet ret = new EduObAndVideoListRet();
            ret.PageInfo.CurrentPage = para.PageInfo.CurrentPage;
            try
            {
                if (string.IsNullOrEmpty(para.UserCode))
                    throw new Exception("UserCode不能为空");
                if (string.IsNullOrEmpty(para.KindCode))
                    throw new Exception("KindCode不能为空");

                string sqlStr = string.Format("exec dbo.EduAndObVideoListMain {0}, {1}, {2}", para.UserCode,
                                para.KindCode, para.PageInfo.CurrentPage);
                DbCommand cmd = db.GetSqlStringCommond(sqlStr);
                DataSet ds = db.ExecuteDataSet(cmd);
                if (ds.Tables.Count > 0)
                {
                    DataTable dt1 = ds.Tables[0];
                    if (dt1.Rows.Count > 0)
                    {
                        EduObAndVideoLMain main;
                        foreach (DataRow dr1 in ds.Tables[0].Rows)
                        {
                            main = new EduObAndVideoLMain();
                            main.EduObAndVideoID = dr1["EduObAndVideoID"].ToString();
                            main.Title = dr1["Title"].ToString();
                            main.CreatePerson = dr1["CreatePerson"].ToString();
                            main.CreateTime = dr1["CreateTime"].ToString();
                            main.ClassName = dr1["ClassName"].ToString();
                            main.ActivityTypeName = dr1["ActivityTypeName"].ToString();
                            ret.EduObAndVideoLMain.Add(main);
                        }
                    }
                    DataTable dt2 = ds.Tables[1];
                    if (dt2.Rows.Count > 0)
                    {
                        ret.PageInfo.TotalItems = int.Parse(dt2.Rows[0]["TotalItems"].ToString());
                    }
                    ret.Status = "ok";
                    ret.Msg = "请求成功";

                }
                else
                {
                    ret.Status = "ok";
                    ret.Msg = "暂无数据";
                }


            }
            catch (Exception e)
            {
                ret.Status = "error";
                ret.Msg = e.Message;
            }
            return ret;
        }
        #endregion 

控制器代码

 #region 教学活动观摩列表
        [HttpPost]
        public IHttpActionResult EduObAndVideoList([FromBody] EduObAndVideoListPara para)
        {
            return Ok(repo.EduObAndVideoObList(para));
        }
        #endregion

存储过程代码

USE [Preschool_ABC]
GO
/****** Object:  StoredProcedure [dbo].[EduAndObVideoListMain]    Script Date: 2017/9/21 10:30:27 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[EduAndObVideoListMain]
(
	@UserCode INT,
	@KindCode INT,
	@CurrentPage INT
)
AS
	SET XACT_ABORT ON
	SET NOCOUNT ON 	
	SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
	
	DECLARE @intTranCount INT 
	SET @[email protected]@TranCount
	IF @intTranCount<> 0  SAVE TRANSACTION Savepoint ELSE BEGIN TRANSACTION 

	DECLARE @IsUser INT
	DECLARE @ActivityName NVARCHAR(10)
	DECLARE @PageSize INT
	DECLARE @BeginPos INT
	DECLARE @EndPos INT
	DECLARE @TotalItems INT

	BEGIN
		SET @IsUser = 0
		SET @ActivityName = ‘‘
		SET @PageSize = 10
		SET @BeginPos = (@CurrentPage - 1) * @PageSize + 1
		SET @EndPos = @CurrentPage * @PageSize
		SET @TotalItems = 0
			
		IF NOT EXISTS(SELECT 1 FROM dbo.Com_User WHERE UserCode = @UserCode)
		BEGIN
			RAISERROR(‘UserCode错误‘, 16, 1)
			GOTO ErrHandle
		END
		IF NOT EXISTS(SELECT 1 FROM dbo.EduObAndVideo WHERE   KindCode = @KindCode)
		BEGIN
			RAISERROR(‘暂无数据‘, 16, 1)
			GOTO ErrHandle
		END


		BEGIN
			SELECT @TotalItems = COUNT(*) FROM dbo.EduObAndVideo WHERE KindCode = @KindCode;
			WITH temptb AS (
			SELECT ROW_NUMBER() OVER(ORDER BY CreateTime DESC ) AS rowNumber, EduObAndVideoID, Title, CreatePerson, CreateTime, ClassName, ActivityTypeName, KindCode FROM EduObAndVideo
			)
			SELECT EduObAndVideoID, Title, CreatePerson, CreateTime, ClassName, ActivityTypeName FROM temptb WHERE temptb.rowNumber BETWEEN @BeginPos AND @EndPos AND temptb.KindCode = @KindCode
			SELECT @TotalItems AS TotalItems
		END
		
	
	END

	
	--完成--
	IF @intTranCount<>@@TranCount COMMIT TRANSACTION		 
	RETURN
	
ErrHandle:
	 IF @[email protected]@TranCount 
	BEGIN
		ROLLBACK TRANSACTION Savepoint 
		RETURN
	END
	ELSE
	BEGIN
		ROLLBACK TRANSACTION 
		RETURN
	END

  一个完成的列表分页就完成了, 前端可以轻松完成分页效果, 而移动端可以做下拉刷新,下拉加载更多等

以上是关于存储过程实现真分页效果的主要内容,如果未能解决你的问题,请参考以下文章

ngTbale真分页实现排序搜索等功能

Asp.Net真分页技术

EF性能优化

千万数量级分页存储过程(效果演示)

JavaWeb分页的实现

JAVA-分页查询