Dapper应用

1.Dapper是什么

Dapper是一款轻量级ORM工具。如果你在小的项目中,使用Entity Framework、NHibernate 来处理大数据访问及关系映射,未免有点杀鸡用牛刀。你又觉得ORM省时省力,这时Dapper 将是你不二的选择。

2.为什么使用

  1. 轻量,编译完成之后只有120k(好象是变胖了)
  2. 速度快。Dapper的速度接近与IDataReader,取列表的数据超过了DataTable。
  3. 支持多种数据库。Dapper可以在所有Ado.net Providers下工作,包括sqlite, sqlce, firebird, oracle, MySQL, PostgreSQL and SQL Server
  4. 可以映射一对一,一对多,多对多等多种关系。
  5. 性能高。通过Emit反射IDataReader的序列队列,来快速的得到和产生对象,性能不错。
  6. 支持FrameWork2.0,3.0,3.5,4.0,4.5

3.使用Dapper.Net并演示

1. 使用Sqlserver创建测试表

2.创建winform应用程序,引用Dapper封装基础应用和框架

3.创建简单页面实现CRUD

4.开始实现

4.1创建表

CREATE DATABASE test
USE test
GO
CREATE TABLE Test
(
testId INT PRIMARY KEY IDENTITY,
testName VARCHAR(50) NULL
) INSERT INTO Test VALUES ('CallmeYhz') INSERT INTO Test VALUES('周公瑾')

4.2实体

  public class Test
{
public int testId { get; set; }
public string testName { get; set; }
}

4.3 Nuget包

4.4 封装搭建

该类实现接口

    public interface IDaoBase<T, TKey> where T : class
{
int Count(IPredicate predicate, bool buffered = false);
bool Delete(IPredicate predicate);
bool DeleteByID(TKey key);
bool DeleteByIDList(Expression<Func<T, object>> expression, IEnumerable<TKey> idList);
int Execute(string sql, dynamic param = null);
bool Exists(string sql, dynamic param = null, bool buffered = false);
IEnumerable<TReturn> Get<TFirst, TSecond, TReturn>(string sql, Func<TFirst, TSecond, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null);
IList<T> GetAll();
T GetByID(TKey key);
IEnumerable<T> GetByIDList(Expression<Func<T, object>> expression, IEnumerable<TKey> idList, bool buffered = false);
T GetEntity(IPredicate predicate, bool buffered = false);
T GetEntity(string sql, object param, bool buffered = false);
PagedList<T> GetEntityPageList(SearchBase search);
IList<T> GetList(IPredicate predicate = null, IList<ISort> sort = null, bool buffered = false);
IList<T> GetList(string sql, object param = null, bool buffered = true);
IList<TEntity> GetList<TEntity>(string sql, object param = null, bool buffered = true);
SqlMapper.GridReader GetMultiple(string sql, dynamic param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);
TScale GetScale<TScale>(string sql, dynamic param = null, bool buffered = false);
dynamic Insert(T entity);
void InsertBatch(IEnumerable<T> entityList, IDbTransaction transaction = null);
bool Update(T entity);
void UpdateBatch(IEnumerable<T> entityList);
}

数据连接工厂

  public class ConnectionFactory
{
/// <summary>
/// 数据库连接
/// </summary>
public static string ConnectionString { set; get; } /// <summary>
/// 数据库类型
/// </summary>
public static DatabaseType DataBaseType { set; get; } /// <summary>
/// 初始化
/// </summary>
/// <param name="conectionString">连接串</param>
/// <param name="dataBaseType">数据库类型</param>
public static void Init(string conectionString, DatabaseType dataBaseType = DatabaseType.SqlServer)
{
ConnectionFactory.ConnectionString = conectionString;
ConnectionFactory.DataBaseType = dataBaseType;
} /// <summary>
/// 创建数据库连接
/// </summary>
/// <returns></returns>
public static IDbConnection CreateConnection()
{
IDbConnection connection = null; switch (ConnectionFactory.DataBaseType)
{
case DatabaseType.SqlServer:
connection = new SqlConnection(ConnectionFactory.ConnectionString);
break; } return connection;
}
}
 public class TestDao : DaoBase<Test,int>
{
/// <summary>
/// 获取所有数据
/// </summary>
/// <returns></returns>
public IList<Test> GetAllList()
{
string sql = @"
SELECT*FROM Test";
return this.GetList<Test>(sql);
} /// <summary>
/// 根据名称获取实体
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public Test GetByName(string name)
{
string sql = @"
SELECT*FROM Test WHERE testName=@testName";
return this.GetEntity(sql, new { testName = name });
} }

构造一个简单Winform界面

配置数据库连接字符串并且初始化

<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<!-- 测试 -->
<add name="db" connectionString=" Data Source=.;Initial Catalog=test;Integrated Security=SSPI; " /> </connectionStrings>
</configuration>
     //初始化数据库连接
ConnectionFactory.Init(ConfigurationManager.ConnectionStrings["db"].ConnectionString);

4.5演示

