sql:

SELECT *
FROM users;
SELECT *
FROM posts;
SELECT *
FROM Posts p
LEFT JOIN Users u ON u.Id= p.OwnerId
ORDER BY p.Id

结果集:

 
代码:
string sql =@"select * from Posts p
left join Users u on u.Id = p.OwnerId
Order by p.Id";
PostWithConstructor[] data = connection.Query<PostWithConstructor,UserWithConstructor,PostWithConstructor>(sql,(post, user)=>{ post.Owner= user;return post;}).ToArray();
解析:
Query的方法签名:
publicstaticIEnumerable<TReturn>Query<TFirst,TSecond,TReturn>(
#if CSHARP30
thisIDbConnection cnn,string sql,Func<TFirst,TSecond,TReturn>map,object param,IDbTransaction transaction, bool buffered,string splitOn,int? commandTimeout,CommandType? commandType
#else
thisIDbConnection cnn,string sql,Func<TFirst,TSecond,TReturn>map,object param =null,IDbTransaction transaction =null, bool buffered =true,string splitOn ="Id",int? commandTimeout =null,CommandType? commandType =null
#endif
)
IDbConnection的扩展方法,泛型:<TFirst,TSecond,TReturn>传入sql,Func委托:Func<TFirst,TSecond,TReturn> 实例代码中为:
  1. (post, user)=>{ post.Owner= user;return post;}
意思是,通过第一个和第二个类型得出返回类型对象。本例中TFirst-> PostWithConstructor, TSecond-> UserWithConstructor, TReturn-> PostWithConstructor
 
看一下结果:
注意下TFirst,TSecond的顺序,以及sql连表的顺序,换成右链接:
连表的顺序和传入的顺序非常重要;
这里注意一个问题:这个结果集的字段和实体的属性名称不相同,那是怎么映射的?如果有同名的属性怎么去处理?
我们注意到返回的结果集有重名字段,重名字段Id是对应不同的表的,用select * 这样可以把不同表的字段全部取出来。同时注意了,数据库表中的字段和实体Model里的属性名并不一致,其实他们是根据Model实体的构造函数进行区分的,我们可以看到这两个model都有构造函数:
 
dapper用结果集列名作区分,如何区分哪几列是一张表呢?dapper规定了分割字段,按分割的字段名进行对列的分割,默认的分割字段名为:Id,可以手动指定,基于这个原因就有了另外一个规定:分割字段必须是这个表的第一个字段。对于如下表设计就会报错。
dapper现在可以将结果集5列进行区分为两个表了,这时候对应哪个Model就要看传入TFirst,TSecond的顺序。注意一下PostWithConstructor的构造函数的owerid参数,这个参数在构造函数中并未使用,但是也标明了它,因为根据分割字段“Id"区分出给PostWithConstructor实体这三个:Id,OwnerId,[Content]字段。所以必须要在构造函数中有这三个字段的匹配。即:System.Int32 Id, System.Int32 OwnerId, System.String Content,不区分大小写。

