.Net Core中使用Dapper构建泛型仓储

前言:Dapper是.NET的简单对象映射器, 在速度方面 拥有 ORM 的称号, 与使用原始ADO.NET读取数据一样快。 ORM是对象关系映射器,它负责数据库和编程语言之间的映射。

仓储主要是用来解耦业务逻辑层与数据访问层,降低耦合,使得程序在后期有很强的扩展性。

首先新建四个类库

IRepository:

Repository:

引用IRepository

IServices:

Services:

引用IServices、IRepository

导入两个包:Dapper,Npgsql(我用的是pgsql数据库)

万事俱备,上代码了。

IBaseRepository

 public interface IBaseRepository where TEntity : class, new()
    {
        /// 
        /// 查询数据集合
        /// 
        /// 查询Sql语句或存储过程名称
        /// 参数值(可选)
        /// 事务名称(可选)
        /// 超时时间(可选)
        /// 指定如果解释sql字符串:语句/存储过程(可选)
        /// 返回指定实体泛型
        Task QueryFirst(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);

        /// 
        /// 返回 dataset
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        Task Query(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);

        /// 
        /// 查询数据集合
        /// 
        /// 查询Sql语句或存储过程名称
        /// 参数值(可选)
        /// 事务名称(可选)
        /// 是否将查询结果缓存到内存中(可选)
        /// 超时时间(可选)
        /// 指定如果解释sql字符串:语句/存储过程(可选)
        /// 返回指定泛型集合
        Task> QueryList(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null);


        /// 
        /// 简单分页,返回分页后的泛型集合
        /// 
        /// 分页后的泛型集合
        /// 查询Sql语句或存储过程名称
        /// 返回 总记录数
        /// 参数值(可选)
        /// 事务名称(可选)
        /// 是否将查询结果缓存到内存中(可选)
        /// 超时时间(可选)
        /// 指定如果解释sql字符串:语句/存储过程(可选)
        /// 返回分页后的泛型集合
        Task, int>> QueryPagination(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null);

        /// 
        /// 2条Sql语句查询
        /// 
        /// 实体集合一
        /// 实体集合二
        /// 2条查询语句
        /// 返回第一条语句的实体集合
        /// 返回第二条语句的实体集合
        /// 参数值(可选)
        Task, List>> QueryMultiple(string sql, object param = null);

        /// 
        /// 3条Sql语句查询
        /// 
        /// 实体集合一
        /// 实体集合二
        /// 实体集合三
        /// 5条查询语句
        /// 返回第一条语句的实体集合
        /// 返回第二条语句的实体集合
        /// 返回第三条语句的实体集合
        /// 参数值(可选)
        Task, List, List>> QueryMultiple(string sql, object param = null);


        /// 
        /// 4条Sql语句查询
        /// 
        /// 实体集合一
        /// 实体集合二
        /// 实体集合三
        /// 实体集合四
        /// 5条查询语句
        /// 返回第一条语句的实体集合
        /// 返回第二条语句的实体集合
        /// 返回第三条语句的实体集合
        /// 返回第四条语句的实体集合
        /// 参数值(可选)
        Task, List, List, List>> QueryMultiple(string sql, object param = null);

        /// 
        /// 5条Sql语句查询
        /// 
        /// 实体集合一
        /// 实体集合二
        /// 实体集合三
        /// 实体集合四
        /// 实体集合五
        /// 5条查询语句
        /// 返回第一条语句的实体集合
        /// 返回第二条语句的实体集合
        /// 返回第三条语句的实体集合
        /// 返回第四条语句的实体集合
        /// 返回第五条语句的实体集合
        /// 参数值(可选)
        Task, List, List, List, List>> QueryMultiple(string sql, object param = null);


        /// 
        /// 查询单个实体类型
        /// 
        /// 实体类型
        /// 查询Sql语句或存储过程名称
        /// 参数值(可选)
        /// 事务名称(可选)
        /// 是否将查询结果缓存到内存中(可选)
        /// 超时时间(可选)
        /// 指定如果解释sql字符串:语句/存储过程(可选)
        /// 泛型实体类型
        Task QueryOne(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null);

        /// 
        /// 执行sql语句,返回受影响的行数
        /// 
        /// 查询Sql语句或存储过程名称
        /// 参数值(可选)
        /// 事务名称(可选)
        /// 超时时间(可选)
        /// 指定如果解释sql字符串:语句/存储过程(可选)
        /// 返回受影响的行数
        Task Execute(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);

        /// 
        /// 执行sql语句,返回第一行第一列
        /// 
        /// 指定类型
        /// 查询Sql语句
        /// 参数值(可选)
        /// 事务名称(可选)
        /// 超时时间(可选)
        /// 指定如果解释sql字符串:语句/存储过程(可选)
        /// 返回返回第一行第一列
        Task ExecuteScalar(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);

        /// 
        /// 执行存储过程,返回第一行第一列
        /// 
        /// 指定类型
        /// 存储过程名称
        /// 参数键值对
        /// 返回第一行第一列
        Task Execute(string command, Dictionary paras);
    }

