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

相关推荐
濮水大叔13 小时前
如何基于动态关系进行ORM关联查询,并动态推断DTO?
typescript·node.js·orm
濮水大叔3 天前
Prisma不能优雅的支持DTO,试试Vona ORM吧
前端框架·node.js·orm
f0rb7 天前
Java ORM 代码量和性能对比:DoytoQuery vs SpringDataJPA/SpringJdbc/jOOQ/MyBatis-plus
后端·orm
小王子10249 天前
Django模型关系:从一对多到多对多全解析
数据库·mysql·django·orm
sq80010 天前
EFCore ORM 数据库数据表迁移
数据库·后端·orm
小刀飘逸19 天前
typeorm部署问题
node.js·orm
Code季风21 天前
Go 语言开发中用户密码加密存储的最佳实践
go·orm
Code季风22 天前
掌握 GORM 删除:单条删除、批量删除与软删除实践
sql·go·orm