.netCore 引用第三方ORM中间件-Dapper
十年河东,十年河西,莫欺少年穷
学无止境,精益求精
可恶的NetCore对DataTable支持的不好,这对于喜欢直接写SQL的童鞋来说非常难受,因此,在NetCore项目中有必要引入优秀的第三方数据库中间件,目前市场上比较优秀的第三方EF框架有很多,例如:Sqlsugar、Dapper、FreeSql 等,当然,微软也有自己的一套EF框架,EntityFrm 和 EfCore,就效率而言,第三方的框架要完胜微软的EF框架,真不知道微软是干啥吃的、
今天,我们使用Dapper这个轻量级的第三方EF框架。
1.安装Dapper
这里直接使用Nuget安装。
安装完成后,我们需要对Dapper进行扩展,如下:
using Microsoft.Extensions.Configuration; using System; using System.Data; using System.Data.SqlClient; using WuAnCommon; namespace WuAnSqlService { public class DapperHelper { /// 获取连接字符串 private static string Connection= ConfigCommon.Get("WuAnDBContext"); /// 返回连接实例 private static IDbConnection dbConnection = null; /// 静态变量保存类的实例 private static DapperHelper uniqueInstance; /// 定义一个标识确保线程同步 private static readonly object locker = new object(); /// <summary> /// 私有构造方法,使外界不能创建该类的实例,以便实现单例模式 /// </summary> private DapperHelper() { // 这里为了方便演示直接写的字符串,实例项目中可以将连接字符串放在配置文件中,再进行读取。 } /// <summary> /// 获取实例,这里为单例模式,保证只存在一个实例 /// </summary> /// <returns></returns> public static DapperHelper GetInstance() { // 双重锁定实现单例模式,在外层加个判空条件主要是为了减少加锁、释放锁的不必要的损耗 if (uniqueInstance == null) { lock (locker) { if (uniqueInstance == null) { uniqueInstance = new DapperHelper(); } } } return uniqueInstance; } /// <summary> /// 创建数据库连接对象并打开链接 /// </summary> /// <returns></returns> public static IDbConnection OpenCurrentDbConnection() { if (dbConnection == null) { dbConnection = new SqlConnection(Connection); } //判断连接状态 if (dbConnection.State == ConnectionState.Closed) { dbConnection.Open(); } return dbConnection; } } }
上述代码完成Dapper对数据库的连接,下面还需要Dapper操作数据库的公共类,如下:
public static class DapperDbContext { // 获取开启数据库的连接 private static IDbConnection Db { get { //创建单一实例 DapperHelper.GetInstance(); return DapperHelper.OpenCurrentDbConnection(); } } /// <summary> /// 查出一条记录的实体 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <returns></returns> public static T QueryFirstOrDefault<T>(string sql, object param = null) { return Db.QueryFirstOrDefault<T>(sql, param); } public static Task<T> QueryFirstOrDefaultAsync<T>(string sql, object param = null) { return Db.QueryFirstOrDefaultAsync<T>(sql, param); } /// <summary> /// 查出多条记录的实体泛型集合 /// </summary> /// <typeparam name="T">泛型T</typeparam> /// <returns></returns> public static IEnumerable<T> Query<T>(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null) { return Db.Query<T>(sql, param, transaction, buffered, commandTimeout, commandType); } public static Task<IEnumerable<T>> QueryAsync<T>(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { return Db.QueryAsync<T>(sql, param, transaction, commandTimeout, commandType); } public static int Execute(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { return Db.Execute(sql, param, transaction, commandTimeout, commandType); } public static Task<int> ExecuteAsync(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { return Db.ExecuteAsync(sql, param, transaction, commandTimeout, commandType); } public static T ExecuteScalar<T>(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { return Db.ExecuteScalar<T>(sql, param, transaction, commandTimeout, commandType); } public static Task<T> ExecuteScalarAsync<T>(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { return Db.ExecuteScalarAsync<T>(sql, param, transaction, commandTimeout, commandType); } /// <summary> /// 同时查询多张表数据(高级查询) /// "select *from K_City;select *from K_Area"; /// </summary> /// <param name="sql"></param> /// <returns></returns> public static SqlMapper.GridReader QueryMultiple(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { return Db.QueryMultiple(sql, param, transaction, commandTimeout, commandType); } public static Task<SqlMapper.GridReader> QueryMultipleAsync(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { return Db.QueryMultipleAsync(sql, param, transaction, commandTimeout, commandType); } }
有了上述代码,我们就可以写数据库方法了
示例代码:
public BaseResponse<List<WuAnTestModel>> GetDataDapper() { try { return CommonBaseResponse.SetResponse<List<WuAnTestModel>>(DapperDbContext.Query<WuAnTestModel>("select DeviceNo,DeviceSecret from Yk_Device").ToList(),true); } catch (Exception ex) { throw ExceptionHelper.GetAggregateException("WuAnService.WuAnTestService", "GetDataDapper", "", ex); } }
当然,我这个扩展类比较简单,如果需要更复杂的扩展类,可以进行百度,毕竟大家都是面向百度编程。
@天才卧龙的博客