Dataway is introduced

Dataway is an interface configuration tool for applications based on the DataQL service aggregation capability, enabling users to configure an interface that meets their requirements without developing any code. The entire interface is configured, tested, smoked, and published through the UI provided by Dataway. The UI is provided as a Jar package and integrated into the application and shares the same HTTP port with the application; the application does not need to create a separate management port for the Dataway.

The advantage of this inline integration approach pattern is that most older projects can apply Dataway directly without incursion. Thus, the iterative efficiency of old projects is improved and the r&d cost of enterprise projects is greatly reduced.

Dataway provides DataQL configuration capabilities instrumentally. As a result of this change in the r&d model, a significant number of requirements development scenarios require only configuration to complete delivery. This avoids a series of development tasks from data access to the front-end interface, such as Mapper, BO, VO, DO, DAO, Service, Controller are not needed.

Dataway is a member of the Hasor ecosystem, so the first thing to do with using Dataway in Spring is to get through the two ecosystems. We integrate Hasor and Spring Boot in the way recommended in the official documentation. Here is the original: www.hasor.net/web/extends…

Step 1: Introduce related dependencies

<dependency> <groupId>net.hasor</groupId> <artifactId>hasor-spring</artifactId> <version>4.1.3</version> </dependency> < the dependency > < groupId > net. Hasor < / groupId > < artifactId > hasor - dataway < / artifactId > < version > 4.1.3 - fix20200414 < / version > <! </dependency> </dependency>Copy the code

Hasor-spring is responsible for the integration between the Spring and Hasor frameworks. Hasor-dataway works on top of Hasor. With Hasor-spring we can use dataway.

Step 2: Configure the Dataway and initialize the data table

Dataway will provide an interface to configure the interface, similar to Swagger that can be configured as long as the JAR package is integrated. Find the configuration file application.properties for our Springboot project

# Enable Dataway (mandatory: default false) HASOR_DATAQL_DATAWAY=true # Enable Dataway (mandatory: Default false) HASOR_DATAQL_DATAWAY_ADMIN=true # dataway API working path (optional, default: / API /) HASOR_DATAQL_DATAWAY_API_URL=/ API / # dataway- UI working path (optional, default: /interface-ui/) HASOR_DATAQL_DATAWAY_UI_URL=/interface-ui/ # SQL executor dialect Settings (optional, recommended) HASOR_DATAQL_FX_PAGE_DIALECT=mysqlCopy the code

Dataway involves five configuration items that can be configured, but not all of them are required.

HASOR_DATAQL_DATAWAY and HASOR_DATAQL_DATAWAY_ADMIN must be enabled. By default, Datawaty is disabled.

The Dataway requires two tables to work, and here are the short table statements for both tables. The following SQL can be found in the META-INF/hasor-framework/mysql directory of the dataway dependent JAR package. The statement is written in mysql syntax.

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' status' : 0 draft, 1 published, 2 changed, 3 disabled ', 'API_COMMENT' varchar(255) NULL COMMENT 'COMMENT ',' API_type 'varchar(24) NOT NULL COMMENT' SQL, DataQL', 'api_script' mediumtext NOT NULL COMMENT ' XXXXXXX ', 'API_SCHEMA' mediumTEXT NULL COMMENT 'interface request/response data structure ',' API_SAMPLE 'mediumtext NULL COMMENT' interface request/response data structure ', 'API_create_time' datetime DEFAULT CURRENT_TIMESTAMP COMMENT 'create time ', 'API_gmt_time' datetime DEFAULT CURRENT_TIMESTAMP COMMENT 'CURRENT_TIMESTAMP ', PRIMARY KEY (' api_id ') ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET= UTf8MB4 COMMENT='Dataway '; 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' status' : ', 'pub_type' varchar(24) NOT NULL COMMENT ' SQL, DataQL', 'pub_script' mediumtext NOT NULL COMMENT ' XXXXXXX ', 'pub_script_ori' mediumtext NOT NULL COMMENT ', Different only when type is SQL ', 'pub_schema' mediumTEXT NULL COMMENT 'interface request/response data structure ', 'pub_sample' mediumtext NULL COMMENT 'request/response/headers ', 'pub_release_time' datetime DEFAULT CURRENT_TIMESTAMP COMMENT 'CURRENT_TIMESTAMP ', PRIMARY KEY (' pub_id ') ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET= UTf8MB4 COMMENT='Dataway ' '; create index idx_interface_release on interface_release (pub_api_id);Copy the code

