bltoolkit mysql_.NET 轻量级 ORM 框架 - Dapper 介绍
轉(zhuǎn)自:https://blog.csdn.net/hanjun0612/article/details/52170204
Dapper簡(jiǎn)單介紹:
Dapper is a?single file?you can drop in to your project that will extend your?IDbConnection?interface.
Dapper是一個(gè)輕型的開(kāi)源ORM類(lèi),代碼就一個(gè)SqlMapper.cs文件,編譯后就40多K的一個(gè)很小的Dll. 官方資料:點(diǎn)擊這里
Dapper支持Mysql,SqlLite,Mssql2000,Mssql2005,Oracle等一系列的數(shù)據(jù)庫(kù),當(dāng)然如果你知道原理也可以讓它支持Mongo db
Dapper的r支持多表并聯(lián)的對(duì)象。支持一對(duì)多 多對(duì)多的關(guān)系。并且沒(méi)侵入性,想用就用,不想用就不用。無(wú)XML無(wú)屬性。代碼以前怎么寫(xiě)現(xiàn)在還怎么寫(xiě)。
Dapper原理通過(guò)Emit反射IDataReader的序列隊(duì)列,來(lái)快速的得到和產(chǎn)生對(duì)象。性能提升了很多;(比采用常規(guī)的反射)
Dapper支持net2.0,3.0,3.5,4.0。不過(guò)就是要配置下。如果不知道如何配置查看我博客里的在2.0下使用3.5就可以了。
語(yǔ)法十分簡(jiǎn)單。并且無(wú)須遷就數(shù)據(jù)庫(kù)的設(shè)計(jì)。
Dapper執(zhí)行效率:
30W條數(shù)據(jù),取其中的一個(gè)對(duì)象,和第一頁(yè)前15條數(shù)據(jù),耗時(shí)0.0906879秒。這個(gè)速度超過(guò)Datable。
官方的測(cè)試代碼以及數(shù)據(jù)
Performance of?SELECT?mapping over 500 iterations - POCO serialization
Method
Duration
Remarks
Hand coded (using a?SqlDataReader)
47ms
Dapper?ExecuteMapperQuery
49ms
PetaPoco
52ms
Can be faster
BLToolkit
80ms
SubSonic?CodingHorror
107ms
NHibernate SQL
104ms
Linq 2 SQL?ExecuteQuery
181ms
Entity framework?ExecuteStoreQuery
631ms
Performance of?SELECT?mapping over 500 iterations - dynamic serialization
Method
Duration
Remarks
Dapper?ExecuteMapperQuery (dynamic)
48ms
Massive
52ms
Simple.Data
95ms
Performance of?SELECT?mapping over 500 iterations - typical usage
Method
Duration
Remarks
Linq 2 SQL?CompiledQuery
81ms
Not super typical involves complex code
NHibernate HQL
118ms
Linq 2 SQL
559ms
Entity framework
859ms
SubSonic?ActiveRecord.SingleOrDefault
3619ms
Dapper使用介紹:
如果你使用的是vs2012,可以使用NuGet來(lái)進(jìn)行安裝,會(huì)自動(dòng)添加引用,使用時(shí)寫(xiě)入命名空間即可;
using Dapper;
下面的代碼可以作為使用參考:
public?static?readonly?string?sqlconnectionString ="Data Source=xxx;Initial Catalog=Express;User ID=sa;Password=123";
public?static?readonly?string?mysqlconnectionString =@"server=xxx;database=dddd;uid=xxx;pwd=123;charset='gbk'";
public?static?SqlConnection SqlConnection()
{
var connection =new?SqlConnection(sqlconnectionString);
connection.Open();
return?connection;
}
public?static??MySqlConnection? MySqlConnection()
{
var connection =new?MySqlConnection(mysqlconnectionString);
connection.Open();
return?connection;
}
調(diào)用方法
SqlConnection connection = Program.SqlConnection();
獲得一個(gè)實(shí)體對(duì)象
var d = connection.Query("select * from dog where id = 1",null).Single();
獲得實(shí)體對(duì)象結(jié)合
var dd = connection.Query("select * from dog where id < 10", null).ToList();
插入數(shù)據(jù)
//動(dòng)態(tài)參數(shù)
connection.Execute("INSERT INTO dog (Age,Name,Weight) VALUES (@age,@name,@Weight)",
new { @age = i,@name = Guid.NewGuid().ToString(), @Weight = i });
//直接傳入實(shí)體
connection.Execute("INSERT INTO dog (Age,Name,Weight) VALUES (@age,@name,@Weight)",model);
Execute a query and map the results to a strongly typed List
Note: all extension methods assume the connection is already open, they will fail if the connection is closed.
public static IEnumerable Query(this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null, bool buffered = true)
Example usage:
publicclassDog
{
publicint?Age{get;set;}
publicGuidId{get;set;}
publicstringName{get;set;}
publicfloat?Weight{get;set;}
publicintIgnoredProperty{get{return1;}}
}
var guid =Guid.NewGuid();
var dog = connection.Query("select Age = @Age, Id = @Id",new{Age=(int?)null,Id= guid });
dog.Count()
.IsEqualTo(1);
dog.First().Age
.IsNull();
dog.First().Id
.IsEqualTo(guid);
Execute a query and map it to a list of dynamic objects
public static IEnumerable Query (this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null, bool buffered = true)
This method will execute SQL and return a dynamic list.
Example usage:
var rows = connection.Query("select 1 A, 2 B union all select 3, 4");
((int)rows[0].A)
.IsEqualTo(1);
((int)rows[0].B)
.IsEqualTo(2);
((int)rows[1].A)
.IsEqualTo(3);
((int)rows[1].B)
.IsEqualTo(4);
Execute a Command that returns no results
public static int Execute(this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null)
Example usage:
connection.Execute(@"
set nocount on
create table #t(i int)
set nocount off
insert #t
select @a a union all select @b
set nocount on
drop table #t",new{a=1, b=2})
.IsEqualTo(2);
Execute a Command multiple times
The same signature also allows you to conveniently and efficiently execute a command multiple times (for example to bulk-load data)
Example usage:
connection.Execute(@"insert MyTable(colA, colB) values (@a, @b)",
new[]{new{ a=1, b=1},new{ a=2, b=2},new{ a=3, b=3}}
).IsEqualTo(3);// 3 rows inserted: "1,1", "2,2" and "3,3"
This works for any parameter that implements IEnumerable for some T.
var dd = connection.Query("select * from dog where id < 10",null).ToList();
總結(jié)
以上是生活随笔為你收集整理的bltoolkit mysql_.NET 轻量级 ORM 框架 - Dapper 介绍的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java sql 参数_java jd
- 下一篇: 我26岁,鼻子下方,下巴、额头,脸颊都在