Dapper 是一個輕量級ORM框架,在項目中如果對性能比較看中,Dapper是一個不錯的選擇。接下來我們就來看看如何在項目中使用Dapper.
1.安裝Dapper
這里直接使用Nuget安裝。

安裝完成之后,發現Nuget下已經有了Dapper。

2.創建DapperHelper
接下來創建一個DapperHelper幫助類,來進行讀取數據庫連接字符串,打開數據庫等操作。

代碼:
public class DapperHelper
{
/// 數據庫連接名
private static string _connection = string.Empty;
/// 獲取連接名
private static string Connection
{
get { return _connection; }
//set { _connection = value; }
}
/// 返回連接實例
private static IDbConnection dbConnection = null;
/// 靜態變量保存類的實例
private static DapperHelper uniqueInstance;
/// 定義一個標識確保線程同步
private static readonly object locker = new object();
/// <summary>
/// 私有構造方法,使外界不能創建該類的實例,以便實現單例模式
/// </summary>
private DapperHelper()
{
// 這里為了方便演示直接寫的字符串,實例項目中可以將連接字符串放在配置文件中,再進行讀取。
_connection = @"server=.;uid=sa;pwd=sasasa;database=Dapper";
}
/// <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;
}
}
3.創建上下文 DbContext 作用是調用Dapper中操作數據庫的方法,里面是我自己封的一些常用方法,當然了這一步可以略去,直接調用也行,這里只是提供一種思路。
public static class DbContext
{
// 獲取開啟數據庫的連接
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);
}
}
接下來就可以調用了,DbContext.Action()

