Dapper-开源小型ORM
生活随笔
收集整理的這篇文章主要介紹了
Dapper-开源小型ORM
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?一些關于Dapper的介紹:
?1.Dapper是一個輕型的開源ORM類,代碼就一個SqlMapper.cs文件,編譯后就40多K的一個很小的Dll.?
?2.Dapper支持Mysql,SqlLite,Mssql2000,Mssql2005,Oracle等一系列的數據庫
?3.Dapper的r支持多表并聯的對象。支持一對多 多對多的關系。并且沒侵入性。
?4.Dapper原理通過Emit反射IDataReader的序列隊列,來快速的得到和產生對象。性能提升了很多;(比采用常規的反射)并且無須遷就數據庫的設計。
?Dapper源碼下載鏈接:
?點擊這里下載dapper源代碼。
?Demo:
var connection = GetOpenConnection();var guid = Guid.NewGuid();string id = "6e2a106d-d838-48b9-ac74-ad604457bba2";//1泛型var dog = connection.Query<Dog>("select * from Dog where Id = @Id", new { Id = id });//2 動態解析var rows = connection.Query("select * from Dog where Id = @Id", new { Id = id }).ToList();foreach (dynamic item in rows){String 黑客 = item.Name;} Query?
//3.執行不返回結果int result=connection.Execute("insert into Dog values(@age,@id,@name,@weight,@ignoredproperty)", new { age = 49, id = Guid.NewGuid(), name = "YZR", weight = 117.5, ignoredproperty = 1 }); Execute //4.批量ExecuteList<Dog> list = new List<Dog>();list.Add(new Dog() { Age = 9, Id = Guid.NewGuid(), Name = "ZXX", Weight = 120 });list.Add(new Dog() { Age = 9, Id = Guid.NewGuid(), Name = "WMJ", Weight = 120 });List<dynamic> paramsList = new List<dynamic>();foreach (Dog item in list){paramsList.Add(new { age = item.Age, id = item.Id, name = item.Name, weight = item.Weight, ignoredproperty = 1 });}int result = connection.Execute("insert into Dog values(@age,@id,@name,@weight,@ignoredproperty)", paramsList); 批量Execute //5.In操作dog = connection.Query<Dog>("select * from Dog where id in @ids", new { ids = new String[] { "6e2a106d-d838-48b9-ac74-ad604457bba3", "6e2a106d-d838-48b9-ac74-ad604457bba2", "535dab3a-d3c1-4cb0-b8f7-63351f491056" } });//等價于dog = connection.Query<Dog>("select * from Dog where id in (@id1,@id2,@id3)", new { id1 = "6e2a106d-d838-48b9-ac74-ad604457bba3", id2 = "6e2a106d-d838-48b9-ac74-ad604457bba2", id3 = "535dab3a-d3c1-4cb0-b8f7-63351f491056" }); In操作 //數據庫要建立主外鍵關系var sql =@"select p.*,u.* from Dog p left join Owner u on u.OwnerId = p.Id where p.id=@id Order by p.Id";Dog d = null;var t = connection.Query(sql, new { id = "6e2a106d-d838-48b9-ac74-ad604457bba2" });var data = connection.Query<Dog, Owner, Dog>(sql, (post, user) =>{if (d == null || d.Id != post.Id){d = post;}if (user != null){d.user.Add(user);}return post;}, new { id = "6e2a106d-d838-48b9-ac74-ad604457bba2" }); 一對多關系 //多查詢的結果集sql =@"select * from Dog where Id = @idselect * from Owner";using (var multi = connection.QueryMultiple(sql, new { id = "6e2a106d-d838-48b9-ac74-ad604457bba3" })){var data = multi.Read<Dog>().ToList();var owner = multi.Read<Owner>().ToList();} QueryMultiple //事務using (connection){//開始事務IDbTransaction transaction = connection.BeginTransaction();try{string query = "update Dog set Age=Age+1 where Id=@Id";string query2 = "update Dog set Weight=Weight+1.0 where Id=@Id";connection.Execute(query, new { Id = "71fff309-c7c9-4f64-b588-0a03e27459ba" }, transaction, null, null);connection.Execute(query2, new { Id = "71fff309-c7c9-4f64-b588-0a03e27459ba" }, transaction, null, null);//提交事務 transaction.Commit();}catch (Exception ex){//出現異常,事務Rollback transaction.Rollback();throw new Exception(ex.Message);}} IDbTransaction //存儲過程var p = new DynamicParameters();//實例1//p.Add("@result", dbType: DbType.Int32, direction: ParameterDirection.Output);//var user = connection.Query("Test", p, commandType: CommandType.StoredProcedure);//int totalCount = p.Get<int>("@result");//實例2//注意點:調用如果使用query,那么存儲過程需要有select結果集//p.Add("@result", dbType: DbType.Int32, direction: ParameterDirection.Output);//p.Add("@rowcount", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);//var user = connection.Query("TestValue", p, commandType: CommandType.StoredProcedure);//int re = p.Get<int>("@result");//int ro = p.Get<int>("@rowcount");//實例3//存儲過程分頁需要row_number() over( order by id)這個排序的id需要指定p.Add("@PageIndex", 1, dbType: DbType.Int32, direction: ParameterDirection.Input);p.Add("@PageSize", 3, dbType: DbType.Int32, direction: ParameterDirection.Input);p.Add("@TableName", "Dog", dbType: DbType.String, direction: ParameterDirection.Input);p.Add("@Where", " 1=1 order by id asc ", dbType: DbType.String, direction: ParameterDirection.Input);//p.Add("@rowcount", dbType: DbType.Int32, direction: ParameterDirection.Output);p.Add("@rowcount", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);var result = connection.Query("SelectBase", p, commandType: CommandType.StoredProcedure);int count = p.Get<int>("@rowcount"); Procedure //切換數據庫connection.ChangeDatabase("數據庫名稱"); ChangeDatabase?
Demo源代碼下載
點擊這里下載
?
?
轉載于:https://www.cnblogs.com/Francis-YZR/p/5528299.html
總結
以上是生活随笔為你收集整理的Dapper-开源小型ORM的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 51nod百度之星2016练习赛
- 下一篇: MYSQL----myownstars(