dataway基本介绍及与springboot集成

介绍:

在我看来,dataway是一个快速的构建接口的构件。

有了dataway,接口不需要写业务代码,可以直接快速发布。

其原理大概类似于。数据库存放url和sql的映射。到时候执行哪个url就对应执行哪条sql就是了。

dataway如果想做些一般复杂的事情的逻辑的话,是需要自己的语言dataQL的.但是由于其是弱语言,其实并不擅长做更复杂的事情。

springboot集成:

dataway是需要挂靠在web服务器上的,所以会配置tomcat及server.port

dataway是基于数据库存储映射关系,所以也需要配置数据库

maven:

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.6.RELEASE</version>
		<relativePath />
	</parent>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		
		
		<!-- 引入依赖 -->
		<dependency>
			<groupId>net.hasor</groupId>
			<artifactId>hasor-spring</artifactId>
			<version>4.1.8</version><!-- 查看最新版本:https://mvnrepository.com/artifact/net.hasor/hasor-spring -->
		</dependency>
		<dependency>
			<groupId>net.hasor</groupId>
			<artifactId>hasor-dataway</artifactId>
			<version>4.1.8</version><!-- 查看最新版本:https://mvnrepository.com/artifact/net.hasor/hasor-dataway -->
		</dependency>
		
		
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.21</version>
		</dependency>

		
	</dependencies>

启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import net.hasor.spring.boot.EnableHasor;
import net.hasor.spring.boot.EnableHasorWeb;

@EnableHasor
@EnableHasorWeb
@SpringBootApplication
public class DataWayApp {
	public static void main(String[] args) {
		SpringApplication.run(DataWayApp.class, args);
	}
}

dataway配置类:

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import net.hasor.core.ApiBinder;
import net.hasor.core.DimModule;
import net.hasor.dataway.spi.ApiInfo;
import net.hasor.dataway.spi.PreExecuteChainSpi;
import net.hasor.dataway.spi.ResultProcessChainSpi;
import net.hasor.db.JdbcModule;
import net.hasor.db.Level;
import net.hasor.spring.SpringModule;
import net.hasor.utils.future.BasicFuture;

@DimModule
@Component
public class DataWayConfig implements SpringModule{

	@Autowired
	private DataSource dataSource;
	
	@Override
	public void loadModule(ApiBinder apiBinder) throws Throwable {
		/**
		 * 绑定数据源
		 */
		apiBinder.installModule(new JdbcModule(Level.Full, dataSource));
		
		apiBinder.bindSpiListener(PreExecuteChainSpi.class, new PreExecuteChainSpi() {
			/**
			 * 前置过滤器,可以进行校验等操作
			 * 如果设置了future,该接口将直接返回future.不再继续执行.可以实现缓存.
			 * 
			 */
			@Override
			public void preExecute(ApiInfo apiInfo, BasicFuture<Object> future) {
				Map<String, Object> parameterMap = apiInfo.getParameterMap();//参数
				String apiPath = apiInfo.getApiPath();//获取url
				System.out.println("===preExecute");
			}
		});
		
		apiBinder.bindSpiListener(ResultProcessChainSpi.class, new ResultProcessChainSpi() {
			/**
			 * 后置过滤器
			 * 可以获取到返回值.对返回值统一处理
			 */
			@Override
			public Object callAfter(boolean formPre, ApiInfo apiInfo, Object result) {
				System.out.println("=====callAfter");
				
				Map<String, Object> optionMap = apiInfo.getOptionMap();
        		optionMap.put("resultStructure", false);//设置自定义返回值格式,不再拘于默认结构
        		apiInfo.setOptionMap(optionMap);        		
        		
        		/**
        		 * 返回对象
        		 */
        		Map<String,Object>map = new HashMap<>();
        		map.put("code", 0);
        		map.put("body", result);
        		return map;
			}
			/**
			 * 如果出现异常怎么做
			 */
			@Override
			public Object callError(boolean formPre, ApiInfo apiInfo, Throwable e) {
				
				return ResultProcessChainSpi.super.callError(formPre, apiInfo, e);
			}
			
		});
		
	}

}

application.properties

server.port=8080
spring.datasource.url=jdbc:mysql://192.168.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driverClassName=com.mysql.jdbc.Driver
logging.level.root=info


# 启用 Dataway 功能(默认不启用)
HASOR_DATAQL_DATAWAY=true
# 开启 ui 管理功能(注意生产环境必须要设置为 false,否则会造成严重的生产安全事故)
HASOR_DATAQL_DATAWAY_ADMIN=true

# (可选)API工作路径
HASOR_DATAQL_DATAWAY_API_URL=/api/
# (可选)ui 的工作路径,只有开启 ui 管理功能后才有效
HASOR_DATAQL_DATAWAY_UI_URL=/admin/

数据库

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);

interface_release是发布版本的接口信息,可以用来回滚.interface_info是存储接口信息,包括修改或者垃圾箱里等

运行:

启动main方法运行,

启动日志里面包含下面的语句
...
dataway api workAt /api/
...
...
dataway admin workAt /admin/
...

我们打开浏览器输入localhost:8080/admin/就可以看到控制台

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值