Let SpringBoot not need Controller, Service, DAO, Mapper, shit! This tool is perfect!

Introduction to Dataway

Dataway is an interface configuration tool for applications based on the DataQL service aggregation capability. It allows users to configure an interface that meets the requirements without developing any code. The entire interface is configured, tested, smoked, published. One-stop shopping is completed through the UI interface provided by Dataway. The UI will be provided as a Jar package and integrated into the application and share the same http port with the application. The application does not need to open a new management port for Dataway separately.

The advantage of this embedded integration mode is that it allows most old projects to directly apply Dataway without intrusion. In turn, it improves the iteration efficiency of old projects and greatly reduces the R&D costs of enterprise projects.

Dataway tool provides DataQL configuration capabilities. This change in the R&D model has enabled quite a few requirements development scenarios to be delivered only through configuration. This avoids a series of development tasks from data access to front-end interface, for example: Mapper, BO, VO, DO, DAO, Service, Controller are all no longer needed.

Dataway is a member of the Hasor ecosystem, so the first thing to do when using Dataway in Spring is to connect the two ecosystems. According to the method recommended in the official document, we integrate Hasor and Spring Boot. Here is the original text: https://www.hasor.net/web/extends/spring/for_boot.html

Step 1: Introduce related dependencies

 
 

<dependency>
    <groupId>net.hasor</groupId>
    <artifactId>hasor-spring</artifactId>
    <version>4.1.3</version>
</dependency>
<dependency>
    <groupId>net.hasor</groupId>
    <artifactId>hasor-dataway</artifactId>
    <version>4.1.3-fix20200414</version><!-- 4.1.3 包存在UI资源缺失问题 -->
</dependency>

hasor-spring is responsible for the integration between Spring and the Hasor framework. hasor-dataway works on Hasor, and we can use dataway with hasor-spring.

Step 2: Configure Dataway and initialize the data table

dataway will provide an interface for us to configure the interface, which is similar to Swagger, as long as the jar package is integrated, the interface configuration can be realized. Find the configuration file  application.properties of our springboot project

 
 

# Whether to enable the Dataway function (required: default false)
HASOR_DATAQL_DATAWAY=true

# Whether to enable the Dataway background management interface (required: 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 setting (optional, recommended setting)
HASOR_DATAQL_FX_PAGE_DIALECT=mysql

Dataway involves a total of 5 configurable configuration items, but not all configurations are required.

Among them,  HASOR_DATAQL_DATAWAY and HASOR_DATAQL_DATAWAY_ADMIN  must be enabled. By default, Datawaty is not enabled.

Dataway needs two data tables to work, and the following is a brief statement of the two data tables. The following SQL can be found under the "META-INF/hasor-framework/mysql" directory in the dependent jar package of dataway. The table creation 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 'Intercept path',
    `api_status` int(2) NOT NULL COMMENT 'Status: 0 draft, 1 published, 2 with changes, 3 disabled',
    `api_comment` varchar(255) NULL COMMENT 'Comment',
    `api_type` varchar (24) NOT NULL COMMENT 'Script type: SQL, DataQL',
    `api_script` mediumtext NOT NULL COMMENT 'Query script: xxxxxxx',
    `api_schema` mediumtext NULL COMMENT 'Interface request/response data structure',
    `api_sample` mediumtext NULL COMMENT 'request/response/request header sample data',
    `api_create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT 'creation time',
    `api_gmt_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT 'modification time',
    PRIMARY KEY (`api_id`)
) ENGI NE =InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COMMENT='API in 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 'intercept path',
    PRIMARY KEY (`pub_id`) ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COMMENT='Dataway API publishing history. ';









create index idx_interface_release on interface_release (pub_api_id);

Step 3: Configure the data source

As a Spring Boot project, it has its own complete database tool support. We use druid + mysql + spring-boot-starter-jdbc this time.

First introduce the 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>

Then increase 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

If the project has integrated its own data source, then the third step can be ignored.

Step 4: Set the data source to the Hasor container

Spring Boot and Hasor are two independent container frameworks. After we integrate, we need to set the data source in Spring to Hasor in order to use the Dataway capability.

First create a Hasor module and hand it over to Spring for management. Then inject the data source 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));
    }
}

When Hasor starts, it will call the loadModule method, and then set 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);
    }
}

This step is very simple, just add two annotations to the Spring startup class.

Step 6: Launch the application

The application will see the welcome message of Hasor Boot during the startup process

 
 

 _    _                        ____              _
