轻量级ORM框架——第一篇:Dapper快速学习

  我们都知道ORM全称叫做Object Relationship Mapper,也就是可以用object来map我们的db,而且市面上的orm框架有很多,其中有一个框架

叫做dapper,而且被称为the king of ORM。

 

一:为什么选择Dapper

1. 性能优越:

    其实在各大网站上,我们大概都会看到这样的一个对比效果图,在超过500次poco serialization的过程中所表现的性能,我们发现dapper是第二名,

当然第一名谁也无法超越,越底层的当然久越快,同时也就越麻烦。就好像谁能超过“01代码”呢???

 

2. 支持多数据库

      支持多数据库的本质是因为Dapper是对IDBConnection接口进行了方法扩展,比如你看到的SqlMapper.cs,一旦你这样做了,我们也知道,

SqlConnection,MysqlConnection,OracleConnection都是继承于DBConnection,而DBConnection又是实现了IDBConnection的接口,对吧。。。

 

二:安装Dapper

    install dapper的方式通常有两种:

 

1. 通过nuget进行安装

   如果你不知道怎么用nuget进行安装,或者不知道install-package是什么,可以在browser上找一下,比如下面这样:

 

然后我们copy到package console 试试看。

 

 

2. 在github上获取源码。

   为什么要获取源码,是因为用ilspy调试dapper的源码太费劲了,毕竟现在都是异步编程了,从ilspy中看都是匿名方法很多都无法渗透,废话不多

说,我们只要把Dapper文件夹拉出来然后copy到我们的solution就可以了,如下图:

 

三:快速CURD操作

   其实对数据库的操作莫过于CURD,在进行操作之前我们再配一个Users表。

 

1. 配置Users表

