ORM中SqlUtility增加将数据表转成实体集合的多线程版本

public sealed class SqlUtility
{
        /// <summary>
        /// 多线程
        /// 获取SQL执行结果<br/>
        /// 用属性的Set方法赋值
        /// </summary>
        /// <typeparam name="TModel">返回数据类型</typeparam>
        /// <param name="dbName">数据库名</param>
        /// <param name="sql">SQL</param>
        /// <param name="sqlParams">SQL参数w集合</param>
        /// <param name="propertys">返回实体的属性集合</param>
        /// <param name="rowCount">输出总行数</param>
        /// <returns>查询结果集合</returns>
        public static List<TModel> GetExeResultToListTask<TModel, SqlParameter, DataReader>(IDbHelper<SqlParameter, DataReader> dbHelper, string dbName, string sql, List<SqlParameter> sqlParams, Dictionary<string, PropertyInfo> propertys, out int rowCount)
            where TModel : IModel, new()
            where SqlParameter : DbParameter
            where DataReader : DbDataReader
        {
            var dbModelList = new List<TModel>();
            rowCount = 0;
            //mySql查询
            var ds = dbHelper.QueryRead<DataSet>(dbName, sql, sqlParams.ToArray());
            TModel newT = default(TModel);
            if (ds != null && ds.Tables.Count == 2)
            {
                using (ds)
                {
                    //获取总行数
                    int.TryParse(ds.Tables[1].Rows[0][0].ToString(), out rowCount);
                    if (ds.Tables[0].Rows.Count >= 100)
                    {
                        //多线程
                        //10个线程
                        int taskRowCount = ds.Tables[0].Rows.Count / 10;
                        if (ds.Tables[0].Rows.Count % 10 != 0)
                        {
                            taskRowCount++;
                        }
                        var list = new List<List<DataRow>>(10);                        
                        //1.先分任务
                        List<DataRow> rowList = null;
                        var taskList = new List<Task<List<TModel>>>(10);
                        var i = 1;
                        foreach (DataRow row in ds.Tables[0].Rows)
                        {
                            if (i == 1 || i > taskRowCount)
                            {
                                rowList = new List<DataRow>();
                                list.Add(rowList);
                                rowList.Add(row);
                                i = 2;
                            }
                            else
                            {
                                rowList.Add(row);
                                i++;
                            }
                        }
                        //开10个线程处理
                        for (int t = 0; t < list.Count; t++)
                        {
                            List<DataRow> taskRowList = list[t];
                            var task = Task.Run(() => tableToModelList<TModel>(taskRowList, propertys));
                            taskList.Add(task);
                        }
                        Task.WaitAll(taskList.ToArray());
                        foreach (Task<List<TModel>> itemTask in taskList)
                        {
                            dbModelList.AddRange(itemTask.Result);
                        }
                        //清除变量,让GC快速回收
                        rowList = null;
                        taskList = null;
                        list = null;
                    }
                    else
                    {
                        //单线程
                        foreach (DataRow row in ds.Tables[0].Rows)
                        {
                            //用属性的Set方法赋值
                            newT = newModel<TModel>(propertys, row);
                            dbModelList.Add(newT);
                        }
                    }
                }
            }
            return dbModelList;
        }

        private static List<TModel> tableToModelList<TModel>(List<DataRow> rowList, Dictionary<string, PropertyInfo> propertys) where TModel : IModel, new()
        {
            var modelList = new List<TModel>();
            if (rowList != null && rowList.Count > 0)
            {
                foreach (DataRow row in rowList)
                {
                    modelList.Add(newModel<TModel>(propertys, row));
                }
            }
            return modelList;
        }
}

 

posted @ 2023-01-26 19:21  baivfhpwxf  阅读(19)  评论(0编辑  收藏  举报