dapper通用分页方法
/// <summary> /// dapper通用分页方法 /// </summary> /// <typeparam name="T">泛型集合实体类</typeparam> /// <param name="conn">数据库连接池连接对象</param> /// <param name="files">列</param> /// <param name="tableName">表</param> /// <param name="where">条件</param> /// <param name="orderby">排序</param> /// <param name="pageIndex">当前页</param> /// <param name="pageSize">当前页显示条数</param> /// <param name="total">结果集总数</param> /// <returns></returns> public static IEnumerable<T> GetPageList<T>(IDbConnection conn, string files, string tableName, string where, string orderby, int pageIndex, int pageSize, out int total) { int skip = 1; if (pageIndex > 0) { skip = (pageIndex - 1) * pageSize + 1; } StringBuilder sb = new StringBuilder(); sb.AppendFormat("SELECT COUNT(1) FROM {0} where {1};", tableName, where); sb.AppendFormat(@"SELECT {0} FROM(SELECT ROW_NUMBER() OVER(ORDER BY {3}) AS RowNum,{0} FROM {1} WHERE {2} ) AS result WHERE RowNum >= {4} AND RowNum <= {5} ORDER BY {3}", files, tableName, where, orderby, skip, pageIndex * pageSize); using (var reader = conn.QueryMultiple(sb.ToString())) { total = reader.ReadFirst<int>(); return reader.Read<T>(); } }
PS:网上找方法费劲,把找好的记录在自己的博客是个好习惯。
PS:转载请注明来源 https://www.cnblogs.com/sunshine-wy,疑问和勘误可在下方留言。