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 天前
开箱即用的 GoWind Admin|风行,企业级前后端一体中后台框架:基于 GORM 从零实现新服务
后端·go·orm
喵个咪6 天前
开箱即用的 GoWind Admin|风行,企业级前后端一体中后台框架:基于 Ent 从零实现新服务
后端·go·orm
哈库纳玛塔塔6 天前
MongoDB 数据库 ORM/ODM 新工具
java·数据库·spring boot·mongodb·orm
乔伊酱9 天前
Bean Searcher 遇“鬼”记:为何我的查询条件偷偷跑进了 HAVING?
java·前端·orm
Anthony_492625 天前
【踩坑】gorm 回写主键不正确
mysql·go·orm
修行者Java1 个月前
JPA 的说明和使用
orm·jpa
安冬的码畜日常1 个月前
【JUnit实战3_31】第十九章:基于 JUnit 5 + Hibernate + Spring 的数据库单元测试
spring·单元测试·jdbc·hibernate·orm·junit5
_pass_1 个月前
flask 框架的ORM 学习及应用
学习·flask·orm
123461611 个月前
互联网大厂Java面试:从Spring Boot到微服务的探索
java·数据库·spring boot·微服务·面试·mybatis·orm
qqxhb2 个月前
系统架构设计师备考第55天——数据库设计融合&物联网层次架构&案例分析
数据库·物联网·系统架构·orm·网络层·感知层·平台应用层