Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Dataway to configure data query interface for SpringBoot

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how SpringBoot uses the Dataway configuration data query interface. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Dataway introduction

Dataway is an interface configuration tool for applications based on the aggregation capability of DataQL services. Enables the user to configure an interface that meets the requirements without developing any code. The whole interface is configured, tested, smoked and released. The one-stop shop is done through the UI interface provided by Dataway. UI is provided as Jar package and integrated into the application and shares the same http port with the application, so the application does not need to open up a new management port for Dataway separately.

The advantage of this embedded integration mode is that it allows most older projects to apply Dataway directly without intrusiveness. Then improve the iterative efficiency of the old project and greatly reduce the R & D cost of the enterprise project.

Dataway tooling provides DataQL configuration capabilities. As a result of this change in the R & D model, a considerable number of requirements development scenarios can be delivered only by configuration. Thus avoiding a series of development tasks from data access to front-end interfaces, such as Mapper, BO, VO, DO, DAO, Service, Controller are all not needed.

Dataway is a member of Hasor ecology, so the first thing to do to use 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 text: https://www.hasor.net/web/extends/spring/for_boot.html

Step 1: introduce related dependencies net.hasor hasor-spring 4.1.3 net.hasor hasor-dataway 4.1.3-fix20200414

Hasor-spring is responsible for the integration between Spring and Hasor frameworks.

Hasor-dataway works on top of Hasor, and with hasor-spring we can use dataway.

Step 2: configure Dataway and initialize the data table

Dataway provides an interface for us to configure interfaces, which is similar to Swagger as long as the jar package set achieves interface configuration. Find the configuration file application.properties for our springboot project

# whether to enable the Dataway function (required: default false) whether HASOR_DATAQL_DATAWAY=true# enables the Dataway background management interface (required: default false) HASOR_DATAQL_DATAWAY_ADMIN=true# dataway API work path (optional, default: / api/) HASOR_DATAQL_DATAWAY_API_URL=/api/# dataway-ui work path (optional, default: / interface-ui/) HASOR_DATAQL_DATAWAY_UI_URL=/interface-ui/# SQL executor dialect settings (optional Recommended setting) HASOR_DATAQL_FX_PAGE_DIALECT=mysql

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

The two configurations of HASOR_DATAQL_DATAWAY and HASOR_DATAQL_DATAWAY_ADMIN must be turned on, and Datawaty is not enabled by default.

Dataway requires two data tables to work. Here are the summary table statements for these two data tables. The following SQL can be found under the "META-INF/hasor-framework/mysql" directory in dataway's dependent jar package, and the table 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`path 'NOT NULL COMMENT' blocking path', `api_ status`int (2) NOT NULL COMMENT 'status: 0 draft 1 release, 2 changes 3 disable', `api_ comment` varchar 'NULL COMMENT' comment', `api_ type` varchar (24) NOT NULL COMMENT 'script type: SQL, DataQL', `api_ script` mediumtext NOT NULL COMMENT' query script: xxxxxxx' The request / response data structure of the `api_ schema`mediumtext NULL COMMENT 'API, `api_ sample`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' API' in PRIMARY KEY (`api_ id`) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COMMENT='Dataway CREATE TABLE `pub_ method varchar (12) NOT NULL COMMENT 'HttpMethod:GET, PUT, POST', `pub_ path`varchar' NOT NULL COMMENT 'intercept path' to which CREATE TABLE `pub_ int (11) NOT NULL AUTO_INCREMENT COMMENT 'Publish ID', `pub_ id` int (11) NOT NULL COMMENT' belongs `pub_ status` int (2) NOT NULL COMMENT 'status: 0 valid 1 invalid (may be offline)', `pub_ type`varchar (24) NOT NULL COMMENT 'script type: SQL, DataQL', `pub_ script`mediumtext NOT NULL COMMENT' query script: xxxxxxx', `pub_script_ ori` mediumtext NOT NULL COMMENT 'original query script It is different only when the type is SQL', the request / response data structure of the API `pub_ schema`mediumtext NULL COMMENT', `request / response / request header sample data', `pub_release_ time`datetime DEFAULT CURRENT_TIMESTAMP COMMENT 'release time (no offline update)' 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 perfect database tool support. We use druid + mysql + spring-boot-starter-jdbc this time.

