SpringBoot实践(三):SpringBoot集成Dataway数据查询接口

当前的项目使用Vue+Dataway+springcloud构建, Dataway是一个尚未开采的宝藏,它基于 DataQL提供服务聚合能力,DataQL 的设计初衷是数据的聚合和转换以及过程中的简单加工,使用者无需开发任何代码(例如:Mapper、BO、VO、DO、DAO、Service、Controller )就能配置满足需求的接口;

DataWay的主打场景应该是在数据展示和涉及频繁地取数据查数据,大部分项目都可以在无侵入的情况下直接应用 Dataway,进而改进老项目的迭代效率,并且Dataway提供一站式 UI 界面;

一、简介

- Dataway 官方手册:https://www.hasor.net/web/dataway/about.html
- Dataway 在 OSC 上的项目地址,欢迎收藏:https://www.oschina.net/p/dataway
- DataQL 手册地址:https://www.hasor.net/web/dataql/what_is_dataql.html
- Hasor 项目的首页:https://www.hasor.net/web/index.html

根据持久化类与表的映射关系,对象关系映射(Object Relational Mapping,简称ORM框架)在运行时把对象持久化到数据库中,以实现业务和数据的逻辑交互;ORM 框架最大的特点是具有 Mapping 过程,然后通过框架在进行 CURD 操作。 例如:Mybatis、Hibernate;

Dataway 是 Hasor 生态中的一员,它的架构极其简单,没有 ORM 框架中最关键的 Mapping 过程,相当于给 DataQL 披了一张皮,DataQL 不是新的编程语言而是查询语言,通过提供直观、灵活的语法来描述客户端应用程序的数据需求和交互,DataQL的语法手册参考:DataQL语法手册

二、集成

参考:springboot+mysql+durid+dataway集成,这里只是测试,使用h2数据库,不用durid连接池;

1、idea新建springboot工程,引入maven依赖,hasor-spring 负责 Spring 和 Hasor 框架之间的整合,hasor-dataway依赖就可以使用;hasor.version是4.1.13

<dependency>
    <groupId>net.hasor</groupId>
    <artifactId>hasor-spring</artifactId>
    <version>${hasor.version}</version>
</dependency>
<dependency>
    <groupId>net.hasor</groupId>
    <artifactId>hasor-dataway</artifactId>
    <version>${hasor.version}</version>
</dependency>

2、修改springboot的application.properties配置,增加对于dataway的配置,其中 HASOR_DATAQL_DATAWAYHASOR_DATAQL_DATAWAY_ADMIN 两个配置是必须要打开的,默认情况下 Datawaty 是不启用的。

spring.datasource.ds1.url=jdbc:h2:~/dataway
spring.datasource.ds1.username=root
spring.datasource.ds1.password=123456
spring.datasource.ds1.driver-class-name=org.h2.Driver


# 是否启用 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

3、建表:Dataway 需要2个数据表(默认mysql,我这里使用idea自带的h2):interface_info和interface_release;

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

4、Spring Boot 和 Hasor 是两个独立的容器框架,需要把 Spring 中的数据源设置到 Hasor 中;

① 新建configuration包,然后创建HasorModule配置类(Hasor模块),Hasor 启动的时候会调用 复写的loadModule 方法,在这里再把 DataSource 设置到 Hasor 中;

② 新建DataSourceConfig配置类,兼容多个数据源;

package com.example.dataway.configuration;
import net.hasor.core.ApiBinder;
import net.hasor.core.DimModule;
import net.hasor.db.JdbcModule;
import net.hasor.db.Level;
import net.hasor.spring.SpringModule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;

@DimModule
@Component
public class 	HasorModule implements SpringModule {

	@Autowired
	//@Qualifier("ds1DataSource")
	private DataSource ds1DataSource = null;
	
	@Override
	public void loadModule(ApiBinder apiBinder) throws Throwable {
		// .DataSource form Spring boot into Hasor
		apiBinder.installModule(new JdbcModule(Level.Full, this.ds1DataSource));
		apiBinder.installModule(new JdbcModule(Level.Full, "ds1", this.ds1DataSource));
		//apiBinder.installModule(new JdbcModule(Level.Full, "ds2", this.ds2DataSource));
	}

}

package com.example.dataway.configuration;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
// @Description 多数据源配置
@Configuration
public class DataSourceConfig {
	// 主数据源 ds1数据源
	@Primary
	@Bean(name = "ds1DataSourceProperties")
	@ConfigurationProperties(prefix = "spring.datasource.ds1")
	public DataSourceProperties ds1DataSourceProperties() {
		return new DataSourceProperties();
	}

	@Primary
	@Bean(name = "ds1DataSource")
	public DataSource ds1DataSource(@Qualifier("ds1DataSourceProperties") DataSourceProperties dataSourceProperties) {
		return dataSourceProperties.initializeDataSourceBuilder().build();
	}
//
//	// 第二个ds2数据源配置
//	@Bean(name = "ds2DataSourceProperties")
//	@ConfigurationProperties(prefix = "spring.datasource.ds2")
//	public DataSourceProperties ds2DataSourceProperties() {
//		return new DataSourceProperties();
//	}
//
//	@Bean("ds2DataSource")
//	public DataSource ds2DataSource(@Qualifier("ds2DataSourceProperties") DataSourceProperties dataSourceProperties) {
//		return dataSourceProperties.initializeDataSourceBuilder().build();
//	}

}

5、在springboot的主类上启用hasor,启动;

package com.example.dataway;
import net.hasor.spring.boot.EnableHasor;
import net.hasor.spring.boot.EnableHasorWeb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableHasor
@EnableHasorWeb
@SpringBootApplication
public class DatawayApplication {

    public static void main(String[] args) {
        SpringApplication.run(DatawayApplication.class, args);
    }
}

6、使用;

猜你喜欢

转载自blog.csdn.net/yezonggang/article/details/110287175