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的封装类
/// <summary> ///作者: 逍遥帝君 /// 2016-10-26 /// </summary> public class DapperMySQLHelp { #region +ExcuteNonQuery 增、删、改同步操作 /// <summary> /// 增、删、改同步操作 /// 作者: 逍遥帝君 /// 2016-10-26 /// </summary> /// <typeparam name="T">实体</typeparam> /// <param name="connection">链接字符串</param> /// <param name="cmd">sql语句</param> /// <param name="param">参数</param> /// <param name="flag">true存储过程,false sql语句</param> /// <returns>int</returns> public int ExcuteNonQuery<T>(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 增、删、改异步操作 /// <summary> /// 增、删、改异步操作 /// 作者: 逍遥帝君 /// 2016-10-26 /// </summary> /// <typeparam name="T">实体</typeparam> /// <param name="connection">链接字符串</param> /// <param name="cmd">sql语句</param> /// <param name="param">参数</param> /// <param name="flag">true存储过程,false sql语句</param> /// <returns>int</returns> public async Task<int> ExcuteNonQueryAsync<T>(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 同步查询操作 /// <summary> /// 同步查询操作 /// 作者: 逍遥帝君 /// 2016-10-26 /// </summary> /// <typeparam name="T">实体</typeparam> /// <param name="connection">连接字符串</param> /// <param name="cmd">sql语句</param> /// <param name="param">参数</param> /// <param name="flag">true存储过程,false sql语句</param> /// <returns>object</returns> public object ExecuteScalar<T>(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 异步查询操作 /// <summary> /// 异步查询操作 /// 作者: 逍遥帝君 /// 2016-10-26 /// </summary> /// <typeparam name="T">实体</typeparam> /// <param name="connection">连接字符串</param> /// <param name="cmd">sql语句</param> /// <param name="param">参数</param> /// <param name="flag">true存储过程,false sql语句</param> /// <returns>object</returns> public async Task<object> ExecuteScalarAsync<T>(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 同步查询一条数据 /// <summary> /// 同步查询一条数据 /// 作者: 逍遥帝君 /// 2016-10-26 /// </summary> /// <typeparam name="T">实体</typeparam> /// <param name="connection">连接字符串</param> /// <param name="cmd">sql语句</param> /// <param name="param">参数</param> /// <param name="flag">true存储过程,false sql语句</param> /// <returns>t</returns> public T FindOne<T>(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 异步查询一条数据 /// <summary> /// 异步查询一条数据 /// 作者: 逍遥帝君 /// 2016-10-26 /// </summary> /// <typeparam name="T">实体</typeparam> /// <param name="connection">连接字符串</param> /// <param name="cmd">sql语句</param> /// <param name="param">参数</param> /// <param name="flag">true存储过程,false sql语句</param> /// <returns>t</returns> public async Task<T> FindOneAsync<T>(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 同步查询数据集合 /// <summary> /// 同步查询数据集合 /// 作者: 逍遥帝君 /// 2016-10-26 /// </summary> /// <typeparam name="T">实体</typeparam> /// <param name="connection">连接字符串</param> /// <param name="cmd">sql语句</param> /// <param name="param">参数</param> /// <param name="flag">true存储过程,false sql语句</param> /// <returns>t</returns> public IList<T> FindToList<T>(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<T> tlist = new List<T>(); 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 异步查询数据集合 /// <summary> /// 异步查询数据集合 /// 作者: 逍遥帝君 /// 2016-10-26 /// </summary> /// <typeparam name="T">实体</typeparam> /// <param name="connection">连接字符串</param> /// <param name="cmd">sql语句</param> /// <param name="param">参数</param> /// <param name="flag">true存储过程,false sql语句</param> /// <returns>t</returns> public async Task<IList<T>> FindToListAsync<T>(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<T> tlist = new List<T>(); 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 同步查询数据集合 /// <summary> /// 同步查询数据集合 /// 作者: 逍遥帝君 /// 2016-10-26 /// </summary> /// <typeparam name="T">实体</typeparam> /// <param name="connection">连接字符串</param> /// <param name="cmd">sql语句</param> /// <param name="param">参数</param> /// <param name="flag">true存储过程,false sql语句</param> /// <returns>t</returns> public IList<T> FindToListAsPage<T>(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<T> tlist = new List<T>(); 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 同步分页查询数据集合 /// <summary> /// 同步分页查询数据集合 /// 作者: 逍遥帝君 /// 2016-10-26 /// </summary> /// <typeparam name="T">实体</typeparam> /// <param name="connection">连接字符串</param> /// <param name="cmd">sql语句</param> /// <param name="param">参数</param> /// <param name="flag">true存储过程,false sql语句</param> /// <returns>t</returns> public IList<T> FindToListByPage<T>(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<T> tlist = new List<T>(); 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 异步分页查询数据集合 /// <summary> /// 异步分页查询数据集合 /// 作者: 逍遥帝君 /// 2016-10-26 /// </summary> /// <typeparam name="T">实体</typeparam> /// <param name="connection">连接字符串</param> /// <param name="cmd">sql语句</param> /// <param name="param">参数</param> /// <param name="flag">true存储过程,false sql语句</param> /// <returns>t</returns> public async Task<IList<T>> FindToListByPageAsync<T>(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<T> tlist = new List<T>(); 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<Person>(connection, "page_getperson", param);
//总条数 var count = param.Get<int>("_pagecount"); var kk = result;