BaseRepository

  public class BaseRepository : IBaseRepository where TEntity : class, new()
    {
        private readonly IConfiguration _configuration;
        public BaseRepository(IConfiguration configuration) => this._configuration = configuration;

        /// 
        /// 创建数据库连接,并打开连接
        /// 连接字符串写在 json 配置文件里面
        /// 
        /// IDbConnection
        public IDbConnection GetOpenConn()
        {
            IDbConnection con = null;
            string connectionString = _configuration["Connection:dbContent"];
            con = new NpgsqlConnection(connectionString);
            try
            {
                con.Open();
            }
            catch (Exception ex)
            {
                throw new Exception("数据库连接错误:" + ex.Message);
            }

            return con;
        }


        /// 
        /// 查询数据集合
        /// 
        /// 查询Sql语句或存储过程名称
        /// 参数值(可选)
        /// 事务名称(可选)
        /// 超时时间(可选)
        /// 指定如果解释sql字符串:语句/存储过程(可选)
        /// 返回指定实体泛型
        public async Task QueryFirst(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            using (IDbConnection con = this.GetOpenConn())
            {
                return con.QueryFirst(sql, param, transaction, commandTimeout, commandType);
            }
        }

        /// 
        /// 返回 dataset
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public async Task Query(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            using (IDbConnection con = GetOpenConn())
            {
                IDataReader reader = con.ExecuteReader(sql, param, transaction, commandTimeout, commandType);
                DataSet ds = new XDataSet();
                ds.Load(reader, LoadOption.OverwriteChanges, null, new DataTable[] { });
                return ds;
            }
        }


        /// 
        /// 查询数据集合
        /// 
        /// 查询Sql语句或存储过程名称
        /// 参数值(可选)
        /// 事务名称(可选)
        /// 是否将查询结果缓存到内存中(可选)
        /// 超时时间(可选)
        /// 指定如果解释sql字符串:语句/存储过程(可选)
        /// 返回指定泛型集合
        public async Task> QueryList(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
        {
            using (IDbConnection con = GetOpenConn())
            {
                return con.Query(sql, param, transaction, buffered, commandTimeout, commandType);
            }
        }

        /// 
        /// 简单分页,返回分页后的泛型集合
        /// 
        /// 分页后的泛型集合
        /// 查询Sql语句或存储过程名称
        /// 返回 总记录数
        /// 参数值(可选)
        /// 事务名称(可选)
        /// 是否将查询结果缓存到内存中(可选)
        /// 超时时间(可选)
        /// 指定如果解释sql字符串:语句/存储过程(可选)
        /// 返回分页后的泛型集合
        public async Task, int>> QueryPagination(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
        {
            using (IDbConnection con = GetOpenConn())
            {
                var multi = con.QueryMultiple(sql, param, transaction, commandTimeout, commandType);
                int totalCount = int.Parse(multi.Read().Single().ToString());
                return Tuple.Create, int>(multi.Read(), totalCount);
            }
        }

        /// 
        /// 2条Sql语句查询
        /// 
        /// 实体集合一
        /// 实体集合二
        /// 2条查询语句
        /// 返回第一条语句的实体集合
        /// 返回第二条语句的实体集合
        /// 参数值(可选)
        public async Task, List>> QueryMultiple(string sql, object param = null)
        {
            using (IDbConnection con = GetOpenConn())
            {
                var multi = con.QueryMultiple(sql, param);
                var tfList = new List();
                var tsList = new List();
                if (!multi.IsConsumed)
                {
                    tfList = multi.Read().ToList();
                    tsList = multi.Read().ToList();
                }
                return Tuple.Create, List>(tfList, tsList);
            }
        }

        /// 
        /// 3条Sql语句查询
        /// 
        /// 实体集合一
        /// 实体集合二
        /// 实体集合三
        /// 5条查询语句
        /// 返回第一条语句的实体集合
        /// 返回第二条语句的实体集合
        /// 返回第三条语句的实体集合
        /// 参数值(可选)
        public async Task, List, List>> QueryMultiple(string sql, object param = null)
        {
            using (IDbConnection con = GetOpenConn())
            {
                var multi = con.QueryMultiple(sql, param);
                var tfList = new List();
                var tsList = new List();
                var ttList = new List();
                if (!multi.IsConsumed)
                {
                    tfList = multi.Read().ToList();
                    tsList = multi.Read().ToList();
                    ttList = multi.Read().ToList();
                }
                return Tuple.Create, List, List>(tfList, tsList, ttList);
            }
        }

        /// 
        /// 4条Sql语句查询
        /// 
        /// 实体集合一
        /// 实体集合二
        /// 实体集合三
        /// 实体集合四
        /// 5条查询语句
        /// 返回第一条语句的实体集合
        /// 返回第二条语句的实体集合
        /// 返回第三条语句的实体集合
        /// 返回第四条语句的实体集合
        /// 参数值(可选)
        public async Task, List, List, List>> QueryMultiple(string sql, object param = null)
        {
            using (IDbConnection con = GetOpenConn())
            {
                var multi = con.QueryMultiple(sql, param);
                var tfList = new List();
                var tsList = new List();
                var ttList = new List();
                var tfourList = new List();
                if (!multi.IsConsumed)
                {
                    tfList = multi.Read().ToList();
                    tsList = multi.Read().ToList();
                    ttList = multi.Read().ToList();
                    tfourList = multi.Read().ToList();
                }
                return Tuple.Create, List, List, List>(tfList, tsList, ttList, tfourList);
            }
        }

        /// 
        /// 5条Sql语句查询
        /// 
        /// 实体集合一
        /// 实体集合二
        /// 实体集合三
        /// 实体集合四
        /// 实体集合五
        /// 5条查询语句
        /// 返回第一条语句的实体集合
        /// 返回第二条语句的实体集合
        /// 返回第三条语句的实体集合
        /// 返回第四条语句的实体集合
        /// 返回第五条语句的实体集合
        /// 参数值(可选)
        public async Task, List, List, List, List>> QueryMultiple(string sql, object param = null)
        {
            using (IDbConnection con = GetOpenConn())
            {
                var multi = con.QueryMultiple(sql, param);
                var tfList = new List();
                var tsList = new List();
                var ttList = new List();
                var tfourList = new List();
                var tfiveList = new List();
                if (!multi.IsConsumed)
                {
                    tfList = multi.Read().ToList();
                    tsList = multi.Read().ToList();
                    ttList = multi.Read().ToList();
                    tfourList = multi.Read().ToList();
                    tfiveList = multi.Read().ToList();
                }
                return Tuple.Create, List, List, List, List>(tfList, tsList, ttList, tfourList, tfiveList);
            }
        }

        /// 
        /// 查询单个实体类型
        /// 
        /// 实体类型
        /// 查询Sql语句或存储过程名称
        /// 参数值(可选)
        /// 事务名称(可选)
        /// 是否将查询结果缓存到内存中(可选)
        /// 超时时间(可选)
        /// 指定如果解释sql字符串:语句/存储过程(可选)
        /// 泛型实体类型
        public async Task QueryOne(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
        {
            var dataResult = await QueryList(sql, param, transaction, buffered, commandTimeout, commandType);
            return dataResult != null && dataResult.Count() > 0 ? dataResult.ToList()[0] : new TEntity();
        }

        /// 
        /// 执行sql语句,返回受影响的行数
        /// 
        /// 查询Sql语句或存储过程名称
        /// 参数值(可选)
        /// 事务名称(可选)
        /// 超时时间(可选)
        /// 指定如果解释sql字符串:语句/存储过程(可选)
        /// 返回受影响的行数
        public async Task Execute(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            using (IDbConnection con = GetOpenConn())
            {
                return con.Execute(sql, param, transaction, commandTimeout, commandType);
            }
        }

        /// 
        /// 执行sql语句,返回第一行第一列
        /// 
        /// 指定类型
        /// 查询Sql语句
        /// 参数值(可选)
        /// 事务名称(可选)
        /// 超时时间(可选)
        /// 指定如果解释sql字符串:语句/存储过程(可选)
        /// 返回返回第一行第一列
        public async Task ExecuteScalar(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            using (IDbConnection con = GetOpenConn())
            {
                return con.ExecuteScalar(sql, param, transaction, commandTimeout, commandType);
            }
        }

        /// 
        /// 执行存储过程,返回第一行第一列
        /// 
        /// 指定类型
        /// 存储过程名称
        /// 参数键值对
        /// 返回第一行第一列
        public async Task Execute(string command, Dictionary paras)
        {
            using (IDbConnection con = GetOpenConn())
            {
                IDbCommand com = con.CreateCommand();
                com.CommandText = command;
                com.CommandType = CommandType.StoredProcedure;

                if (paras != null)
                {
                    foreach (var item in paras.Keys)
                    {
                        IDbDataParameter para = com.CreateParameter();
                        para.Value = paras[item];
                        para.ParameterName = item;
                        com.Parameters.Add(para);
                    }
                }

                return (TEntity)com.ExecuteScalar();
            }
        }

        /// 
        /// 数据适配器,扩展Fill方法
        /// .NET的DataSet.Load方法,底层调用DataAdapter.Fill(DataTable[], IDataReader, int, int)
        /// Dapper想要返回DataSet,需要重写Load方法,不必传入DataTable[],因为数组长度不确定
        /// 
        public class XLoadAdapter : DataAdapter
        {
            /// 
            /// 数据适配器
            /// 
            public XLoadAdapter()
            {
            }

            /// 
            /// 读取dataReader
            /// 
            /// 
            /// 
            /// 
            /// 
            /// 
            public int FillFromReader(DataSet ds, IDataReader dataReader, int startRecord, int maxRecords)
            {
                return this.Fill(ds, "Table", dataReader, startRecord, maxRecords);
            }
        }

        /// 
        /// 扩展Load方法
        /// 
        public class XDataSet : DataSet
        {
            /// 
            /// Dapper想要返回DataSet,需要重写Load方法
            /// 
            /// IDataReader
            /// LoadOption
            /// FillErrorEventHandler
            /// DataTable
            public override void Load(IDataReader reader, LoadOption loadOption, FillErrorEventHandler handler, params DataTable[] tables)
            {
                XLoadAdapter adapter = new XLoadAdapter
                {
                    FillLoadOption = loadOption,
                    MissingSchemaAction = MissingSchemaAction.AddWithKey
                };
                if (handler != null)
                {
                    adapter.FillError += handler;
                }
                adapter.FillFromReader(this, reader, 0, 0);
                if (!reader.IsClosed && !reader.NextResult())
                {
                    reader.Close();
                }
            }
        }
    }

IBaseServices

   public interface IBaseServices where TEntity : class
    {
        /// 
        /// 查询数据集合
        /// 
        /// 查询Sql语句或存储过程名称
        /// 参数值(可选)
        /// 事务名称(可选)
        /// 超时时间(可选)
        /// 指定如果解释sql字符串:语句/存储过程(可选)
        /// 返回指定实体泛型
        Task QueryFirst(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);

        /// 
        /// 返回 dataset
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        Task Query(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);

        /// 
        /// 查询数据集合
        /// 
        /// 查询Sql语句或存储过程名称
        /// 参数值(可选)
        /// 事务名称(可选)
        /// 是否将查询结果缓存到内存中(可选)
        /// 超时时间(可选)
        /// 指定如果解释sql字符串:语句/存储过程(可选)
        /// 返回指定泛型集合
        Task> QueryList(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null);


        /// 
        /// 简单分页,返回分页后的泛型集合
        /// 
        /// 分页后的泛型集合
        /// 查询Sql语句或存储过程名称
        /// 返回 总记录数
        /// 参数值(可选)
        /// 事务名称(可选)
        /// 是否将查询结果缓存到内存中(可选)
        /// 超时时间(可选)
        /// 指定如果解释sql字符串:语句/存储过程(可选)
        /// 返回分页后的泛型集合
        Task, int>> QueryPagination(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null);

        /// 
        /// 2条Sql语句查询
        /// 
        /// 实体集合一
        /// 实体集合二
        /// 2条查询语句
        /// 返回第一条语句的实体集合
        /// 返回第二条语句的实体集合
        /// 参数值(可选)
        Task, List>> QueryMultiple(string sql, object param = null);

        /// 
        /// 3条Sql语句查询
        /// 
        /// 实体集合一
        /// 实体集合二
        /// 实体集合三
        /// 5条查询语句
        /// 返回第一条语句的实体集合
        /// 返回第二条语句的实体集合
        /// 返回第三条语句的实体集合
        /// 参数值(可选)
        Task, List, List>> QueryMultiple(string sql, object param = null);


        /// 
        /// 4条Sql语句查询
        /// 
        /// 实体集合一
        /// 实体集合二
        /// 实体集合三
        /// 实体集合四
        /// 5条查询语句
        /// 返回第一条语句的实体集合
        /// 返回第二条语句的实体集合
        /// 返回第三条语句的实体集合
        /// 返回第四条语句的实体集合
        /// 参数值(可选)
        Task, List, List, List>> QueryMultiple(string sql, object param = null);

        /// 
        /// 5条Sql语句查询
        /// 
        /// 实体集合一
        /// 实体集合二
        /// 实体集合三
        /// 实体集合四
        /// 实体集合五
        /// 5条查询语句
        /// 返回第一条语句的实体集合
        /// 返回第二条语句的实体集合
        /// 返回第三条语句的实体集合
        /// 返回第四条语句的实体集合
        /// 返回第五条语句的实体集合
        /// 参数值(可选)
        Task, List, List, List, List>> QueryMultiple(string sql, object param = null);


        /// 
        /// 查询单个实体类型
        /// 
        /// 实体类型
        /// 查询Sql语句或存储过程名称
        /// 参数值(可选)
        /// 事务名称(可选)
        /// 是否将查询结果缓存到内存中(可选)
        /// 超时时间(可选)
        /// 指定如果解释sql字符串:语句/存储过程(可选)
        /// 泛型实体类型
        Task QueryOne(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null);

        /// 
        /// 执行sql语句,返回受影响的行数
        /// 
        /// 查询Sql语句或存储过程名称
        /// 参数值(可选)
        /// 事务名称(可选)
        /// 超时时间(可选)
        /// 指定如果解释sql字符串:语句/存储过程(可选)
        /// 返回受影响的行数
        Task Execute(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);

        /// 
        /// 执行sql语句,返回第一行第一列
        /// 
        /// 指定类型
        /// 查询Sql语句
        /// 参数值(可选)
        /// 事务名称(可选)
        /// 超时时间(可选)
        /// 指定如果解释sql字符串:语句/存储过程(可选)
        /// 返回返回第一行第一列
        Task ExecuteScalar(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);

        /// 
        /// 执行存储过程,返回第一行第一列
        /// 
        /// 指定类型
        /// 存储过程名称
        /// 参数键值对
        /// 返回第一行第一列
        Task Execute(string command, Dictionary paras);
    }

BaseServices

 public class BaseServices : IBaseServices where TEntity : class, new()
    {
        public IBaseRepository BaseDal;

        /// 
        /// 查询数据集合
        /// 
        /// 查询Sql语句或存储过程名称
        /// 参数值(可选)
        /// 事务名称(可选)
        /// 超时时间(可选)
        /// 指定如果解释sql字符串:语句/存储过程(可选)
        /// 返回指定实体泛型
        public async Task QueryFirst(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await BaseDal.QueryFirst(sql, param, transaction, commandTimeout, commandType);
        }

        public Task Query(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return BaseDal.Query(sql, param, transaction, commandTimeout, commandType);
        }

        /// 
        /// 查询数据集合
        /// 
        /// 查询Sql语句或存储过程名称
        /// 参数值(可选)
        /// 事务名称(可选)
        /// 是否将查询结果缓存到内存中(可选)
        /// 超时时间(可选)
        /// 指定如果解释sql字符串:语句/存储过程(可选)
        /// 返回指定泛型集合
        public async Task> QueryList(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await BaseDal.QueryList(sql, param, transaction, buffered, commandTimeout, commandType);
        }


        /// 
        /// 简单分页,返回分页后的泛型集合
        /// 
        /// 分页后的泛型集合
        /// 查询Sql语句或存储过程名称
        /// 返回 总记录数
        /// 参数值(可选)
        /// 事务名称(可选)
        /// 是否将查询结果缓存到内存中(可选)
        /// 超时时间(可选)
        /// 指定如果解释sql字符串:语句/存储过程(可选)
        /// 返回分页后的泛型集合
        public async Task, int>> QueryPagination(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
        {

            return await BaseDal.QueryPagination(sql, param, transaction, buffered, commandTimeout, commandType);
        }

        /// 
        /// 2条Sql语句查询
        /// 
        /// 实体集合一
        /// 实体集合二
        /// 2条查询语句
        /// 返回第一条语句的实体集合
        /// 返回第二条语句的实体集合
        /// 参数值(可选)
        public async Task, List>> QueryMultiple(string sql, object param = null)
        {

            return await BaseDal.QueryMultiple(sql, param);
        }

        /// 
        /// 3条Sql语句查询
        /// 
        /// 实体集合一
        /// 实体集合二
        /// 实体集合三
        /// 5条查询语句
        /// 返回第一条语句的实体集合
        /// 返回第二条语句的实体集合
        /// 返回第三条语句的实体集合
        /// 参数值(可选)
        public async Task, List, List>> QueryMultiple(string sql, object param = null)
        {
            return await BaseDal.QueryMultiple(sql, param);
        }

        /// 
        /// 4条Sql语句查询
        /// 
        /// 实体集合一
        /// 实体集合二
        /// 实体集合三
        /// 实体集合四
        /// 5条查询语句
        /// 返回第一条语句的实体集合
        /// 返回第二条语句的实体集合
        /// 返回第三条语句的实体集合
        /// 返回第四条语句的实体集合
        /// 参数值(可选)
        public async Task, List, List, List>> QueryMultiple(string sql, object param = null)
        {

            return await BaseDal.QueryMultiple(sql, param);
        }

        /// 
        /// 5条Sql语句查询
        /// 
        /// 实体集合一
        /// 实体集合二
        /// 实体集合三
        /// 实体集合四
        /// 实体集合五
        /// 5条查询语句
        /// 返回第一条语句的实体集合
        /// 返回第二条语句的实体集合
        /// 返回第三条语句的实体集合
        /// 返回第四条语句的实体集合
        /// 返回第五条语句的实体集合
        /// 参数值(可选)
        public async Task, List, List, List, List>> QueryMultiple(string sql, object param = null)
        {
            return await BaseDal.QueryMultiple(sql, param);
        }

        /// 
        /// 查询单个实体类型
        /// 
        /// 实体类型
        /// 查询Sql语句或存储过程名称
        /// 参数值(可选)
        /// 事务名称(可选)
        /// 是否将查询结果缓存到内存中(可选)
        /// 超时时间(可选)
        /// 指定如果解释sql字符串:语句/存储过程(可选)
        /// 泛型实体类型
        public async Task QueryOne(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await BaseDal.QueryOne(sql, param, transaction, buffered, commandTimeout, commandType);
        }

        /// 
        /// 执行sql语句,返回受影响的行数
        /// 
        /// 查询Sql语句或存储过程名称
        /// 参数值(可选)
        /// 事务名称(可选)
        /// 超时时间(可选)
        /// 指定如果解释sql字符串:语句/存储过程(可选)
        /// 返回受影响的行数
        public async Task Execute(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await BaseDal.Execute(sql, param, transaction, commandTimeout, commandType);
        }

        /// 
        /// 执行sql语句,返回第一行第一列
        /// 
        /// 指定类型
        /// 查询Sql语句
        /// 参数值(可选)
        /// 事务名称(可选)
        /// 超时时间(可选)
        /// 指定如果解释sql字符串:语句/存储过程(可选)
        /// 返回返回第一行第一列
        public async Task ExecuteScalar(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await ExecuteScalar(sql, param, transaction, commandTimeout, commandType);
        }

        /// 
        /// 执行存储过程,返回第一行第一列
        /// 
        /// 指定类型
        /// 存储过程名称
        /// 参数键值对
        /// 返回第一行第一列
        public async Task Execute(string command, Dictionary paras)
        {
            return await BaseDal.Execute(command, paras);
        }
    }

下面写一个简单的查询,用户登录判断用户名与密码

   public interface IUserInfoRepository: IBaseRepository
    {
    }

  public class UserInfoRepository : BaseRepository, IUserInfoRepository
    {
        public UserInfoRepository(IConfiguration configuration) : base(configuration)
        {
        }
    }

    public interface IUserInfoServices:IBaseServices
    {
        Task> QueryUserInfo(string userName, string password);
    }

 public class UserInfoServices : BaseServices, IUserInfoServices
    {
        IUserInfoRepository _dal;
        public UserInfoServices(IUserInfoRepository dal)
        {
            this._dal = dal;
            base.BaseDal = dal;
        }

        public async Task> QueryUserInfo(string userName, string password)
        {var Result = await _dal.QueryList($"SELECT * FROM user_info where user_name='{userName}' and password='{password}' limit 1 OFFSET 0");
            return Result;
        }
    }

在控制器中以依赖注入的形式使用(前提是你要借助IOC容器注入好你要使用的对象),如果对此有困惑,可看下面这篇博客:

https://www.cnblogs.com/zhangnever/p/12324133.html

至此就没有了,各位同学可以再行封装。

如有不足的地方,还望见谅!