ShadowSql之借Dapper打通ORM最后一公里

ShadowSql专职拼写sql,要想做为ORM就需要借高人之手

我们要借的就是Dapper,Dapper以高性能著称,ShadowSql搭配Dapper就是强强联手

为此本项目内置了一个子项目Dapper.Shadow就是Dapper扩展

以下是Dapper.Shadow的示例

一、配置Dapper执行器

复制代码
ISqlEngine engine = new SqliteEngine();
IDbConnection connection = new SqliteConnection("Data Source=file::memory:;Cache=Shared");
IExecutor executor = new DapperExecutor(engine, connection);

其中engine数据库(及方言)的配置对象,现在支持5种,分别是MsSql、MySql、Oracle、Postgres和Sqlite

实现ISqlEngine可以自定义数据库类型或者方言的支持

二、读取整张表

复制代码
var students = Executor.From("Students")
    .ToDapperSelect()
    .Get<Student>();

三、查询数据

1、SqlQuery查询数据

复制代码
        var students = Executor.From("Students")
            .ToSqlQuery()
            .Where("Age=10")
            .ToDapperSelect()
            .Get<Student>();
复制代码
        var students = Executor.From("Students")
            .ToSqlQuery()
            .ColumnValue("Age", 10)
            .ToDapperSelect()
            .Get<Student>();
复制代码
        var table = new StudentTable("Students");
        var students = table.ToSqlQuery()
            .Where(table.Age.EqualValue(10))
            .ToSelect()
            .Get<Student>(Executor);
复制代码
        var students = new StudentTable("Students")
            .ToSqlQuery()
            .Where(table => table.Age.EqualValue(10))
            .ToSelect()
            .Get<Student>(Executor);
复制代码
        var students = new Table("Students")
            .DefineColums("Age")
            .ToSqlQuery()
            .Where(student => student.Column("Age").EqualValue(10))
            .ToDapperSelect(Executor)
            .Get<Student>();

主要分以下三种

1.1 把执行器当数据库对象,这样查询就自带执行器,可以直接执行

1.2 执行时把执行器当参数传入

1.3 先查询,调用ToDapperSelect创建可执行对象

2、Query查询数据

复制代码
        var table = new StudentTable("Students");
        var students = table.ToQuery()
            .And(table.Age.EqualValue(10))
            .ToSelect()
            .Get<Student>(Executor);
复制代码
        var students = Executor.From("Students")
            .ToQuery()
            .And(table => table.Field("Age").EqualValue(10))
            .ToDapperSelect()
            .Get<Student>();
复制代码
        var table = new StudentTable("Students");
        var students = table.ToQuery()
            .And(table.Age.EqualValue(10))
            .ToDapperSelect(Executor)
            .Get<Student>();

查询方式多样,限与篇幅没法一一

以上示例邮件可以清晰显示ShadowSql和Dapper可以无缝对接

就这样实现了一个精简的高性能ORM,您也可以使用ShadowSql和Dapper来DIY属于自己的高性能ORM

相关推荐
余衫马12 天前
Mysql 5.7 与 SqlSugar 5.X 整合开发实战
mysql·c#·orm·sqlsugar
濮水大叔14 天前
Node.js 主流ORM框架动态分表方案大盘点
typescript·nodejs·orm·prisma
向上的车轮16 天前
Spring Boot生态中ORM对数据治理的支持有哪些?
spring boot·数据治理·orm
hayson23 天前
nebula graph orm框架 norm 用法解析 - 结构迁移、标签配置
orm·graphql
outsider_友人A1 个月前
前端也想写后端(2)ORM框架以及数据库关系和操作
node.js·orm·nestjs
Code季风1 个月前
GORM 一对多关联(Has Many)详解:从基础概念到实战应用
数据库·go·orm
白应穷奇1 个月前
Diesel异步编程: 深入理解Rust ORM的异步特性
rust·orm
xjm爱学习1 个月前
最强ORM让你开发效率提升百倍
java·后端·orm