ASP .Net Core 使用 Dapper 轻型ORM框架

一:优势

1,Dapper是一个轻型的ORM类。代码就一个SqlMapper.cs文件,编译后就40K的一个很小的Dll.

2,Dapper很快。Dapper的速度接近与IDataReader,取列表的数据超过了DataTable。

3,Dapper支持什么数据库。Dapper支持Mysql,SqlLite,Mssql2000,Mssql2005,Oracle等一系列的数据库,当然如果你知道原理也可以让它支持Mongo db

4,Dapper的r支持多表并联的对象。支持一对多 多对多的关系。并且没侵入性,想用就用,不想用就不用。无XML无属性。代码以前怎么写现在还怎么写。 5,Dapper原理通过Emit反射IDataReader的序列队列,来快速的得到和产生对象。性能实在高高高。

6,Dapper支持net2.0,3.0,3.5,4.0。【如果想在Net2.0下使用,可以去网上找一下Net2.0下如何配置运行Net3.5即可。】 7,Dapper语法十分简单。并且无须迁就数据库的设计。

二:ASP .Net Core 中使用dapper可以很方便的操作mysql数据

需要安装如下类库
    "Dapper": "1.50.2",
    "NETStandard.Library": "1.6.0",
    "SapientGuardian.MySql.Data": "6.9.813"

