以前四篇介紹了一個國內開發者開發的優秀框架SqlSugar,給咱們眼前一亮的感受。這一篇,咱們將試試另外一個出鏡率比較高的ORM框架-Dapper。sql
Dapper是一個輕量級的ORM框架,其以高速、簡單易用爲特色。在某些時候,效率甚至能夠與ADO.NET 媲美。那麼,吹得天花亂墜,就讓咱們實際看看它的表現吧。數據庫
照例,先建立一個項目:DapperDemo緩存
dotnet new console --name DapperDemo
而後切換到目錄裏:app
cd DapperDemo
添加包支持:框架
dotnet add package Dapper
若是不出意外的話,目前項目中已經安裝好了Dapper。如今就讓咱們開始愉快的使用吧。ide
首先,須要注意的一點是,與其餘的ORM框架不一樣的是,Dapper須要咱們手動建立一個IConnection。Dapper的全部操做都是依託於IConnection來操做,並且Dapper將其支持的方法封裝成了IConnection的擴展方法。測試
因此,在使用以前咱們須要先建立一個IConnection。爲了方便演示,我把以前SqlSugar演示用過的測試數據庫拿過來了,是一個SQLite,因此咱們須要先安裝一個SQLite的驅動:優化
dotnet add package Microsoft.Data.SQLite
在Program.cs中引入兩個包:this
using Microsoft.Data.Sqlite;
using Dapper;
在Main方法裏建立一個IConnection:spa
using(var connection = new SqliteConnection("Data Source=./demo.db"))
{
}
Dapper的查詢至關簡單:
var result = connection.Query("select * from Persion");
傳入一個SQL語句,返回一個可枚舉對象。若是不指定類型,將返回類型爲dynamic的列表。咱們來看一下Query方法的相關聲明:
public static IEnumerable<dynamic> Query(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null);
public static IEnumerable<T> Query<T>(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null);
public static IEnumerable<TReturn> Query<TReturn>(this IDbConnection cnn, string sql, Type[] types, Func<object[], TReturn> map, object param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null, CommandType? commandType = null);
咱們就以最經常使用的三個爲例,給大夥分析一下參數以及調用方式:
cnn 一個數據庫鏈接,因此Dapper不負責管理數據庫鏈接,這部分由咱們手動管理
sql 傳入的SQL語句,Dapper以IDbConnection爲基礎,以SQL爲執行命令,因此必須咱們來傳入SQL語句
param 一個能夠爲Null的Object類型,表示SQL的參數化,Dapper對參數化作了一些優化,在SQL的參數化裏,參數名映射到了object的屬性上。
transaction 表示是否有IConnection級別的事務,默認爲null,傳入後該指令會被事務包含
buffered 緩存
commandTimeout 命令執行是否超時以及超時時間
commandType 表示命令模式 有 Text 普通模式,StoredProcedure 存儲過程 ,TableDirect 表查詢
splitOn 默認狀況下以Id 做爲兩個對象之間的區分
Dapper在數據查詢方面不只支持集合做爲查詢結果,還能夠獲取單個數據。
一共有四種方式獲取單數據:
public static T QueryFirst<T>(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);
public static T QueryFirstOrDefault<T>(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);
QueryFirst 表示獲取第一條查詢結果,若是沒有結果,則會拋出一個異常。
QueryFirstOrDefault 與QueryFirst同樣,但不一樣的是,若是沒有則不會拋出異常,而是直接返回一個該類型的默認值,數值類型的默認值爲(0),引用類型的默認值爲Null。
public static T QuerySingle<T>(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);
public static T QuerySingleOrDefault<T>(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);
QuerySingle也能查詢單條數據做爲結果,但與QueryFirst不一樣的是QuerySingle查詢時,若是數據存在多行將會拋出異常,若是不想要異常則可使用QuerySingleOrDefault做爲查詢方法。
這個另一種查詢方式,對於SQL語句來講,沒有明顯的限制,因此咱們有時候能夠傳入多個查詢SQL語句進去,而後分別獲取來自各個表的查詢數據:
string sql = "SELECT * FROM Invoice WHERE InvoiceID = @InvoiceID; SELECT * FROM InvoiceItem WHERE InvoiceID = @InvoiceID;";
using (var connection = My.ConnectionFactory())
{
connection.Open();
using (var multi = connection.QueryMultiple(sql, new {InvoiceID = 1}))
{
var invoice = multi.Read<Invoice>().First();
var invoiceItems = multi.Read<InvoiceItem>().ToList();
}
}
看一下它的基本參數和方法聲明:
public static GridReader QueryMultiple(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);
這個方法返回一個GridReader,經過Read方法獲取須要的數據。
Dapper固然不僅有查詢這一項功能,Dapper支持使用存儲過程、insert、update、delete等其餘的SQL語句進行操做數據庫。使用方式:
string sql = "Invoice_Insert";
using (var connection = My.ConnectionFactory())
{
var affectedRows = connection.Execute(sql,
new {Kind = InvoiceKind.WebInvoice, Code = "Single_Insert_1"},
commandType: CommandType.StoredProcedure);
var affectedRows2 = connection.Execute(sql,
new[]
{
new {Kind = InvoiceKind.WebInvoice, Code = "Many_Insert_1"},
new {Kind = InvoiceKind.WebInvoice, Code = "Many_Insert_2"},
new {Kind = InvoiceKind.StoreInvoice, Code = "Many_Insert_3"}
},
commandType: CommandType.StoredProcedure
);
}
示例就是使用存儲過程的例子。
string sql = "INSERT INTO Customers (CustomerName) Values (@CustomerName);";
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
var affectedRows = connection.Execute(sql, new {CustomerName = "Mark"});
Console.WriteLine(affectedRows);
var customer = connection.Query<Customer>("Select * FROM CUSTOMERS WHERE CustomerName = 'Mark'").ToList();
FiddleHelper.WriteTable(customer);
}
//== 屢次插入
string sql = "INSERT INTO Customers (CustomerName) Values (@CustomerName);";
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
connection.Open();
var affectedRows = connection.Execute(sql,
new[]
{
new {CustomerName = "John"},
new {CustomerName = "Andy"},
new {CustomerName = "Allan"}
}
);
這是執行插入的示例。
string sql = "UPDATE Categories SET Description = @Description WHERE CategoryID = @CategoryID;";
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
var affectedRows = connection.Execute(sql,new {CategoryID = 1, Description = "Soft drinks, coffees, teas, beers, mixed drinks, and ales"});
Console.WriteLine(affectedRows);
}
更新。
string sql = "DELETE FROM Customers WHERE CustomerID = @CustomerID";
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
var affectedRows = connection.Execute(sql, new {CustomerID = 1});
Console.WriteLine(affectedRows);
}
刪除。
Execute沒什麼好說的,基本就是執行SQL語句的形式完成增刪改爲等操做。
值得注意的是:
public static IDataReader ExecuteReader(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);
返回一個IDataReader實例,這個實例能夠給DataTable填充數據,使用方法以下:
DataTable table = new DataTable();
table.Load(reader);
以及:
public static T ExecuteScalar<T>(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);
這個方法是返回查詢結果的第一行第一列的元素。
若是單說Dapper的話,並無太多好說的。不過Dapper是真的快,在實際開發中有時候會用Dapper做爲EF Core的一個補充。
固然了,Dapper還有不少其餘的插件,使用那些插件能夠爲Dappe帶來非通常的提高。咱們下一篇將介紹一下Dapper的插件。