根据SQL自动解析生成动态API。
- 使用模板语法生成API控制器:scriban/scriban
- API注释
- 基于数据表配置CURD
- 多个数据库连接配置
- 命名空间隔离(由于不支持命名空间定义,使用子类完成)
- 全局配置
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<SqlApis Name="Students" Comment="学生(增删改查等处理)">
<SqlApi Name="GetStudentsByCount" SqlTpl="SELECT TOP (@Count) *
FROM [dbo].[Students]" Comment="获取前N条学生数据">
<Input>
<!--会根据参数名称自动合并字段配置-->
<Parameter Name="Count" DefaultValue="2" Comment="获取记录数"></Parameter>
</Input>
</SqlApi>
<SqlApi Name="GetStudentById">
<SqlTpl>
SELECT TOP 1 * FROM [dbo].[Students]
WHERE [Id] = @Id
</SqlTpl>
<Output>
<Parameter Name="Name" DefaultValue="2" Comment="名称"></Parameter>
</Output>
</SqlApi>
<SqlApi Name="Insert" HttpRoute='[HttpPost("")]'>
<SqlTpl>
INSERT INTO [dbo].[Students]
([Name],[IdCard],[StudentCode],[Phone],[Nation],[Guardian],[GuardianPhone],[Address])
VALUES
(@Name,@IdCard,@StudentCode,@Phone,@Nation,@Guardian,@GuardianPhone,@Address)
</SqlTpl>
<Input>
<Parameter Name="Name" Comment="名称"></Parameter>
</Input>
</SqlApi>
<SqlApi Name="GetStudents" Comment="分页查询">
<SqlTpl>
select top (select (@PageSize)) *
from (select row_number() over(order by id) as rownumber,*
from [dbo].[Students]) temp_row
where rownumber>(@SkipCount)
</SqlTpl>
</SqlApi>
</SqlApis>
<SqlApis Name="Classes">
<SqlApi Name="GetAll" HttpRoute='[HttpGet("")]'>
<SqlTpl>
SELECT TOP (1000) *
FROM [dbo].[Classes]
</SqlTpl>
</SqlApi>
</SqlApis>
<SqlApis Name="Iot">
<SqlApi Name="GetAll" ConnectionString="Server=(localdb)\MSSQLLocalDB;Database=eschool-iot;Trusted_Connection=True;MultipleActiveResultSets=true" HttpRoute='[HttpGet("")]'>
<SqlTpl>
SELECT TOP (@PageSize) *
FROM [dbo].[Devices]
</SqlTpl>
</SqlApi>
</SqlApis>
</configuration>
名称 | Nuget |
---|---|
Magicodes.DynamicSqlApi.Core | |
Magicodes.DynamicSqlApi.All | |
Magicodes.DynamicSqlApi.CsScript | |
Magicodes.DynamicSqlApi.Dapper | |
Magicodes.DynamicSqlApi.SqlServer |
以下内容均已有思路,但是缺乏精力,因此虚席待PR,有兴趣的朋友可以参与进来,多多交流。
见上图:
- API分组
- API权限控制
- API文档以及注释
- 导入导出
- HTTP状态码
- 日志
- 数据验证
- 数组支持
- 引用Nuget包"Magicodes.DynamicSqlApi.All"
名称 | 说明 | Nuget |
---|---|---|
Magicodes.DynamicSqlApi.All | Magicodes.DynamicSqlApi 默认实现 |
- 添加配置文件“sqlMapper.xml”
配置文件默认为“sqlMapper.xml”,配置参考下文:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<SqlApis Name="AuditLogs">
<SqlApi Name="GetAbpAuditLogsList" SqlTpl="SELECT TOP (@Count) *
FROM [dbo].[AbpAuditLogs]">
<Input>
<!--会根据参数名称自动合并字段配置-->
<Parameter Name="Count" DefaultValue="2"></Parameter>
</Input>
</SqlApi>
<SqlApi Name="GetAbpAuditLogById">
<SqlTpl>
SELECT TOP 1 * FROM [dbo].[AbpAuditLogs]
WHERE [Id] = @Id
</SqlTpl>
</SqlApi>
</SqlApis>
<SqlApis Name="Users">
<SqlApi Name="GetAbpUsers">
<SqlTpl>
SELECT TOP (1000) *
FROM [dbo].[AbpUsers]
</SqlTpl>
</SqlApi>
</SqlApis>
</configuration>
如上述配置所示,仅需配置SQL语句即可,参数和结果列表全由Magicodes.DynamicSqlApi自动解析生成。Name是必须的。
- 配置ASP.NET Core工程
添加配置:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
//根据环境变量加载不同的JSON配置
config.AddJsonFile("appsettings.json", true, true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json",
true, true);
//从环境变量添加配置
config.AddEnvironmentVariables();
config.AddXmlFile("sqlMapper.xml", true, false);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
启用DynamicSqlApi:
public void ConfigureServices(IServiceCollection services)
{
services.AddAllDynamicSqlApi(Configuration["ConnectionStrings:Default"]);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseDynamicSqlApi();
}
关注“麦扣聊技术”订阅号可以获得最新文章、教程、文档:
-
编程交流群<85318032>
-
产品交流群<897857351>