三:基于dapper的操作mysql的封装类

    /// 
    ///作者: 逍遥帝君
    /// 2016-10-26
    /// 
    public class DapperMySQLHelp
    {

        #region +ExcuteNonQuery 增、删、改同步操作
        /// 
        /// 增、删、改同步操作
        /// 作者: 逍遥帝君
        /// 2016-10-26
        ///  
        /// 实体
        /// 链接字符串
        /// sql语句
        /// 参数
        /// true存储过程,false sql语句
        /// int
        public int ExcuteNonQuery(string connection, string cmd, DynamicParameters param, bool flag = true) where T : class, new()
        {
            int result = 0;
            using (MySqlConnection con = new MySqlConnection(connection))
            {
                if (flag)
                {
                    result = con.Execute(cmd, param, null, null, CommandType.StoredProcedure);
                }
                else
                {
                    result = con.Execute(cmd, param, null, null, CommandType.Text);
                }
            }
            return result;
        }
        #endregion

        #region +ExcuteNonQueryAsync 增、删、改异步操作
        /// 
        /// 增、删、改异步操作
        /// 作者: 逍遥帝君
        /// 2016-10-26
        /// 
        /// 实体
        /// 链接字符串
        /// sql语句
        /// 参数
        /// true存储过程,false sql语句
        /// int
        public async Task<int> ExcuteNonQueryAsync(string connection, string cmd, DynamicParameters param, bool flag = true) where T : class, new()
        {
            int result = 0;
            using (MySqlConnection con = new MySqlConnection(connection))
            {
                if (flag)
                {
                    result = await con.ExecuteAsync(cmd, param, null, null, CommandType.StoredProcedure);
                }
                else
                {
                    result = await con.ExecuteAsync(cmd, param, null, null, CommandType.Text);
                }
            }
            return result;
        }
        #endregion

        #region +ExecuteScalar 同步查询操作
        /// 
        /// 同步查询操作
        /// 作者: 逍遥帝君
        /// 2016-10-26
        /// 
        /// 实体
        /// 连接字符串
        /// sql语句
        /// 参数
        /// true存储过程,false sql语句
        /// object
        public object ExecuteScalar(string connection, string cmd, DynamicParameters param, bool flag = true) where T : class, new()
        {
            object result = null;
            using (MySqlConnection con = new MySqlConnection(connection))
            {
                if (flag)
                {
                    result = con.ExecuteScalar(cmd, param, null, null, CommandType.StoredProcedure);
                }
                else
                {
                    result = con.ExecuteScalar(cmd, param, null, null, CommandType.Text);
                }
            }
            return result;
        }
        #endregion

        #region +ExecuteScalarAsync 异步查询操作
        /// 
        /// 异步查询操作
        /// 作者: 逍遥帝君
        /// 2016-10-26
        /// 
        /// 实体
        /// 连接字符串
        /// sql语句
        /// 参数
        /// true存储过程,false sql语句
        /// object
        public async Task<object> ExecuteScalarAsync(string connection, string cmd, DynamicParameters param, bool flag = true) where T : class, new()
        {
            object result = null;
            using (MySqlConnection con = new MySqlConnection(connection))
            {
                if (flag)
                {
                    result = await con.ExecuteScalarAsync(cmd, param, null, null, CommandType.StoredProcedure);
                }
                else
                {
                    result = con.ExecuteScalarAsync(cmd, param, null, null, CommandType.Text);
                }
            }
            return result;
        }
        #endregion

        #region +FindOne  同步查询一条数据
        /// 
        /// 同步查询一条数据
        /// 作者: 逍遥帝君
        /// 2016-10-26
        /// 
        /// 实体
        /// 连接字符串
        /// sql语句
        /// 参数
        /// true存储过程,false sql语句
        /// t
        public T FindOne(string connection, string cmd, DynamicParameters param, bool flag = true) where T : class, new()
        {
            IDataReader dataReader = null;
            using (MySqlConnection con = new MySqlConnection(connection))
            {
                if (flag)
                {
                    dataReader = con.ExecuteReader(cmd, param, null, null, CommandType.StoredProcedure);
                }
                else
                {
                    dataReader = con.ExecuteReader(cmd, param, null, null, CommandType.Text);
                }
                if (dataReader == null || !dataReader.Read()) return null;
                Type type = typeof(T);
                T t = new T();
                foreach (var item in type.GetProperties())
                {
                    for (int i = 0; i < dataReader.FieldCount; i++)
                    {
                        //属性名与查询出来的列名比较
                        if (item.Name.ToLower() != dataReader.GetName(i).ToLower()) continue;
                        var kvalue = dataReader[item.Name];
                        if (kvalue == DBNull.Value) continue;
                        item.SetValue(t, kvalue, null);
                        break;
                    }
                }
                return t;
            }
        }
        #endregion

        #region +FindOne  异步查询一条数据
        /// 
        /// 异步查询一条数据
        /// 作者: 逍遥帝君
        /// 2016-10-26
        /// 
        /// 实体
        /// 连接字符串
        /// sql语句
        /// 参数
        /// true存储过程,false sql语句
        /// t
        public async Task FindOneAsync(string connection, string cmd, DynamicParameters param, bool flag = true) where T : class, new()
        {
            IDataReader dataReader = null;
            using (MySqlConnection con = new MySqlConnection(connection))
            {
                if (flag)
                {
                    dataReader = await con.ExecuteReaderAsync(cmd, param, null, null, CommandType.StoredProcedure);
                }
                else
                {
                    dataReader = await con.ExecuteReaderAsync(cmd, param, null, null, CommandType.Text);
                }
                if (dataReader == null || !dataReader.Read()) return null;
                Type type = typeof(T);
                T t = new T();
                foreach (var item in type.GetProperties())
                {
                    for (int i = 0; i < dataReader.FieldCount; i++)
                    {
                        //属性名与查询出来的列名比较
                        if (item.Name.ToLower() != dataReader.GetName(i).ToLower()) continue;
                        var kvalue = dataReader[item.Name];
                        if (kvalue == DBNull.Value) continue;
                        item.SetValue(t, kvalue, null);
                        break;
                    }
                }
                return t;
            }
        }
        #endregion

        #region +FindToList  同步查询数据集合
        /// 
        /// 同步查询数据集合
        /// 作者: 逍遥帝君
        /// 2016-10-26
        /// 
        /// 实体
        /// 连接字符串
        /// sql语句
        /// 参数
        /// true存储过程,false sql语句
        /// t
        public IList FindToList(string connection, string cmd, DynamicParameters param, bool flag = true) where T : class, new()
        {
            IDataReader dataReader = null;
            using (MySqlConnection con = new MySqlConnection(connection))
            {
                if (flag)
                {
                    dataReader = con.ExecuteReader(cmd, param, null, null, CommandType.StoredProcedure);
                }
                else
                {
                    dataReader = con.ExecuteReader(cmd, param, null, null, CommandType.Text);
                }
                if (dataReader == null || !dataReader.Read()) return null;
                Type type = typeof(T);
                List tlist = new List();
                while (dataReader.Read())
                {
                    T t = new T();
                    foreach (var item in type.GetProperties())
                    {
                        for (int i = 0; i < dataReader.FieldCount; i++)
                        {
                            //属性名与查询出来的列名比较
                            if (item.Name.ToLower() != dataReader.GetName(i).ToLower()) continue;
                            var kvalue = dataReader[item.Name];
                            if (kvalue == DBNull.Value) continue;
                            item.SetValue(t, kvalue, null);
                            break;
                        }
                    }
                    if (tlist != null) tlist.Add(t);
                }
                return tlist;
            }
        }
        #endregion

        #region +FindToListAsync  异步查询数据集合
        /// 
        /// 异步查询数据集合
        /// 作者: 逍遥帝君
        /// 2016-10-26
        /// 
        /// 实体
        /// 连接字符串
        /// sql语句
        /// 参数
        /// true存储过程,false sql语句
        /// t
        public async Task> FindToListAsync(string connection, string cmd, DynamicParameters param, bool flag = true) where T : class, new()
        {
            IDataReader dataReader = null;
            using (MySqlConnection con = new MySqlConnection(connection))
            {
                if (flag)
                {
                    dataReader = await con.ExecuteReaderAsync(cmd, param, null, null, CommandType.StoredProcedure);
                }
                else
                {
                    dataReader = await con.ExecuteReaderAsync(cmd, param, null, null, CommandType.Text);
                }
                if (dataReader == null || !dataReader.Read()) return null;
                Type type = typeof(T);
                List tlist = new List();
                while (dataReader.Read())
                {
                    T t = new T();
                    foreach (var item in type.GetProperties())
                    {
                        for (int i = 0; i < dataReader.FieldCount; i++)
                        {
                            //属性名与查询出来的列名比较
                            if (item.Name.ToLower() != dataReader.GetName(i).ToLower()) continue;
                            var kvalue = dataReader[item.Name];
                            if (kvalue == DBNull.Value) continue;
                            item.SetValue(t, kvalue, null);
                            break;
                        }
                    }
                    if (tlist != null) tlist.Add(t);
                }
                return tlist;
            }
        }
        #endregion

        #region +FindToList  同步查询数据集合
        /// 
        /// 同步查询数据集合
        ///   作者: 逍遥帝君
        /// 2016-10-26
        /// 
        /// 实体
        /// 连接字符串
        /// sql语句
        /// 参数
        /// true存储过程,false sql语句
        /// t
        public IList FindToListAsPage(string connection, string cmd, DynamicParameters param, bool flag = true) where T : class, new()
        {
            IDataReader dataReader = null;
            using (MySqlConnection con = new MySqlConnection(connection))
            {
                if (flag)
                {
                    dataReader = con.ExecuteReader(cmd, param, null, null, CommandType.StoredProcedure);
                }
                else
                {
                    dataReader = con.ExecuteReader(cmd, param, null, null, CommandType.Text);
                }
                if (dataReader == null || !dataReader.Read()) return null;
                Type type = typeof(T);
                List tlist = new List();
                while (dataReader.Read())
                {
                    T t = new T();
                    foreach (var item in type.GetProperties())
                    {
                        for (int i = 0; i < dataReader.FieldCount; i++)
                        {
                            //属性名与查询出来的列名比较
                            if (item.Name.ToLower() != dataReader.GetName(i).ToLower()) continue;
                            var kvalue = dataReader[item.Name];
                            if (kvalue == DBNull.Value) continue;
                            item.SetValue(t, kvalue, null);
                            break;
                        }
                    }
                    if (tlist != null) tlist.Add(t);
                }
                return tlist;
            }
        }
        #endregion

        #region +FindToListByPage  同步分页查询数据集合
        /// 
        /// 同步分页查询数据集合
        /// 作者: 逍遥帝君
        /// 2016-10-26
        /// 
        /// 实体
        /// 连接字符串
        /// sql语句
        /// 参数
        /// true存储过程,false sql语句
        /// t
        public IList FindToListByPage(string connection, string cmd, DynamicParameters param, bool flag = true) where T : class, new()
        {
            IDataReader dataReader = null;
            using (MySqlConnection con = new MySqlConnection(connection))
            {
                if (flag)
                {
                    dataReader = con.ExecuteReader(cmd, param, null, null, CommandType.StoredProcedure);
                }
                else
                {
                    dataReader = con.ExecuteReader(cmd, param, null, null, CommandType.Text);
                }
                if (dataReader == null || !dataReader.Read()) return null;
                Type type = typeof(T);
                List tlist = new List();
                while (dataReader.Read())
                {
                    T t = new T();
                    foreach (var item in type.GetProperties())
                    {
                        for (int i = 0; i < dataReader.FieldCount; i++)
                        {
                            //属性名与查询出来的列名比较
                            if (item.Name.ToLower() != dataReader.GetName(i).ToLower()) continue;
                            var kvalue = dataReader[item.Name];
                            if (kvalue == DBNull.Value) continue;
                            item.SetValue(t, kvalue, null);
                            break;
                        }
                    }
                    if (tlist != null) tlist.Add(t);
                }
                return tlist;
            }
        }
        #endregion

        #region +FindToListByPageAsync  异步分页查询数据集合
        /// 
        /// 异步分页查询数据集合
        /// 作者: 逍遥帝君
        /// 2016-10-26
        /// 
        /// 实体
        /// 连接字符串
        /// sql语句
        /// 参数
        /// true存储过程,false sql语句
        /// t
        public async Task> FindToListByPageAsync(string connection, string cmd, DynamicParameters param, bool flag = true) where T : class, new()
        {
            IDataReader dataReader = null;
            using (MySqlConnection con = new MySqlConnection(connection))
            {
                if (flag)
                {
                    dataReader = await con.ExecuteReaderAsync(cmd, param, null, null, CommandType.StoredProcedure);
                }
                else
                {
                    dataReader = await con.ExecuteReaderAsync(cmd, param, null, null, CommandType.Text);
                }
                if (dataReader == null || !dataReader.Read()) return null;
                Type type = typeof(T);
                List tlist = new List();
                while (dataReader.Read())
                {
                    T t = new T();
                    foreach (var item in type.GetProperties())
                    {
                        for (int i = 0; i < dataReader.FieldCount; i++)
                        {
                            //属性名与查询出来的列名比较
                            if (item.Name.ToLower() != dataReader.GetName(i).ToLower()) continue;
                            var kvalue = dataReader[item.Name];
                            if (kvalue == DBNull.Value) continue;
                            item.SetValue(t, kvalue, null);
                            break;
                        }
                    }
                    if (tlist != null) tlist.Add(t);
                }
                return tlist;
            }
        }
        #endregion

    }

四:分页查询存储过程

存储过程
CREATE PROCEDURE  page_getperson
(
  _pageIndex INT,
  _pageSize INT,
  out _pagecount int
)
BEGIN
DECLARE startIndex
int DEFAULT 0; DECLARE tcount int DEFAULT 0; set startIndex=(_pageIndex-1)*_pageSize; SELECT * from Person LIMIT startIndex,_pageSize; SELECT COUNT(id) into tcount from Person; set _pagecount=tcount; END

五:参数写法

 DynamicParameters param = new DynamicParameters();
  param.Add("_pageIndex", 2);
  param.Add("_pageSize", 5);
  param.Add("_pagecount", dbType: DbType.Int32, direction: ParameterDirection.Output);
  var result = mysql.FindToListByPage(connection, "page_getperson", param);
//总条数
var count = param.Get<int>("_pagecount"); var kk = result;

 

转载于:https://www.cnblogs.com/xiaoyaodijun/p/6005771.html

你可能感兴趣的:(ASP .Net Core 使用 Dapper 轻型ORM框架)