Dapper使用方法:dapper-dot-net/Tests/Tests.cs解析(1)方法:TestMultiMapWithConstructor的更多相关文章

  1. .NET中常用的几种解析JSON方法

    一.基本概念 json是什么? JSON:JavaScript 对象表示法(JavaScript Object Notation). JSON 是一种轻量级的数据交换格式,是存储和交换文本信息的语法. ...

  2. 深入理解Java中的反射机制和使用原理!详细解析invoke方法的执行和使用

    反射的概念 反射: Refelection,反射是Java的特征之一,允许运行中的Java程序获取自身信息,并可以操作类或者对象的内部属性 通过反射,可以在运行时获得程序或者程序中的每一个类型的成员活 ...

  3. 开源且功能强大的C# 扩展方法类库Pure.Ext,包含1000+个拓展方法 (支持.Net Framework和.Net Core)

    先上地址 Github: https://github.com/purestackorg/pure.ext Gitee: https://gitee.com/purestack/pure.ext 扩展 ...

  4. Java基础-接口.编写2个接口:InterfaceA和InterfaceB;在接口InterfaceA中有个方法void printCapitalLetter();在接口InterfaceB中有个方法void printLowercaseLetter();然 后写一个类Print实现接口InterfaceA和InterfaceB,要求 方法 实现输出大写英文字母表的功能,printLowerca

    #34.编写2个接口:InterfaceA和InterfaceB:在接口InterfaceA中有个方法void printCapitalLetter():在接口InterfaceB中有个方法void ...

  5. 反射-优化及程序集等(用委托的方式调用需要反射调用的方法(或者属性、字段),而不去使用Invoke方法)

    反射-优化及程序集等(用委托的方式调用需要反射调用的方法(或者属性.字段),而不去使用Invoke方法)   创建Delegate (1).Delegate.CreateDelegate(Type, ...

  6. 用JQuery中的Ajax方法获取web service等后台程序中的方法

    用JQuery中的Ajax方法获取web service等后台程序中的方法 1.准备需要被前台html页面调用的web Service,这里我们就用ws来代替了,代码如下: using System; ...

  7. JQ绑定事件(1.9已经废除了live()等绑定事件方法,on()方法是官方推荐的绑定事件的一个方法)

    本文来源:http://www.cnblogs.com/leejersey/p/3545372.html jQuery on()方法是官方推荐的绑定事件的一个方法. $(selector).on(ev ...

  8. C# 解析js方法,并调用js方法

    本文转载:http://www.cnblogs.com/StudyLife/archive/2013/03/11/2953516.html 本文不是基于B/S的 后台调用前台js方法,而是给你一段js ...

  9. python面对对象编程------4:类基本的特殊方法__str__,__repr__,__hash__,__new__,__bool__,6大比较方法

    一:string相关:__str__(),__repr__(),__format__() str方法更面向人类阅读,print()使用的就是str repr方法更面对python,目标是希望生成一个放 ...

随机推荐

  1. linux 5 配置xmanager

    0 关闭防火墙或者打开177端口 iptables -A INPUT -p udp --dport 177 -j ACCEPT 1.vi /etc/inittab id:5:initdefault:  ...

  2. hdoj 5344 MZL's xor

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5344 #include<stdio.h> #include<cstring> ...

  3. Maven配置文件说明

    <projectxmlns="http://maven.apache.org/POM/4.0.0 "       xmlns:xsi="http://www.w3. ...

  4. UI:target-action设计模式、手势识别器

    ⼀.target/action设计模式 ⼆.代理设计模式 三.UIImageView 四.⼿势识别器 target/action设计模式 耦合是衡量⼀个程序写的好坏的标准之⼀, 耦合是衡量模块与模块之 ...

  5. Js 基本数据类型、引用数据类型

    数据类型 1.   ECMAScript变量包含两种不同类型的值:基本类型值.引用类型值: 2.   基本类型值:指的是保存在栈内存中的简单数据段: 3.   引用类型值:指的是那些保存在堆内存中的对 ...

  6. ARM&Linux 下驱动开发第一节(小试牛刀)

    #include<linux/init.h> #include<linux/module.h> static int __init hello_init(void) { pri ...

  7. Codeforces Round #340 (Div. 2) D. Polyline 水题

    D. Polyline 题目连接: http://www.codeforces.com/contest/617/problem/D Descriptionww.co There are three p ...

  8. Codeforces Round #260 (Div. 1) D. Serega and Fun 分块

    D. Serega and Fun Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/pro ...

  9. [Express] Level 1: First Step

    Installing Express Let's start building our new Express application by installing Express. Type the ...

  10. wampserver下打开phpMyAdmin出现403错误的问题解决方法

    图1 图2 wamp下打开phpMyAdmin出现403错误的问题解决方法安装完wamp后打开其下的phpMyAdmin也就是路径http://localhost/phpmyadmin/ 出现[图一] ...