| |  | |                      |  _ \            | |
| |__| | __ _ ___  ___  _ __  | |_) | ___   ___ | |_
|  __  |/ _` / __|/ _ \| '__| |  _ < / _ \ / _ \| __|
| |  | | (_| \__ \ (_) | |    | |_) | (_) | (_) | |_
|_|  |_|\__,_|___/\___/|_|    |____/ \___/ \___/ \__|

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

 
 

2020-04-14 13:52:59.696 [main] INFO  n.h.core.context.TemplateAppContext - loadModule class net.hasor.dataway.config.DatawayModule
2020-04-14 13:52:59.697 [main] INFO  n.hasor.dataway.config.DatawayModule - dataway api workAt /api/
2020-04-14 13:52:59.697 [main] INFO  n.h.c.e.AbstractEnvironment - var -> HASOR_DATAQL_DATAWAY_API_URL = /api/.
2020-04-14 13:52:59.704 [main] INFO  n.hasor.dataway.config.DatawayModule - dataway admin workAt /interface-ui/
2020-04-14 13:52:59.716 [main] INFO  net.hasor.core.binder.ApiBinderWrap - mapingTo[901d38f22faa419a8593bb349905ed0e] -> bindType ‘class net.hasor.dataway.web.ApiDetailController’ mappingTo: ‘[/interface-ui/api/api-detail]’.
2020-04-14 13:52:59.716 [main] INFO  net.hasor.core.binder.ApiBinderWrap - mapingTo[c6eb9f3b3d4c4c8d8a4f807435538172] -> bindType ‘class net.hasor.dataway.web.ApiHistoryListController’ mappingTo: ‘[/interface-ui/api/api-history]’.
2020-04-14 13:52:59.717 [main] INFO  net.hasor.core.binder.ApiBinderWrap - mapingTo[eb841dc72ad54023957233ef602c4327] -> bindType ‘class net.hasor.dataway.web.ApiInfoController’ mappingTo: ‘[/interface-ui/api/api-info]’.
2020-04-14 13:52:59.717 [main] INFO  net.hasor.core.binder.ApiBinderWrap - mapingTo[96aebb46265245459ae21d558e530921] -> bindType ‘class net.hasor.dataway.web.ApiListController’ mappingTo: ‘[/interface-ui/api/api-list]’.
2020-04-14 13:52:59.718 [main] INFO  net.hasor.core.binder.ApiBinderWrap - mapingTo[7467c07f160244df8f228321f6262d3d] -> bindType ‘class net.hasor.dataway.web.ApiHistoryGetController’ mappingTo: ‘[/interface-ui/api/get-history]’.
2020-04-14 13:52:59.719 [main] INFO  net.hasor.core.binder.ApiBinderWrap - mapingTo[97d8da5363c741ba99d87c073a344412] -> bindType ‘class net.hasor.dataway.web.DisableController’ mappingTo: ‘[/interface-ui/api/disable]’.
2020-04-14 13:52:59.720 [main] INFO  net.hasor.core.binder.ApiBinderWrap - mapingTo[8ddc3316ef2642dfa4395ca8ac0fff04] -> bindType ‘class net.hasor.dataway.web.SmokeController’ mappingTo: ‘[/interface-ui/api/smoke]’.
2020-04-14 13:52:59.720 [main] INFO  net.hasor.core.binder.ApiBinderWrap - mapingTo[cc06c5fb343b471aacedc58fb2fe7bf8] -> bindType ‘class net.hasor.dataway.web.SaveApiController’ mappingTo: ‘[/interface-ui/api/save-api]’.
2020-04-14 13:52:59.720 [main] INFO  net.hasor.core.binder.ApiBinderWrap - mapingTo[7915b2b1f89a4e73891edab0264c9bd4] -> bindType ‘class net.hasor.dataway.web.PublishController’ mappingTo: ‘[/interface-ui/api/publish]’.
2020-04-14 13:52:59.721 [main] INFO  net.hasor.core.binder.ApiBinderWrap - mapingTo[0cfa34586455414591bdc389bff23ccb] -> bindType ‘class net.hasor.dataway.web.PerformController’ mappingTo: ‘[/interface-ui/api/perform]’.
2020-04-14 13:52:59.721 [main] INFO  net.hasor.core.binder.ApiBinderWrap - mapingTo[37fe4af3e2994acb8deb72d21f02217c] -> bindType ‘class net.hasor.dataway.web.DeleteController’ mappingTo: ‘[/interface-ui/api/delete]’.

When you see “dataway api workAt  /api/  ” and dataway admin workAt  /interface-ui/  information, you can be sure that the Dataway configuration has taken effect.

Step 7: Access the interface management page to configure the interface

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

picture

Step 8: Create a new interface

Dataway provides 2 language modes, we can use the powerful DataQL query language, or directly use the SQL language (in Dataway, the SQL language will also be converted into DataQL for execution.)

picture

First, we try to execute a select query in SQL mode, and we can immediately see the query result of this SQL.

picture

In the same way, the way we use DataQL needs to be written like this:

 
 

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

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

Finally, call this block of code in DataQL and return the query result.

After the interface is written, it can be saved and released. For the convenience of testing, I choose the GET method.

picture

After the interface is published, we directly request: http://127.0.0.1:8080/api/demos, and we see the long-awaited return value of the interface.

picture

final summary

After the above steps, we introduced how to use Dataway to simply configure the interface based on the Spring Boot project. The Dataway method is really refreshing. An interface can be configured so easily without developing a single line of code, and without doing any Mapping entity mapping binding.

Finally, put a few useful links:

  • Dataway official manual: https://www.hasor.net/web/dataway/about.html

  • Dataway's project address on OSC, welcome to bookmark:

    • https://www.oschina.net/p/dataway

  • DataQL manual address: https://www.hasor.net/web/dataql/what_is_dataql.html

  • Home page of Hasor project: https://www.hasor.net/web/index.html

Guess you like

Origin blog.csdn.net/Blue92120/article/details/132035282