CREATE TABLE [dbo].[Users](
    [UserID] [int] IDENTITY(1,1) NOT NULL,
    [UserName] [varchar](50) NULL,
    [Email] [varchar](100) NULL,
    [Address] [varchar](100) NULL,
 CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED 
(
    [UserID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

 

2. Insert操作

  通常来说,有两种insert操作:

<1>单条insert操作

  这是一个简单的参数化insert,而且还可以塞入匿名类型,对吧,跟原始的SqlParameter相比,是不是简单的多???

     static void Main(string[] args)
        {
            IDbConnection connection = new SqlConnection("Data Source=.;Initial Catalog=DataMip;Integrated Security=True;MultipleActiveResultSets=True");

            var result = connection.Execute("Insert into Users values (@UserName, @Email, @Address)",
                                   new { UserName = "jack", Email = "380234234@qq.com", Address = "上海" });
        }

 

<2> InsertBulk操作

     既然是Bulk操作,那肯定就是批量插入了,我们要做的就是将上面这个 ”匿名对象" 变成 ”匿名对象集合“ 就可以了。。。为了方便操作,这里定义

一个Users类,比如下面这样。。。

    static void Main(string[] args)
        {
            IDbConnection connection = new SqlConnection("Data Source=.;Initial Catalog=DataMip;Integrated Security=True;MultipleActiveResultSets=True");

            //var result = connection.Execute("Insert into Users values (@UserName, @Email, @Address)",
            //                       new { UserName = "jack", Email = "380234234@qq.com", Address = "上海" });

            var usersList = Enumerable.Range(0, 10).Select(i => new Users()
            {
                Email = i + "qq.com",
                Address = "安徽",
                UserName = i + "jack"
            });

            var result = connection.Execute("Insert into Users values (@UserName, @Email, @Address)",usersList);
        }

 

2. Query操作

   其实在Dapper在query上提供的的文章太多了。。。这篇我们就按照最简单的参数化查询就好了。。。比如我要找到username=jack的记录,如下:

1         static void Main(string[] args)
2         {
3             IDbConnection connection = new SqlConnection("Data Source=.;Initial Catalog=DataMip;Integrated Security=True;MultipleActiveResultSets=True");
4 
5             var query = connection.Query<Users>("select * from Users where UserName=@UserName", new { UserName = "jack" });
6 
7         }

 

图上的亮点就在于能够自动化mapper到我们object上面来,这是我们DataReader所不能办到的,对吧~~

 

3.update操作

  这种操作方式,我们还是使用Execute方法来实现,和insert是一种套路的哦。

  

 

4. delete操作

    这里我还是采用参数化的形式来删除UserID=10这条记录,方式如下:

 

最终sql的table展示如下,可以看到已经正确的修改了UserID=11的记录,删除了UserID=10的record。。。。当然Dapper好玩的地方多着呢,

这篇只是一个入门而已。。。希望本篇对大家有帮助~~~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
namespace StopWatch { public partial class Form2 : Form { DateTime examtime; DateTime nowtime; DateTime t = DateTime.Now; int add = 0; public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { timer1.Enabled = false; examtime = new DateTime(1, 1, 1, 0,0, 0); labTime.Text = "0" + examtime.ToLongTimeString(); if (t.Hour < 10) labT.Text = "0" + DateTime.Now.ToLongTimeString(); else labT.Text = DateTime.Now.ToLongTimeString(); } private void button2_Click(object sender, EventArgs e) { if (add < 1 ) { starttime(); } else { MessageBox.Show("记录已满,请清除记录后再开始记录!"); } } private void button1_Click(object sender, EventArgs e) { if (timer1.Enabled != false) { Add(); } if (add >= 5) { MessageBox.Show("对不起!最多只能记录4条!!"); timer1.Enabled = false; } } private void button3_Click(object sender, EventArgs e) { ClearTime(); add = 0 - 1; Add(); stoptime(); } private void button4_Click(object sender, EventArgs e) { Application.Exit(); } private void timer1_Tick(object sender, EventArgs e) { examtime = examtime.AddSeconds(1); if (examtime.Hour < 10) labTime.Text = "0" + examtime.ToLongTimeString(); else labTime.Text = examtime.ToLongTimeString(); } private void timer2_Tick(object sender, EventArgs e) { if(t.Hour<10) labT.Text = "0"+DateTime.Now.ToLongTimeString(); else labT.Text = DateTime.Now.ToLongTimeString();nowtime = nowtime.AddSeconds(1); } private void Add() { add = add + 1; switch (add) { case 1: textBox1.Text = "0" + examtime.ToLongTimeString(); timer1.Enabled = true; break; case 2: textBox2.Text = "0" + examtime.ToLongTimeString(); timer1.Enabled = true; break; case 3: textBox3.Text = "0" + examtime.ToLongTimeString(); timer1.Enabled = true; break; case 4: textBox4.Text = "0" + examtime.ToLongTimeString(); Program.hash.add(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text); timer1.Enabled = true; timer1.Enabled = false; break; case 5: default: break; } } private void ClearTime() { examtime = new DateTime(1, 1, 1, 0, 0, 0); labTime.Text = "0" + examtime.ToLongTimeString(); textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); textBox4.Clear(); textBox1.Text = "0" + examtime.ToLongTimeString(); textBox2.Text = "0" + examtime.ToLongTimeString(); textBox3.Text = "0" + examtime.ToLongTimeString(); textBox4.Text = "0" + examtime.ToLongTimeString(); timer1.Enabled = false; } private void starttime() { timer1.Enabled = true; } private void stoptime() { timer1.Enabled = false; } private void textBox1_TextChanged(object sender, EventArgs e) { } private void textBox2_TextChanged(object sender, EventArgs e) { } private void textBox3_TextChanged(object sender, EventArgs e) { } private void textBox4_TextChanged(object sender, EventArgs e) { } private void pictureBox1_Click(object sender, EventArgs e) { } private void textBox5_TextChanged(object sender, EventArgs e) { } private void labTime_Click(object sender, EventArgs e) { } private void timeshow_Click(object sender, EventArgs e) { Form3 frm = new Form3(); frm.Show(); } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值