Dataway整合Spring Boot
一、创建spring boot项目工程
二、添加dataway需要引用的两种依赖hasor-spring、hasor-dataway,hasor-spring是提供spring和dataway需要的环境
<dependency>
<groupId>net.hasor</groupId>
<artifactId>hasor-spring</artifactId>
<version>4.1.13</version>
</dependency>
<dependency>
<groupId>net.hasor</groupId>
<artifactId>hasor-dataway</artifactId>
<version>4.1.13</version>
</dependency>
三、在对应的application.properties或者application.yml中添加如下参数
1) dataway参数如下,前两种必选,默认false关闭状态
# 是否启用 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
2)配置数据库参数如下,并在hasor-dataway-4.1.13.jar > META-INF > hasor-framework > mysql(因为本文使用的是mysql,如果你用的不是mysql也可以在同路径中查找interface_info.sql、interface_release.sql两个sql建表语句,目前4.1.13支持四种数据库(mysql、oracle、postgresql、sqlserver2012))
# db
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xx
spring.datasource.username=xxxx
spring.datasource.password=xxxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type:com.alibaba.druid.pool.DruidDataSource
#interface_info.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 unique index idx_interface_info on interface_info (api_method, api_path);
#interface_release.sql
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);
3) 配置druid
# 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
四、添加spring相关依赖如下所示
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
注解:这里需要额外引入以下依赖防止启动报错找不到
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
五、编写ExampleModule类继承SpringModule,并在创建项目的启动类上添加@EnableHasor、@EnableHasorWeb两注解来启用 Hasor和Hasor Web,前者在bean上标记了 @DimModule 注解并且实现了 Module 接口,那么它们会被作为 Hasor 的初始化 Module;后者配置 了Hasor 的全局拦截器和监听器。
@DimModule
@Component
public class ExampleModule implements SpringModule{
@Autowired
private DataSource dataSource = null;
@Override
public void loadModule(ApiBinder apiBinder) throws Throwable {
apiBinder.installModule(new JdbcModule(Level.Full, this.dataSource));
}
}
六、启动项目,看到如下日志,项目则配置成功,可直接本地访问http://localhost:8080/interface-ui/#/
2020-08-03 14:50:32.896 INFO 79548 --- [ost-startStop-1] net.hasor.dataway.config.DatawayModule : dataway api workAt /api/
2020-08-03 14:50:32.896 INFO 79548 --- [ost-startStop-1] n.h.c.environment.AbstractEnvironment : var -> HASOR_DATAQL_DATAWAY_API_URL = /api/.
2020-08-03 14:50:32.908 INFO 79548 --- [ost-startStop-1] net.hasor.dataway.config.DatawayModule : dataway self isolation ->net.hasor.dataway.config.DatawayModule
2020-08-03 14:50:32.912 INFO 79548 --- [ost-startStop-1] net.hasor.dataway.config.DatawayModule : dataway admin workAt /interface-ui/