




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、 Dapper.NET簡介及其使用方法框架組之FX架構(gòu)團(tuán)隊(duì)2014.11第11頁 共11頁Dapper.NET簡介及其使用方法文檔修訂記錄序號版本日期撰寫審定說明1V1.02014-11-04 2V2.02014-12-16 目錄一、DAPPER.NET簡介4二、為什么選擇使用DAPPER.NET4三、如何使用DAPPER.NET43.1、Query()方法的使用53.1.1、Query泛型方法的使用、返回的結(jié)果映射到強(qiáng)類型列表/單個(gè)對象、返回結(jié)果的各行映射到多種對象(Multi Mapping)53.1.2、Query非泛型方法的使用63.2、QueryMul
2、tiple()方法的使用63.3、Execute()方法的使用73.3.1、單條記錄的增改刪73.3.2、批量增改刪83.4、ExecuteScalar方法的使用103.5、調(diào)用存儲過程10四、更多參考資料11古大集團(tuán)- Dapper.NET簡介及其使用方法一、Dapper.NET簡介Dapper.NET是個(gè)開源的輕型ORM。它擴(kuò)展了IDbConnection接口的功能,所以只要某類實(shí)現(xiàn)IDbConnection接口,那么該類對象就能調(diào)用到Dapper.NET中的IDbConnection擴(kuò)展方法。目前支持的平臺有:.NET Framework 3.5、.NET Framework 4.0和.
3、NET Framework 4.5。我們提供的Demo,所支持的平臺是.NET Framework 4.0。二、為什么選擇使用Dapper.NET1、它語法十分簡單,易學(xué)易用,并且無須遷就數(shù)據(jù)庫的設(shè)計(jì)。2、它能和所有.NET ado提供商一起工作,如:SqLite、sqlce、firebird、Oracle, MySQL、PostgreSQL 以及SQL Server等。3、原理通過Emit反射IDataReader的序列隊(duì)列,來快速地得到和產(chǎn)生對象。4、能用很少的代碼就能實(shí)現(xiàn)批量查詢(通過使用QueryMultiple方法實(shí)現(xiàn))以及批量增、刪、改。5、它的運(yùn)行速度十分快,接近于IDataRe
4、ader。如下兩表顯示的數(shù)據(jù)(數(shù)據(jù)由官網(wǎng)提供)體現(xiàn)了它的性能優(yōu)勢。表1、Performance of SELECT mapping over 500 iterations - POCO serialization方法運(yùn)行時(shí)間(單位:毫秒)Hand coded (using a SqlDataReader )47Dapper ExecuteMapperQuery49ServiceStack.OrmLite (QueryById)50PetaPoco52BLToolkit80SubSonic CodingHorror107NHibernate SQL104Linq 2 SQL ExecuteQue
5、ry181Entity Framework ExecuteStoreQuery631表2、Performance of SELECT mapping over 500 iterations - dynamic serialization方法運(yùn)行時(shí)間(單位:毫秒)Dapper ExecuteMapperQuery (dynamic)48Massive52Simple.Data95三、如何使用Dapper.NET本節(jié)貼出的代碼都是關(guān)于Dapper的最最基本的用法。若想深入學(xué)習(xí),請參考官方網(wǎng)站。首先,在您需要用到Dapper.NET的項(xiàng)目中引用Dapper.dll,請見圖1。然后在需要使用Dappe
6、r.NET的源代碼文件中加上【using Dapper;】。圖1、引用Dapper.dll3.1、Query()方法的使用3.1.1、Query泛型方法的使用、返回的結(jié)果映射到強(qiáng)類型列表/單個(gè)對象例1、返回的結(jié)果映射到強(qiáng)類型列表:public IEnumerable<DapperDemoEntity> GetDapperDemoList() using (IDbConnection connection = Common.OpenConnection() const string query = "SELECT ID AS DapperDemoID, Nam
7、e AS DapperDemoName, ModifiedDate FROM dbo.DapperDemo WHERE ParentID > 0" return connection.Query<DapperDemoEntity>(query); 例2、返回的結(jié)果映射到單個(gè)對象:public DapperDemoEntity GetDapperDemo(int dapperDemoId) using (IDbConnection connection = Common.OpenConnection() const string query ="SELECT
8、 ID AS DapperDemoID, Name AS DapperDemoName, ModifiedDate FROM dbo.DapperDemoWHERE ParentID > 0 AND ID = DapperDemoID" / DapperDemoID will be mapped to the param DapperDemoID: return connection.Query<DapperDemoEntity>(query, new DapperDemoID = dapperDemoId ).SingleOrDefault(); 、
9、返回結(jié)果的各行映射到多種對象(Multi Mapping)Dapper也有能力返回具有內(nèi)嵌對象成員的類對象。此時(shí)必須要給splitOn參數(shù)(splitOn參數(shù)的意思是從哪個(gè)列起開始讀取第二個(gè)對象)傳值,但有種情況:當(dāng)對象分割列為Id或id時(shí),可省略splitOn參數(shù)。例3、public IEnumerable<DapperDemoEntity> GetDapperDemoWithParentList() using (IDbConnection connection = Common.OpenConnection() const string query ="SELECT
10、 child.ID AS DapperDemoID, child.Name AS DapperDemoName, child.ModifiedDate, parent.ID AS DapperDemoParentID, parent.Name AS DapperDemoParentNameFROM dbo.DapperDemo child LEFT JOIN dbo.DapperDemo parent ON parent.ID = child.ParentIDWHERE parent.ID IS NOT NULL" return connection.Query<DapperD
11、emoEntity, DapperDemoParentEntity, DapperDemoEntity>(query , (child, parent) => child.ParentDapperDemo = parent; return child; , splitOn: "DapperDemoParentID"); 3.1.2、Query非泛型方法的使用此種方式的使用不需要定義實(shí)體類。例4、返回的結(jié)果映射到單個(gè)動態(tài)對象:public dynamic GetDapperDemoDynamic(int dapperDemoId) using (IDbConnec
12、tion connection = Common.OpenConnection() const string query ="SELECT ID AS DapperDemoID, Name AS DapperDemoName, ModifiedDate FROM dbo.DapperDemoWHERE ParentID > 0 AND ID = DapperDemoID" return connection.Query(query, new DapperDemoID = dapperDemoId ).SingleOrDefault(); 3.2、QueryMultip
13、le()方法的使用通過QueryMultiple()方法的使用,使得您能在單個(gè)查詢中處理多個(gè)結(jié)果Dapper.SqlMapper.GridReader,即通過QueryMultiple()方法的使用,可實(shí)現(xiàn)批量查詢。例5、public IList<DapperDemoEntity> GetUseQueryMultiple() List<DapperDemoParentEntity> parentDapperDemoList = null; List<DapperDemoEntity> dapperDemoList = null; using (IDbConn
14、ection connection = Common.OpenConnection() const string query ="SELECT ID AS DapperDemoParentID, Name AS DapperDemoParentName, ModifiedDate FROM dbo.DapperDemo WHERE ParentID < 1;SELECT child.ID AS DapperDemoID, child.Name AS DapperDemoName, child.ModifiedDate, parent.ID AS DapperDemoParent
15、ID, parent.Name AS DapperDemoParentNameFROM dbo.DapperDemo child LEFT JOIN dbo.DapperDemo parent ON parent.ID = child.ParentIDWHERE parent.ID IS NOT NULL" using (SqlMapper.GridReader grid = connection.QueryMultiple(query) parentDapperDemoList = grid.Read<DapperDemoParentEntity>().ToList()
16、; dapperDemoList = grid.Read<DapperDemoEntity, DapperDemoParentEntity, DapperDemoEntity>(child, parent) => child.ParentDapperDemo = parent; return child; , splitOn: "DapperDemoParentID").ToList<DapperDemoEntity>(); return dapperDemoList; 3.3、Execute()方法的使用通過Execute()方法的使用,可分
17、別實(shí)現(xiàn)單條記錄的增改刪操作和批量記錄的增改刪操作。該方法返回的是受影響的行數(shù)。3.3.1、單條記錄的增改刪例6、新增單條記錄:public int InsertDapperDemo(DapperDemoEntity dapperDemo) using (IDbConnection connection = Common.OpenConnection() const string sql = "INSERT INTO dbo.DapperDemo(ParentID, Name, ModifiedDate)VALUES (DapperDemoParentID, DapperDemoNam
18、e, ModifiedDate);SELECT CAST(SCOPE_IDENTITY() AS INT)" int dapperDemoID = connection.Query<int>(sql, dapperDemo).Single(); return dapperDemoID; 例7、更新單條記錄:public int UpdateDapperDemo(DapperDemoEntity dapperDemo) using (IDbConnection connection = Common.OpenConnection() const string sql =&q
19、uot;UPDATE dbo.DapperDemo SET ParentID = DapperDemoParentID, Name = DapperDemoName, ModifiedDate = ModifiedDate WHERE ID = DapperDemoID" return connection.Execute(sql, dapperDemo); 例8、刪除單條記錄:public int DeleteDapperDemo(DapperDemoEntity dapperDemo) using (IDbConnection connection = Common.OpenCo
20、nnection() const string sql = "DELETE FROM dbo.DapperDemo WHERE ParentID > 0 AND ID = DapperDemoID" return connection.Execute(sql, dapperDemo); 3.3.2、批量增改刪有兩種寫法可以實(shí)現(xiàn)批量新增、修改和刪除操作。第1種方法:請分別看如下3例:例9、新增多條記錄:public int InsertDapperDemoList(IList<DapperDemoEntity> list) using (IDbConnect
21、ion connection = Common.OpenConnection() const string sql = "INSERT INTO dbo.DapperDemo(ParentID, Name, ModifiedDate) VALUES (DapperDemoParentID, DapperDemoName, ModifiedDate)" int rowsAffectd = connection.Execute(sql, list); return rowsAffectd; 例10、修改多條記錄:public int UpdateDapperDemoList(I
22、List<DapperDemoEntity> list) using (IDbConnection connection = Common.OpenConnection() const string sql ="UPDATE dbo.DapperDemo SET ParentID = DapperDemoParentID, Name = DapperDemoName, ModifiedDate = ModifiedDate WHERE ID = DapperDemoID" int rowsAffectd = connection.Execute(sql, lis
23、t); return rowsAffectd; 例11、刪除多條記錄:public int DeleteDapperDemoList(IList<DapperDemoEntity> list) using (IDbConnection connection = Common.OpenConnection() const string sql = "DELETE FROM dbo.DapperDemo WHERE ParentID > 0 AND ID = DapperDemoID" return connection.Execute(sql, list);
24、 第2種方法:例12、public int DeleteDapperDemoList(DapperDemoEntity dapperDemo) using (IDbConnection connection = Common.OpenConnection() const string deleteChildSQL = "DELETE FROM dbo.DapperDemo WHERE ParentID > 0 AND ParentID = DapperDemoParentID" const string deleteParentSQL = "DELETE F
25、ROM dbo.DapperDemo WHERE ParentID < 1 AND ID = DapperDemoParentID" IDbTransaction transaction = connection.BeginTransaction(); int rowsAffected = connection.Execute(deleteChildSQL, new DapperDemoParentID = dapperDemo.DapperDemoParentID , transaction); rowsAffected += connection.Execute(delet
26、eParentSQL, new DapperDemoParentID = dapperDemo.DapperDemoParentID , transaction); transaction.Commit(); return rowsAffected; 3.4、ExecuteScalar方法的使用例13、ExecuteScalar泛型方法的使用:public T UseGenericExecuteScalar<T>(string dbType) using (IDbConnection connection = Common.OpenConnection() T scalar = c
27、onnection.ExecuteScalar<T>("SELECT CAST(1 AS " + dbType + ")"); return scalar; 例14、ExecuteScalar非泛型方法的使用:public int GetChildDapperDemoCount() using (IDbConnection connection = Common.OpenConnection() const string sql = "SELECT COUNT(1) FROM dbo.DapperDemo WHERE ParentID > 0" return (int)connection.ExecuteScalar(sql); 3.5、調(diào)用存儲過程請注意以下例子的底色為藍(lán)色的部分。例15、如何調(diào)用用到輸出參數(shù)和有返回值的存儲過程:public dynamic ProcedureWithOutAndReturnParameter() int successCode = -1; s
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年汽車行業(yè)供應(yīng)鏈韌性評估與風(fēng)險(xiǎn)管理在汽車行業(yè)供應(yīng)鏈追溯中的應(yīng)用報(bào)告
- it面試題庫及答案6
- 人工智能教育應(yīng)用新領(lǐng)域:2025年K2教育中人工智能個(gè)性化學(xué)習(xí)系統(tǒng)效果報(bào)告
- 2025年康復(fù)醫(yī)療服務(wù)體系康復(fù)醫(yī)療政策法規(guī)解讀報(bào)告
- hetian書記員考試試題及答案
- flash上機(jī)考試試題及答案
- 2025年金融科技在財(cái)富管理領(lǐng)域的深度應(yīng)用趨勢分析報(bào)告
- Z世代消費(fèi)心理對汽車新消費(fèi)品牌的影響報(bào)告
- 2025年電商平臺供應(yīng)鏈金融創(chuàng)新與風(fēng)險(xiǎn)管理的信用風(fēng)險(xiǎn)預(yù)警報(bào)告
- cdfi醫(yī)師技師考試試題及答案
- 2024年江蘇省鎮(zhèn)江市潤州區(qū)中考第二次中考生物模擬試卷
- 《揚(yáng)州慢》教學(xué)課件
- 國寶大熊貓的資料介紹三年級8篇
- 2024年貴州省貴陽市南明區(qū)中考一??荚囄锢碓囶}
- 電子產(chǎn)品出廠檢驗(yàn)報(bào)告
- 《施工現(xiàn)場消防》課件
- 某地區(qū)地質(zhì)災(zāi)害-崩塌勘查報(bào)告
- 2024年新高考適應(yīng)性考試俄語試題含答案
- 非法營運(yùn)培訓(xùn)課件
- 《海拉EPS傳感器》課件
- 子宮頸癌護(hù)理查房課件
評論
0/150
提交評論