一、介紹
依託 DataQL 服務聚合能力,爲應用提供一個 UI 界面。並以 jar 包的方式集成到應用中。 通過 Dataway 可以直接在界面上配置和發佈接口。
這種模式的革新使得開發一個接口不必在編寫任何形式的代碼,只需要配置一條 DataQL 查詢即可完成滿足前端對接口的需求。 從而避免了從數據庫到前端之間一系列的開發配置任務,例如:Mapper、DO、DAO、Service、Controller 統統不在需要。
Dataway特意採用了 jar包集成的方式發佈,這使得任意的老項目都可以無侵入的集成 Dataway。 直接改進老項目的迭代效率,大大減少企業項目研發成本。
Dataway 工具化的提供 DataQL 配置能力。這種研發模式的變革使得,相當多的需求開發場景只需要配置即可完成交付。 從而避免了從數據存取到前端接口之間的一系列開發任務,例如:Mapper、BO、VO、DO、DAO、Service、Controller 統統不在需要。
如上圖所示 Dataway 在開發模式上提供了巨大的便捷。雖然工作流程中標識了由後端開發來配置 DataQL 接口,但這主要是出於考慮接口責任人。 但在實際工作中根據實際情況需要,配置接口的人員可以是產品研發生命週期中任意一名角色。
- Dataway 官方手冊:https://www.hasor.net/web/dataway/about.html
- Dataway OSC 項目地址:https://www.oschina.net/p/dataway
- DataQL 手冊地址:https://www.hasor.net/web/dataql/what_is_dataql.html
- Hasor 項目的首頁:https://www.hasor.net/web/index.html
二、實現
如下圖所示:生成接口
測試接口查詢
依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.citydo</groupId>
<artifactId>dataway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dataway</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- 引入依賴 -->
<dependency>
<groupId>net.hasor</groupId>
<artifactId>hasor-spring</artifactId>
<version>4.1.6</version>
</dependency>
<dependency>
<groupId>net.hasor</groupId>
<artifactId>hasor-dataway</artifactId>
<version>4.1.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置
# 是否啓用 Dataway 功能(必選:默認false)
HASOR_DATAQL_DATAWAY=true
# 是否開啓 Dataway 後臺管理界面(必選:默認false)
HASOR_DATAQL_DATAWAY_ADMIN=true
# dataway API工作路徑(可選,默認:/api/)
HASOR_DATAQL_DATAWAY_API_URL=/api/
# dataway-ui 的工作路徑(可選,默認:/interface-ui/)
HASOR_DATAQL_DATAWAY_UI_URL=/interface-ui/
# SQL執行器方言設置(可選,建議設置)
HASOR_DATAQL_FX_PAGE_DIALECT=mysql
# db
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/znkf_new
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# druid
spring.datasource.druid.initial-size=3
spring.datasource.druid.min-idle=3
spring.datasource.druid.max-active=10
spring.datasource.druid.max-wait=60000
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=admin
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=1
代碼
package com.citydo.dataway;
import net.hasor.spring.boot.EnableHasor;
import net.hasor.spring.boot.EnableHasorWeb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableHasor() // 在Spring 中啓用 Hasor
@EnableHasorWeb() // 將 hasor-web 配置到 Spring 環境中,Dataway 的 UI 是通過 hasor-web 提供服務。
public class DatawayApplication {
public static void main(String[] args) {
SpringApplication.run(DatawayApplication.class, args);
}
}
package com.citydo.dataway;
import net.hasor.core.ApiBinder;
import net.hasor.core.DimModule;
import net.hasor.db.JdbcModule;
import net.hasor.db.Level;
import net.hasor.spring.SpringModule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
@DimModule
@Component
public class ExampleModule implements SpringModule {
@Autowired
private DataSource dataSource;
@Override
public void loadModule(ApiBinder apiBinder) throws Throwable {
// .DataSource form Spring boot into Hasor
apiBinder.installModule(new JdbcModule(Level.Full, this.dataSource));
// .custom DataQL
//apiBinder.tryCast(QueryApiBinder.class).loadUdfSource(apiBinder.findClass(DimUdfSource.class));
//apiBinder.tryCast(QueryApiBinder.class).bindFragment("sql", SqlFragment.class);
}
}
sql
CREATE TABLE `interface_info` (
`api_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`api_method` varchar(12) NOT NULL COMMENT 'HttpMethod:GET、PUT、POST',
`api_path` varchar(512) NOT NULL COMMENT '攔截路徑',
`api_status` int(2) NOT NULL COMMENT '狀態:0草稿,1發佈,2有變更,3禁用',
`api_comment` varchar(255) NULL COMMENT '註釋',
`api_type` varchar(24) NOT NULL COMMENT '腳本類型:SQL、DataQL',
`api_script` mediumtext NOT NULL COMMENT '查詢腳本:xxxxxxx',
`api_schema` mediumtext NULL COMMENT '接口的請求/響應數據結構',
`api_sample` mediumtext NULL COMMENT '請求/響應/請求頭樣本數據',
`api_option` mediumtext NULL COMMENT '擴展配置信息',
`api_create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '創建時間',
`api_gmt_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '修改時間',
PRIMARY KEY (`api_id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COMMENT='Dataway 中的API';
CREATE TABLE `interface_release` (
`pub_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Publish ID',
`pub_api_id` int(11) NOT NULL COMMENT '所屬API ID',
`pub_method` varchar(12) NOT NULL COMMENT 'HttpMethod:GET、PUT、POST',
`pub_path` varchar(512) NOT NULL COMMENT '攔截路徑',
`pub_status` int(2) NOT NULL COMMENT '狀態:0有效,1無效(可能被下線)',
`pub_type` varchar(24) NOT NULL COMMENT '腳本類型:SQL、DataQL',
`pub_script` mediumtext NOT NULL COMMENT '查詢腳本:xxxxxxx',
`pub_script_ori` mediumtext NOT NULL COMMENT '原始查詢腳本,僅當類型爲SQL時不同',
`pub_schema` mediumtext NULL COMMENT '接口的請求/響應數據結構',
`pub_sample` mediumtext NULL COMMENT '請求/響應/請求頭樣本數據',
`pub_option` mediumtext NULL COMMENT '擴展配置信息',
`pub_release_time`datetime DEFAULT CURRENT_TIMESTAMP COMMENT '發佈時間(下線不更新)',
PRIMARY KEY (`pub_id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COMMENT='Dataway API 發佈歷史。';
create index idx_interface_release on interface_release (pub_api_id);