# DBApi **Repository Path**: modriclee/db-api ## Basic Information - **Project Name**: DBApi - **Description**: 零代码开发api服务,只需编写sql,就可以生成http api服务。支持api动态创建,多数据源连接,动态添加数据源,兼容多种数据库。 适用于BI报表、数据可视化大屏的后端接口快速开发 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: test - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 703 - **Created**: 2022-04-10 - **Last Updated**: 2023-07-06 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # DBApi ## 介绍 - 本项目基于[DBApi](https://gitee.com/freakchicken/db-api) 进行二次开发 - 快速生成数据库的http接口服务,零代码开发,只需编写sql,就可以生成http api服务。是数据库的上层应用,方便数据库数据对外发布http服务 ## 使用场景 - BI报表、数据可视化大屏的后端接口快速开发; - 前端程序员快速开发后端接口进行接口联调; - 企业数据资产对外快速发布http服务及统一管理 - 企业数据接口的统一管理中心 ## 特点 - 开箱即用,不需要编程,不需要依赖其他软件(只需要java运行环境) - 支持单机模式、集群模式;支持云原生容器化部署 - 支持API级别的访问权限控制,支持IP白名单、黑名单控制 - 支持所有类型数据库(JDBC连接方式),包括mysql/sqlserver/postgreSql/hive/oracle等等 - 支持动态sql,类似mybatis的动态sql,支持sql编辑、运行、调试 - 支持API结果缓存,支持缓存开启/关闭(通过插件实现) - 支持自定义代码逻辑的数据转换,比如数据脱敏(通过插件实现) - 支持API配置导入导出,方便测试环境到生产环境的API迁移 - 支持一个接口内多条SQL执行(例如分页功能) - 支持页面上的API请求测试 ## 视频教程 [查看视频教程](https://www.bilibili.com/video/BV1zL411G7Qh) ## 安装教程 - 请阅读 [安装教程](./dbapi-assembly/docs/deployment.md) ## 软件截图 ![](https://freakchicken.gitee.io/images/dbApi/20210904/api.png) ![](https://freakchicken.gitee.io/images/dbApi/20220313/datasource_add.png) ![](https://freakchicken.gitee.io/images/dbApi/20220313/api_add.png) ![](https://freakchicken.gitee.io/images/dbApi/20220313/api_add_high.png) ![](https://freakchicken.gitee.io/images/dbApi/20210803/sql_run.png) ![](https://freakchicken.gitee.io/images/dbApi/20210803/api_list.png) ![](https://freakchicken.gitee.io/images/dbApi/20210502/group.png) ![](https://freakchicken.gitee.io/images/dbApi/20210502/request.png) ![](https://freakchicken.gitee.io/images/dbApi/20210502/token_add.png) ![](https://freakchicken.gitee.io/images/dbApi/20210502/token.png) ![](https://freakchicken.gitee.io/images/dbApi/20210502/token_auth.png) ![](https://freakchicken.gitee.io/images/dbApi/20210502/docs.png) ![](https://freakchicken.gitee.io/images/dbApi/20210803/ip.png) ## 使用说明 请阅读 [详细使用说明](./dbapi-assembly/docs/instruction.md) ## 插件开发 - 请阅读 [插件开发指南](./dbapi-assembly/docs/plugin%20development.md) - 作者已经开发了字段加密插件和redis缓存插件,请阅读[案例demo](https://gitee.com/freakchicken/dbapi-plugin-demo) ## 二次开发 - 请阅读 [开发指南](./dbapi-assembly/docs/development.md) ## API接口 ### 1.登陆获取Token **描述**:获取用户的Token,后续请求需要此token **请求类型**:POST **请求URL**:/user/login **请求参数** | 名称 | 类型 | 是否必须 | 描述 | | :------- | :----- | :------: | :----- | | user | string | 是 | 用户名 | | password | string | 是 | 密码 | **返回参数** | 参数类型 | 描述 | | :------- | :---- | | string | token | **请求示例** curl -X POST http://127.0.0.1:8520/user/login -d "username=admin&password=admin" **返回示例** {"msg":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxIiwiZXhwIjoxNjUwNTIzMjYxLCJpYXQiOjE2NTA1MjE0NjF9.rVvmXvBYgQcHCU8vkwe7pPJaRyNl2OwhWpLKWorFafY","success":true,"data":null} ### 2.增加数据源 **描述**:增加数据源 **请求类型**:POST **请求URL**:/datasource/add **请求参数** | 名称 | 类型 | 是否必须 | 描述 | | :------------ | :----- | :------: | :----------------------------------------------------------- | | name | string | 是 | 数据源名称 | | note | string | 否 | 描述 | | driver | string | 是 | Jdbc 驱动class,若type为其他,则需要讲驱动放置于lib目录内 | | url | string | 是 | Jdbc url | | type | string | 是 | 数据库选择(当前可选有mysql/postgresql/hive/sqlserver/clickhouse/kylin/oracle以及其他) | | username | string | 是 | 用户名 | | password | string | 是 | 密码 | | edit_password | string | 否 | 密码是否为明文 | | tableSql | string | 是 | 查询所有表名称的SQL | **返回参数** | 参数类型 | 描述 | | :------- | :------- | | string | 数据源id | **请求示例** curl -X POST http://127.0.0.1:8520/datasource/add -H "Authorization:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxIiwiZXhwIjoxNjUwNTIzMjYxLCJpYXQiOjE2NTA1MjE0NjF9.rVvmXvBYgQcHCU8vkwe7pPJaRyNl2OwhWpLKWorFafY" -d "name=DataSource¬e=测试4.21&driver=com.mysql.cj.jdbc.Driver&url=jdbc:mysql://docker.for.mac.host.internal:3306/new_test?useSSL=false&characterEncoding=UTF-8&serverTimezone=GMT%2B8&type=mysql&username=root&password=1234qwer&tableSql=show tables" **返回示例** {"msg":"create success","success":true,"data":"{\"createTime\":\"2022-04-21 06:23:42\",\"driver\":\"com.mysql.cj.jdbc.Driver\",\"edit_password\":false,\"id\":\"8fxeabZL\",\"name\":\"DataSource\",\"note\":\"测试4.21\",\"password\":\"CDth4GC35e3+uVm31GQvyw==\",\"tableSql\":\"show tables\",\"type\":\"mysql\",\"updateTime\":\"2022-04-21 06:23:42\",\"url\":\"jdbc:mysql://docker.for.mac.host.internal:3306/new_test?useSSL=false\",\"username\":\"root\"}"} ### 3.修改数据源 **描述**:增加数据源 **请求类型**:POST **请求URL**:/datasource/update **请求参数** | 名称 | 类型 | 是否必须 | 描述 | | :------------ | :----- | :------: | :----------------------------------------------------------- | | id | String | 是 | 数据源id | | name | string | 是 | 数据源名称 | | note | string | 否 | 描述 | | driver | string | 是 | Jdbc 驱动class,若type为其他,则需要讲驱动放置于lib目录内 | | url | string | 是 | Jdbc url | | type | string | 是 | 数据库选择(当前可选有mysql/postgresql/hive/sqlserver/clickhouse/kylin/oracle以及其他) | | username | string | 是 | 用户名 | | password | string | 是 | 密码 | | edit_password | string | 否 | 密码是否为明文 | | tableSql | string | 是 | 查询所有表名称的SQL | **返回参数** | 参数类型 | 描述 | | :------- | :------- | | string | 数据源id | **请求示例** curl -X POST http://127.0.0.1:8520/datasource/update -H "Authorization:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxIiwiZXhwIjoxNjQ5OTMzNTM2LCJpYXQiOjE2NDk5MzE3MzZ9.gp4d5cW3kH8M0XuWE8bnw5tBFxiIP2WM1JX6xRmlwwk" -d "id=NsrgKehz&name=testData¬e=测试&driver=com.mysql.cj.jdbc.Driver&url=jdbc:mysql://docker.for.mac.host.internal:3306/new_test?useSSL=false&characterEncoding=UTF-8&serverTimezone=GMT%2B8&type=mysql&username=root&password=1234qwer&tableSql=show tables" **返回示例** {"msg":"create success","success":true,"data":"{\"createTime\":\"2022-04-14 10:22:35\",\"driver\":\"com.mysql.cj.jdbc.Driver\",\"edit_password\":true,\"id\":\"NsrgKehz\",\"name\":\"testDataSource\",\"note\":\"测试\",\"password\":\"CDth4GC35e3+uVm31GQvyw==\",\"tableSql\":\"show tables\",\"type\":\"mysql\",\"updateTime\":\"2022-04-14 10:22:35\",\"url\":\"jdbc:mysql://docker.for.mac.host.internal:3306/new_test?useSSL=false\",\"username\":\"root\"}"} ### 4.删除数据源 **描述**:增加数据源 **请求类型**:POST **请求URL**:/datasource/delete/{id} **请求参数** 无 **返回参数** | 参数类型 | 描述 | | :------- | :------- | | string | 数据源id | **请求示例** curl -X POST http://127.0.0.1:8520/datasource/delete/wVzf7YdV -H "Authorization:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxIiwiZXhwIjoxNjQ5OTMzNTM2LCJpYXQiOjE2NDk5MzE3MzZ9.gp4d5cW3kH8M0XuWE8bnw5tBFxiIP2WM1JX6xRmlwwk" **返回示例** {"msg":"delete success","success":true,"data":null} ### 5.测试数据源连接 **描述**:增加数据源 **请求类型**:POST **请求URL**:/datasource/connect **请求参数** | 名称 | 类型 | 是否必须 | 描述 | | :------------ | :----- | :------: | :-------------------------------------------------------- | | url | string | 是 | Jdbc url | | username | string | 是 | 用户名 | | password | string | 是 | 密码加密后的密文 | | edit_password | string | 是 | 密码是否为明文 | | driver | string | 是 | Jdbc 驱动class,若type为其他,则需要讲驱动放置于lib目录内 | **返回参数** | 参数类型 | 描述 | | :------- | :------- | | string | 数据源id | **请求示例** curl -X POST http://127.0.0.1:8520/datasource/connect -H "Authorization:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxIiwiZXhwIjoxNjUwNTIzMjYxLCJpYXQiOjE2NTA1MjE0NjF9.rVvmXvBYgQcHCU8vkwe7pPJaRyNl2OwhWpLKWorFafY" -d "url=jdbc:mysql://docker.for.mac.host.internal:3306/new_test?useSSL=false&characterEncoding=UTF-8&serverTimezone=GMT%2B8&username=root&password=1234qwer&edit_password=&driver=com.mysql.cj.jdbc.Driver" **返回示例** {"msg":null,"success":true,"data":null} ### 6.创建API分组 **描述**:增加数据源 **请求类型**:POST **请求URL**:/group/create **请求参数** | 参数类型 | 描述 | | :------- | :------- | | string | 数据源id | **请求示例** curl -X POST http://127.0.0.1:8520/group/create/ -H "Authorization:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxIiwiZXhwIjoxNjUwNTIzMjYxLCJpYXQiOjE2NTA1MjE0NjF9.rVvmXvBYgQcHCU8vkwe7pPaRyNl2OwhWpLKWorFafY" -d "name=测试4.21" **返回示例** 无 ### 7.创建API **描述**:增加数据源 **请求类型**:POST **请求URL**:/apiConfig/add **请求参数** | 名称 | 类型 | 是否必须 | 描述 | | :---------------- | :------ | :------: | :--------------- | | cachePlugin | string | 否 | 缓存插件 | | cachePluginParams | string | 否 | 缓存插件输入参数 | | datasourceId | string | 是 | 数据源id | | groupId | string | 是 | API分组id | | name | string | 是 | API名称 | | note | string | 否 | API描述 | | params | string | 否 | 参数 | | path | string | 是 | 接口后缀 | | previlege | integer | 否 | | | sqlList | list | 是 | 接口的sql语句 | **返回参数** | 参数类型 | 描述 | | :------- | :------ | | string | API信息 | **请求示例** curl -X POST http://127.0.0.1:8520/apiConfig/add -H "Authorization:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxIiwiZXhwIjoxNjUwNTI1ODEyLCJpYXQiOjE2NTA1MjQwMTJ9.5AxSHzsvb9M_9b-QCZrcO2PmeuZKTrKiQK_yuxzhmfs" -H "Accept: application/json" -H "Content-Type: application/json" -d "{\"name\":\"测试API\",\"path\":\"test2-api\",\"note\":null,\"groupId\":\"dkAv22iX\",\"previlege\":1,\"cachePlugin\":null,\"cachePluginParams\":null,\"datasourceId\":\"xnbJ5l9X\",\"sqlList\":[{\"transformPlugin\":null,\"transformPluginParams\":null,\"sqlText\":\"show tables;\"}],\"params\":\"[]\"}" curl -X POST http://127.0.0.1:8520/apiConfig/add -H "Authorization:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxIiwiZXhwIjoxNjUwNTI1ODEyLCJpYXQiOjE2NTA1MjQwMTJ9.5AxSHzsvb9M_9b-QCZrcO2PmeuZKTrKiQK_yuxzhmfs" -H "Accept: application/json" -H "Content-Type: application/json" -d "{\"name\":\"测试API-4.21\",\"path\":\"test421-api\",\"note\":null,\"groupId\":\"dkAv22iX\",\"previlege\":1,\"datasourceId\":\"xnbJ5l9X\",\"sqlList\":[{\"sqlText\":\"select SUM(nums), count(DISTINCT uuid) as count , count(DISTINCT blockchainTime) as count2,AVG(serviceDelay),avg(abs(blockchainDelay)),AVG(abs(delay)),min(clickTime),max(clickTime),max(blockchainDelay),max(delay),max(trueAvgRt) from monitor where blockchainCompleted='success' and serviceCompleted='success' and port='8999-normal';\"}],\"params\":\"[]\"}" **返回示例** {"msg":"create success","success":true,"data":"{\"createTime\":\"2022-04-15 08:09:58\",\"datasourceId\":\"xnbJ5l9X\",\"groupId\":\"dkAv22iX\",\"id\":\"WcWvLFcb\",\"name\":\"测试API\",\"params\":\"[]\",\"path\":\"test2-api\",\"previlege\":1,\"sqlList\":[{\"apiId\":\"WcWvLFcb\",\"id\":24,\"sqlText\":\"show tables;\"}],\"status\":0,\"updateTime\":\"2022-04-15 08:09:58\"}"} ### 8.API上线 **描述**:增加数据源 **请求类型**:POST **请求URL**:/apiConfig/online/{api-id} **请求参数** 无 **返回参数** 无 **请求示例** curl -X POST http://127.0.0.1:8520/apiConfig/online/xoEulFR7 -H "Authorization:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxIiwiZXhwIjoxNjUwNTI1ODEyLCJpYXQiOjE2NTA1MjQwMTJ9.5AxSHzsvb9M_9b-QCZrcO2PmeuZKTrKiQK_yuxzhmfs" **返回示例** 无 ### 9.测试API接口 curl -X GET http://127.0.0.1:8520/api/test421-api {"data":[{"Tables_in_new_test":"Sheet3"},{"Tables_in_new_test":"account"},{"Tables_in_new_test":"hibernate_sequence"},{"Tables_in_new_test":"monitor"}],"success":true}**%** ## springboot集成 如果您想更加灵活的使用DBApi,在您自己的java springboot项目中使用代码配置接口, 请使用[dbApi-spring-boot-starter开源框架](https://gitee.com/freakchicken/dbApi-spring-boot-starter) ## TODO - 支持application/json - 支持API调用记录监控 - 支持API报错邮件告警 - api熔断支持 - 请求路由参数支持 - 流量监控支持,ip限流,分组限流 - 功能性api支持(RPC调用) - 分库分表数据源支持(shardingSphere/mycat)