.NET 轻量级 ORM 框架 - Dapper 介绍
Dapper简单介绍:
Dapper is a single file you can drop in to your project that will extend your IDbConnection interface.
Dapper是一个轻型的开源ORM类,代码就一个SqlMapper.cs文件,编译后就40多K的一个很小的Dll. 官方资料:点击这里
Dapper支持Mysql,SqlLite,Mssql2000,Mssql2005,Oracle等一系列的数据库,当然如果你知道原理也可以让它支持Mongo db
Dapper的r支持多表并联的对象。支持一对多 多对多的关系。并且没侵入性,想用就用,不想用就不用。无XML无属性。代码以前怎么写现在还怎么写。
Dapper原理通过Emit反射IDataReader的序列队列,来快速的得到和产生对象。性能提升了很多;(比采用常规的反射)
Dapper支持net2.0,3.0,3.5,4.0。不过就是要配置下。如果不知道如何配置查看我博客里的在2.0下使用3.5就可以了。
语法十分简单。并且无须迁就数据库的设计。
Dapper执行效率:
30W条数据,取其中的一个对象,和第一页前15条数据,耗时0.0906879秒。这个速度超过Datable。
官方的测试代码以及数据
Performance of SELECT mapping over 500 iterations - POCO serialization
| Method | Duration | Remarks |
| Hand coded (using a SqlDataReader) | 47ms | |
| Dapper ExecuteMapperQuery<Post> | 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来进行安装,会自动添加引用,使用时写入命名空间即可;
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; |
}
调用方法
SqlConnection connection = Program.SqlConnection();
获得一个实体对象
var d = connection.Query<Dog>("select * from dog where id = 1",null).Single<Dog>();
获得实体对象结合
var dd = connection.Query<Dog>("select * from dog where id < 10", null).ToList<Dog>();
插入数据
//动态参数
connection.Execute("INSERT INTO dog (Age,Name,Weight) VALUES (@age,@name,@Weight)",new { @age = i,@name = Guid.NewGuid().ToString(), @Weight = i });//直接传入实体
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<T> Query<T>(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<Dog>("select Age = @Age, Id = @Id",new{Age=(int?)null,Id= guid }); dog.Count()
.IsEqualTo(); dog.First().Age
.IsNull(); dog.First().Id
.IsEqualTo(guid); Execute a query and map it to a list of dynamic objects public static IEnumerable<dynamic> 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[].A)
.IsEqualTo(); ((int)rows[].B)
.IsEqualTo(); ((int)rows[].A)
.IsEqualTo(); ((int)rows[].B)
.IsEqualTo(); 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=, b=})
.IsEqualTo(); 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=, b=},new{ a=, b=},new{ a=, b=}}
).IsEqualTo();// 3 rows inserted: "1,1", "2,2" and "3,3" This works for any parameter that implements IEnumerable<T> for some T.
var dd = connection.Query<Dog>("select * from dog where id < 10", null).ToList<Dog>();
.NET 轻量级 ORM 框架 - Dapper 介绍的更多相关文章
- .NET轻量级ORM框架Dapper入门精通
一.课程介绍 本次分享课程包含两个部分<.NET轻量级ORM框架Dapper修炼手册>和<.NET轻量级ORM框架Dapper葵花宝典>,阿笨将带领大家一起领略轻量级ORM框架 ...
- 轻量级ORM框架Dapper应用一:Dapper安装
一.Dapper简介 Dapper是一款轻量级ORM框架,为解决网站访问流量极高而产生的性能问题而构造,主要通过执行TSQL表达式而实现数据库的CQRS. 如果你在项目中遇到性能访问问题,选择Dapp ...
- 基于轻量级ORM框架Dapper的扩展说明
这里简单的介绍一下本人基于Dapper作的一些简单的扩展,供大家参考. 为何要使用这款框架,相信大家看到下面排名就清楚了 其实在各大网站上,我们大概都会看到这样的一个对比效果图,在超过500次poco ...
- 轻量级ORM框架 Dapper快速学习
好在有师兄师姐一起带着做,所以开始没那么困难,但是由于大学涉猎范围有限,往往有很尴尬的时候,不懂构造方法重载,去“请教”,本来以为师兄会帮忙写好,结果“我念,你来写”,被深深的激励了一把,后来就早出晚 ...
- 轻量级ORM框架Dapper应用八:使用Dapper实现DTO
一.什么是DTO 先来看看百度百科的解释: 数据传输对象(DTO)(Data Transfer Object),是一种设计模式之间传输数据的软件应用系统.数据传输目标往往是数据访问对象从数据库中检索数 ...
- 轻量级ORM框架Dapper应用六:Dapper支持存储过程
在Entity Framework中讲解了EF如何支持存储过程,同样,Dapper也支持存储过程,只需要在Query()方法的CommandType中标记使用的是存储过程就可以了.在Users表上面创 ...
- 轻量级ORM框架Dapper应用五:使用Dapper实现Join操作
在这篇文章中,讲解如何使用Dapper使用Inner join的操作 1.新创建两张表:Users表和Product表 Users表定义如下: CREATE TABLE [dbo].[Users]( ...
- 轻量级ORM框架Dapper应用四:使用Dapper返回多个结果集
使用Dapper的QueryMultiple方法可以一次执行多条SQL语句,返回多个结果集,代码如下 using System; using System.Collections.Generic; u ...
- 轻量级ORM框架Dapper应用三:使用Dapper实现In操作
IN 操作符允许我们在 WHERE 子句中规定多个值. 本篇文章中,还是使用和上篇文章中同样的实体类和数据库,Dapper使用in操作符的代码如下: using System; using Syste ...
随机推荐
- C#_delegate - 异步调用实例 BeginInvoke EndInvoke event
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- linux 常识笔记 20160621
Linux分四部分 Linux内核 GNU工具组建 图形化桌面环境 应用软件 Linux系统的核心是内核,内核控制着计算机系统上的所有硬件和软件:必要时分配硬件,有需要时执行软件. 内核负责四项主要功 ...
- 第二次作业第2题_JH
2.每人自己建立一个HelloWorld项目,练习使用git的add/commit/push/pull/fetch/clone等基本命令.比较项目的新旧版本的差别. (1)创建一个HelloWorld ...
- eclipse创建项目时出现appcompat_v7包及解决办法
Android开发学习总结(三)--appcompat_v7项目说明 一.appcompat_v7项目说明 今天来说一下appcompat_v7项目的问题,使用eclipse创建Android项目时, ...
- typedef的使用3——使用经过typedef定义的函数构成的函数数组
#include <stdio.h> #include <string.h>//不加还能跑,加上反而跑不了了...笑哭 #pragma warning(disable:4996 ...
- mkfs.xfs命令没找到
yum install xfsprogs xfsdump
- [记录] web icon 字体
weloveiconfonts 在http://codepen.io/cguillou/pen/jmkfK 中看css发现既然有这样的,yes!
- sharepoint 删除list里的所有内容
[System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") $siteUrl = " ...
- web框架--来自维基百科
- SQL SERVER 与ACCESS、EXCEL的数据导入导出转换
* 说明:复制表(只复制结构,源表名:a 新表名:b) select * into b from a where 1<>1 * 说明:拷贝表(拷贝数据,源表名:a 目标表名:b) ...