Dapper同时操作任意多张表的实现

1:Dapper的查询帮助类,部分代码,其它新增更新删除可以自行扩展

using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Text;

namespace FEG.ESB.Data.EF
{
    using Dapper;
    using MySql.Data.MySqlClient;
    using System.Threading.Tasks;
    using static Dapper.SqlMapper;

    /// 
    /// 
    /// 
    public class FEG_DapperHelper
    {
        private static string str = FEG_ConfigHelper.GetDbConnectionStr();

        /// 
        /// 查询 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static IEnumerable Query(string sql, object parames = null) where T : class, new()
        {
            try
            {
                using MySqlConnection conn = GetMySqlConnnetion();
                return conn.Query(sql, parames);
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// 
        /// 查询 异步操作 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static async Task> QueryAsync(string sql, object parames = null) where T : class, new()
        {
            try
            {
                using MySqlConnection conn = GetMySqlConnnetion();
                return await conn.QueryAsync(sql, parames);
            }
            catch (Exception)
            {
                return null;
            }
        }

        #region old
        ///// 
        ///// 查询两个实体的操作 
        ///// 
        ///// 
        ///// 
        ///// 
        //public static Tuple, IEnumerable> QueryTwoEntity(string sql, object parames = null) where T : class where M : class, new()
        //{
        //    try
        //    {
        //        using MySqlConnection conn = GetMySqlConnnetion();
        //        var readData = conn.QueryMultiple(sql, parames);
        //        var obj = readData.Read() as IEnumerable;
        //        var obj2 = readData.Read() as IEnumerable;
        //        return (obj, obj2).ToTuple();
        //    }
        //    catch (Exception)
        //    {
        //        return null;
        //    }
        //} 
        #endregion

        /// 
        /// 同时查询多个实体的操作
        /// 
        /// 
        /// 
        /// 
        /// 
        public static void QueryMultipeEntity(string sql, Dictionary<string, object> dicParams, Action funcObj)
        {
            using MySqlConnection conn = GetMySqlConnnetion();
            if (dicParams != null)
            {
                DynamicParameters ps = new DynamicParameters();
                foreach (string item in dicParams.Keys)
                {
                    ps.Add(item, dicParams[item]);
                }
                using (var readRsult = conn.QueryMultiple(sql, ps))
                {
                    funcObj.Invoke(readRsult);
                }
            }
        }

        /// 
        /// 获取 MySql连接
        /// 
        /// 
        private static MySqlConnection GetMySqlConnnetion()
        {
            return new MySqlConnection(str);
        }

        /// 
        /// 获取Dapper参数化对象,这里直接New来处理,不到处引入命名空间
        /// 
        /// 
        public static DynamicParameters GetDynamicParameters()
        {
            return new DynamicParameters();
        }
    }

    /// 
    /// 
    /// 
    public class FEG_ConfigHelper
    {
        public static IConfiguration _configuration { get; set; }
        /// 
        /// 获取连接数据库的字符串
        /// 
        /// 
        public static string GetDbConnectionStr()
        {
            return _configuration.GetSection("ConnectionStrings:ReadonlyConnection").Value;
        }

        /// 
        /// 
        /// 
        /// 
        /// 
        public static string GetAppSettingValueByKey(string key)
        {
            return _configuration.GetSection(key).Value;
        }

        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T GetAppSettingEntity(string key) where T : class, new()
        {
            return _configuration.GetSection(key).Value as T;
        }
    }
}
View Code

2:调用的代码:

 /// 
        /// Test dapper,同时查询多个表的操作
        /// 
        /// 
        [HttpPost,Route("TestGridReader")]
        public BasisApiResult TestGridReader()
        {
            BasisApiResult result = new BasisApiResult();
            result.data = _personnelService.TestGridReader();
            return result;
        }
View Code
       public Tuple, IEnumerable> TestGridReader()
        {
            IEnumerable plist = null;
            IEnumerable clist = null;
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("isdel", "0");
            FEG_DapperHelper.QueryMultipeEntity("select * from personnel where isdel=@isdel limit 0,1;select * from course  where isdel=@isdel limit 0,1;", dic, x =>
              {
                  plist = x.Read().ToList();
                  clist = x.Read().ToList();
              });
            return Tuple.Create(plist, clist);
        }
View Code

3:测试OK截图:

Dapper同时操作任意多张表的实现_第1张图片

 

 

你可能感兴趣的:(Dapper同时操作任意多张表的实现)