Dapper.Net 应用的更多相关文章

  1. Dapper逆天入门~强类型,动态类型,多映射,多返回值,增删改查+存储过程+事物案例演示

    Dapper的牛逼就不扯蛋了,答应群友做个入门Demo的,现有园友需要,那么公开分享一下: 完整Demo:http://pan.baidu.com/s/1i3TcEzj 注 意 事 项:http:// ...

  2. Dapper扩展之~~~Dapper.Contrib

    平台之大势何人能挡? 带着你的Net飞奔吧!http://www.cnblogs.com/dunitian/p/4822808.html#skill 上一篇文章:Dapper逆天入门~强类型,动态类型 ...

  3. 由Dapper QueryMultiple 返回数据的问题得出==》Dapper QueryMultiple并不会帮我们识别多个返回值的顺序

    异常汇总:http://www.cnblogs.com/dunitian/p/4523006.html#dapper 今天帮群友整理Dapper基础教程的时候手脚快了点,然后遇到了一个小问题,Dapp ...

  4. Dapper.Contrib:GetAsync<T> only supports an entity with a [Key] or an [ExplicitKey] property

    异常处理:http://www.cnblogs.com/dunitian/p/4523006.html#dapper 原来Model是这样滴 修改后是这样滴 注意点:Model里面的Table和Key ...

  5. Dapper where Id in的解决方案

    简单记一下,一会出去有点事情~ 我们一般写sql都是==>update NoteInfo set NDataStatus=@NDataStatus where NId in (@NIds) Da ...

  6. ASP.NET Core 1.0 使用 Dapper 操作 MySql(包含事务)

    操作 MySql 数据库使用MySql.Data程序包(MySql 开发,其他第三方可能会有些问题). project.json 代码: { "version": "1. ...

  7. Asp.Net Core + Dapper + Repository 模式 + TDD 学习笔记

    0x00 前言 之前一直使用的是 EF ,做了一个简单的小项目后发现 EF 的表现并不是很好,就比如联表查询,因为现在的 EF Core 也没有啥好用的分析工具,所以也不知道该怎么写 Linq 生成出 ...

  8. 搭建一套自己实用的.net架构(3)续 【ORM Dapper+DapperExtensions+Lambda】

    前言 继之前发的帖子[ORM-Dapper+DapperExtensions],对Dapper的扩展代码也进行了改进,同时加入Dapper 对Lambda表达式的支持. 由于之前缺乏对Lambda的知 ...

  9. mono for android中使用dapper或petapoco对sqlite进行数据操作

    在mono for android中使用dapper或petapoco,很简单,新建android 类库项目,直接把原来的文件复制过来,对Connection连接报错部分进行注释和修改就可以运行了.( ...

  10. Dapper:The member of type SeoTKD cannot be used as a parameter Value

    异常汇总:http://www.cnblogs.com/dunitian/p/4523006.html#dapper 上次说了一下Dapper的扩展Dapper.Contrib http://www. ...

随机推荐

  1. ubuntu下安装lrzsz

    secureCRT中可以使用rz和sz命令上传和下载文件,可是这要linux中安装了lrzsz才可以.我用的时候无法使用apt-get自动安装,下面介绍手动安装的方法. 1 下载lrzsz软件  ht ...

  2. 将Apache手动安装成Windows的服务

    将Apache手动安装成Windows的服务 可以选择在安装Apache时自动将其安装为一个服务.如果选择"for all users",那么Apache将会被安装为服务. 如果选 ...

  3. 浅谈Service层为何要有接口

    被人随意问了一句,为何每个service层都要写一个接口呢,多麻烦~虽然想说点什么,但是又不知道从何说起,只好从新整理一下思绪. 情景1:在开源框架中有很多这种情况,就是某个功能支持用户自定义扩展.说 ...

  4. springmvc 上传下载

    springmvc文件上传下载在网上搜索的代码 参考整理了一份需要使用的jar.commons-fileupload.jar与commons-io-1.4.jar 二个文件 1.表单属性为: enct ...

  5. 洛谷P2964 [USACO09NOV]硬币的游戏A Coin Game

    题目描述 Farmer John's cows like to play coin games so FJ has invented with a new two-player coin game c ...

  6. [译]React Context

    欢迎各位指导与讨论 : ) 前言 由于笔者英语和技术水平有限,有不足的地方恳请各位指出.我会及时修正的 O(∩_∩)O 当前React版本 15.0.1 时间 2016/4/25 正文 React一个 ...

  7. SharePoint 2013技巧分享系列 - 同步Exchange显示高清用户照片

    在“SharePoint 2013技巧分享系列 - Active Directory同步显示用户照片”文中介绍了如何同步Active Directory显示用户照片,但是同步完成后,用户照片尺寸和清晰 ...

  8. Python-09-线程、进程、协程、异步IO

    0. 什么是线程(thread)? 线程,有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元.一个标准的线程由线程ID,当前指令指针(PC),寄存器集合和堆 ...

  9. Swift来的正是时候

    早期对ObjectiveC这玩意不是很感冒,一直没有动手搞Apple平台下的开发,现在Swift来了,时机成熟,提升门槛后的IOS,才是量子本人想弄的.现在不用担心搞ObjectiveC的走在前面了. ...

  10. 如何在个人博客引擎 Hexo 中添加 Swiftype 搜索组件

    在您现在看到的我的博客站点,后台使用的是 Hexo 作为博客引擎,但是默认集成的搜索组件是进行 form 提交到 Google 进行搜索的,为了更好地体验,本文介绍如何在 Hexo 博客中集成 Swi ...