First, introduce dependency.

Mysql mysql-connector-java 5.1.30 com.alibaba druid 1.1.21 org.springframework.boot spring-boot-starter-jdbc com.alibaba druid-spring-boot-starter 1.1.10

Then add the configuration of the data source

# dbspring.datasource.url=jdbc:mysql://xxxxxxx:3306/examplespring.datasource.username=xxxxxspring.datasource.password=xxxxxspring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.type:com.alibaba.druid.pool.DruidDataSource# druidspring.datasource.druid.initial-size=3spring.datasource.druid.min-idle=3spring.datasource.druid.max-active=10spring.datasource.druid.max-wait=60000spring.datasource.druid.stat-view-servlet.login-username=adminspring.datasource.druid.stat-view-servlet.login-password=adminspring.datasource.druid.filter .stat.log-slow-sql=truespring.datasource.druid.filter.stat.slow-sql-millis=1

If the project has integrated its own data sources, you can ignore the third step.

Step 4: set the data source to the Hasor container

Spring Boot and Hasor are two independent container frameworks. After integration, we need to set the data sources in Spring to Hasor in order to use the ability of Dataway.

First create a new module for Hasor and give it to Spring to manage. Then inject the data source through Spring.

@ DimModule@Componentpublic 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);}}

The loadModule method is called when Hasor starts, and here the DataSource is set to Hasor.

Step 5: enable Hasor@EnableHasor () @ EnableHasorWeb () @ SpringBootApplication (scanBasePackages = {"net.example.hasor"}) public class ExampleApplication {public static void main (String [] args) {SpringApplication.run (ExampleApplication.class, args);} in SprintBoot

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

Step 6: start the application

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

| _ _ | | _ _ _ | | _ _ _ | _ _ _ | | _ _ _ | / _ `/ _ _ _ | / _\ |'_ _ | | _

< / _ \ / _ \| __|| | | | (_| \__ \ (_) | | | |_) | (_) | (_) | |_|_| |_|\__,_|___/\___/|_| |____/ \___/ \___/ \__| 在后面的日志中还可以看到类似下面这些日志。 2020-04-14 13:52:59.696 [main] INFO n.h.core.context.TemplateAppContext - loadModule class net.hasor.dataway.config.DatawayModule2020-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 main 52VR 59.704 [main] INFO n.hasor.dataway.config.DatawayModule-dataway admin workAt / interface-ui/2020-04-14 1313V 52DA 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 52mapingTo 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 mapingTo 52V 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 13 mapingTo 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 13 INFO net.hasor.core.binder.ApiBinderWrap 52 mapingTo 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 12 12 12 mapingTo 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 13 INFO net.hasor.core.binder.ApiBinderWrap mapingTo 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 13 13 INFO net.hasor.core.binder.ApiBinderWrap 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 main 52 mapingTo 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 13 main 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 the "dataway api workAt / api/" and dataway admin workAt / interface-ui/ messages, you can be sure that the configuration of Dataway has taken effect.

Step 7: access the interface management page for interface configuration

Type "http://127.0.0.1:8080/interface-ui/"" in the browser and you can see the long-awaited interface.

Step 8: create a new interface

Dataway provides two language patterns, we can use the powerful DataQL query language, or we can directly use the SQL language (within Dataway, the SQL language will also be converted to DataQL for execution. )

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

In the same way we use DataQL, we need to write this:

Var query = @ @ sql () return query ()

Where var query = @ @ sql () is used to define the SQL external code block and stores this definition in the query variable name. In the middle is the SQL statement.

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

When the interface is written, you can save the release. For the convenience of testing, I choose GET.

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

Thank you for reading! This is the end of the article on "how SpringBoot uses Dataway configuration data query interface". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report