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

相关推荐
鱼听禅6 天前
C#学习笔记-Entity Framework Core基础操作学习
c#·orm·ef core
jump_jump7 天前
Kysely 源码解剖:类型安全 SQL 如何走到 AST
node.js·orm·源码阅读
咏方舟【长江支流】9 天前
【开源】跨语言·跨平台·跨数据库(4) ——用宝框架 ORM 数据源适配器
数据库·开源·ai编程·orm·咏方舟-长江支流·用宝框架·用宝框架orm
咏方舟【长江支流】9 天前
【开源】跨语言·跨平台·跨数据库(3) —— 为用宝框架增加ORM,让AI执行
数据库·人工智能·开源·ai编程·orm·咏方舟-长江支流·用宝框架
hubro16 天前
.NET热门ORM真实的性能测试总结
测试·orm·性能化化
zzzzzz3101 个月前
LMCache 深度解析:LLM 推理加速的秘密武器,TTFT 降低 13 倍是怎么做到的?
pytorch·机器学习·orm
Jabes.yang1 个月前
互联网大厂Java求职面试实战解析(含技术场景与详解)
spring boot·微服务·面试·orm·技术栈·java se·jakarta ee
落木萧萧8252 个月前
MyBatisGX 批量操作:比 MyBatis-Plus 和 MyBatis-Flex 更好用、更快
mybatis·orm
喵个咪2 个月前
GoWind Toolkit Go后端代码生成 完整全流程实战
后端·go·orm
落木萧萧8252 个月前
为什么我把 MyBatisGX 设计成现在这样
mybatis·orm