Step 3: Configure the data source

As a Spring Boot project, it has its own perfect database tool support. Druid + mysql + spring-boot-starter-JDBC

First introduce dependencies

< the dependency > < groupId > mysql < / groupId > < artifactId > mysql connector - Java < / artifactId > < version > 5.1.30 < / version > </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> The < version > 1.1.10 < / version > < / dependency >Copy the code

Then add the configuration of the data source

# db
spring.datasource.url=jdbc:mysql://xxxxxxx:3306/example
spring.datasource.username=xxxxx
spring.datasource.password=xxxxx
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
Copy the code

If the project has already integrated its own data sources, you can skip step 3.

Step 4: Set up the data source into the Hasor container

Spring Boot and Hasor are two separate container frameworks. After integration, we need to set the data source in Spring to Hasor in order to use Dataway capabilities.

Start by creating a Hasor module and handing it over to Spring to manage. The data source is then injected through Spring.

@DimModule @Component public class ExampleModule implements SpringModule { @Autowired private DataSource dataSource = null; @Override public void loadModule(ApiBinder apiBinder) throws Throwable { // .DataSource form Spring boot into Hasor apiBinder.installModule(new JdbcModule(Level.Full, this.dataSource)); }}Copy the code

When Hasor starts, it calls the loadModule method, where it assigns the DataSource to Hasor.

Step 5: Enable Hasor in SprintBoot

@EnableHasor() @EnableHasorWeb() @SpringBootApplication(scanBasePackages = { "net.example.hasor" }) public class ExampleApplication { public static void main(String[] args) { SpringApplication.run(ExampleApplication.class, args); }}Copy the code

This step is as simple as adding two annotations to the Spring startup class.

Step 6: Launch the application

The welcome message of Hasor Boot is displayed during application startup

_ _ ____ _ | | | | | _ \ | | | | __ | | __ __ ___ ___ __ __ | | | ___ ___ _) | | _ | | / _ ` / (| / _ \ | '__ | | _ < / / _ \ _ Payable \ | | | | | | (_ | \ __ \ (_) | | | | _) | | (_) (_) | | _ _ - | | | _ | \ __, _ | ___ / \ ___ / | _ | | _____ / ___ / \ ___ / \ | 4Copy the code

You can also see logs similar to the following in later logs.

The 2020-04-14 13:52:59. 696. [the main] INFO N.H.C ore. Context. TemplateAppContext - loadModule class Net. Hasor. Dataway. Config. DatawayModule 13:52:59. 2020-04-14, 697 [main] INFO n.h asor. Dataway. Config. DatawayModule - Dataway API 13:52:59 workAt/API / 2020-04-14. 697. [the main] INFO N.H.C.E.A bstractEnvironment - var - > 13:52:59 HASOR_DATAQL_DATAWAY_API_URL = / API /. 2020-04-14. 704. [the main] INFO n.h asor. Dataway. Config. DatawayModule - dataway Admin workAt/interface - UI / 13:52:59. 2020-04-14, 716 [main] INFO net. Hasor. Core. Binder. ApiBinderWrap - D38f22faa419a8593bb349905ed0e mapingTo [901] - > bindType 'class net. Hasor. Dataway. Web. ApiDetailController mappingTo' : '[/ interface - the UI/API/API - the detail].' the 2020-04-14 13:52:59. 716 net [main] INFO. Hasor. Core. Binder. ApiBinderWrap - MapingTo [c6eb9f3b3d4c4c8d8a4f807435538172] - > bindType 'class net. Hasor. Dataway. Web. ApiHistoryListController mappingTo' : '[/ interface - the UI/API/API - history].' the 2020-04-14 13:52:59. 717 net [main] INFO. Hasor. Core. Binder. ApiBinderWrap - MapingTo [eb841dc72ad54023957233ef602c4327] - > bindType 'class net. Hasor. Dataway. Web. ApiInfoController mappingTo' : '[/ interface - the UI/API/API - info].' the 2020-04-14 13:52:59. 717 net [main] info. Hasor. Core. Binder. ApiBinderWrap - Aebb46265245459ae21d558e530921 mapingTo [96] - > bindType 'class net. Hasor. Dataway. Web. ApiListController mappingTo' : '[/ interface - the UI/API/API - the list].' the 2020-04-14 13:52:59. 718 net [main] INFO. Hasor. Core. Binder. ApiBinderWrap - C07f160244df8f228321f6262d3d mapingTo [7467] - > bindType 'class net. Hasor. Dataway. Web. ApiHistoryGetController mappingTo' : '[/ interface - the UI/API/get - history].' the 2020-04-14 13:52:59. 719 net [main] INFO. Hasor. Core. Binder. ApiBinderWrap - D8da5363c741ba99d87c073a344412 mapingTo [97] - > bindType 'class net. Hasor. Dataway. Web. DisableController mappingTo' : '[/ interface - the UI/API/disable]'. The 2020-04-14 13:52:59. [the main] 720 INFO net. Hasor. Core. Binder. ApiBinderWrap - Ddc3316ef2642dfa4395ca8ac0fff04 mapingTo [8] - > bindType 'class net. Hasor. Dataway. Web. SmokeController mappingTo' : '[/ interface - the UI/API/smoke]. "the 2020-04-14 13:52:59. 720 net [main] INFO. Hasor. Core. Binder. ApiBinderWrap - MapingTo [cc06c5fb343b471aacedc58fb2fe7bf8] - > bindType 'class net. Hasor. Dataway. Web. SaveApiController mappingTo' : '[/ interface - the UI/API/save - API].' the 2020-04-14 13:52:59. 720 net [main] INFO. Hasor. Core. Binder. ApiBinderWrap - B2b1f89a4e73891edab0264c9bd4 mapingTo [7915] - > bindType 'class net. Hasor. Dataway. Web. PublishController mappingTo' : '[/ interface - the UI/API/publish]'. The 2020-04-14 13:52:59. 721 net [main] INFO. Hasor. Core. Binder. ApiBinderWrap - Cfa34586455414591bdc389bff23ccb mapingTo [0] - > bindType 'class net. Hasor. Dataway. Web. PerformController mappingTo' : '[/ interface - the UI/API/perform]'. The 2020-04-14 13:52:59. [the main] 721 INFO net. Hasor. Core. Binder. ApiBinderWrap - 37 fe4af3e2994acb8deb72d21f02217c mapingTo [] - > bindType 'class net. Hasor. Dataway. Web. DeleteController mappingTo' : '[/ interface - the UI/API/delete]'.Copy the code

When you see the “Dataway API workAt/API/” and the” dataway admin workAt /interface-ui/ “message, you can confirm that the dataway configuration has taken effect.

Step 7: Access the interface management page to configure interfaces

Enter “http://127.0.0.1:8080/interface-ui/” in your browser to see the long-awaited interface.

! [](https://p6-tt-ipv6.byteimg.com/large/pgc-image/0e8b88ea06da45cdad1201a95538801c)

Step 8: Create an interface

Dataway provides two language patterns, which can be used either in the powerful DataQL query language or directly in SQL (the SQL language is also converted to DataQL for execution within Dataway).

! [](https://p6-tt-ipv6.byteimg.com/large/pgc-image/af51c846a02449c4bf57df13e6ac0a9a)

We first try to execute a SELECT query in SQL mode, and immediately see the result of this SQL query.

! [](https://p1-tt-ipv6.byteimg.com/large/pgc-image/ab65458a064a4df99b52086b04ea20a9)

In the same way that we use DataQL we need to write:

var query = @@sql()<%
    select * from interface_info
%>
return query()
Copy the code

Var query = @@sql()<%… %> is used to define the SQL external code block and store this definition in the Query variable name. <% %> in the middle is the SQL statement.

Finally, this code block is called in DataQL and the query result is returned.

When the interface is written, it can be saved and published. For the convenience of testing, I choose GET mode.

! [](https://p26-tt.byteimg.com/large/pgc-image/54983bebee3c49d7babdd18df7756aa1)

Interface release after we request directly: http://127.0.0.1:8080/api/demos, to see the joints of the long-awaited return values.

! [](https://p6-tt-ipv6.byteimg.com/large/pgc-image/0c65d2e5910c4f5aad84982168e11178)

The final summary

After the previous steps we showed you how to use Dataway to simply configure the interface based on the Spring Boot project. The Dataway approach is really refreshing because an interface can be configured so easily without developing a single line of code or doing any Mapping entity Mapping binding.