Hasor简介

Dataway让SpringBoot不在需要Controller、Service、DAO、Mapper就可以实现接口并CURD数据。

面向生产环境而设计的 Java 应用开发框架。它的核心设计目标是提供一个简单的交互接口给开发者,开发者可以在此基础上灵活的构建自己的应用程序。无论是应用类程序还是框架类工具,Hasor 都会给予你最有力的支持。区别于其它框架的是 Hasor 有着自己一套完整的扩展体系。无论您是一般的应用工程,还是开发工具框架类项目。Hasor都会是一个强有力的基石。

简单来说,就是不需要写代码,只需要数据库稍加配置就可以实现接口,用来做报表类的蛮好的。感觉用了这个东西,简单的功能就不需要后端了,前端会一点Sql就可以做了。废话不多讲,开始集成到SpringBoot项目中!

代码配置

依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<!--parent-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath>/</relativePath>
</parent>
<dependencies>
<!--spring web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--hasor 与spring连接-->
<dependency>
<groupId>net.hasor</groupId>
<artifactId>hasor-spring</artifactId>
<version>4.1.8</version><!-- 查看最新版本:https://mvnrepository.com/artifact/net.hasor/hasor-spring -->
</dependency>
<!--hasor dataway-->
<dependency>
<groupId>net.hasor</groupId>
<artifactId>hasor-dataway</artifactId>
<version>4.1.8</version><!-- 查看最新版本:https://mvnrepository.com/artifact/net.hasor/hasor-dataway -->
</dependency>
<!--spring-boot-starter-jdbc自动配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--Mysql 连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
</dependency>
<!--简化代码-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 启用 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=/interface-ui/

# 数据库配置
spring.datasource.url=jdbc:mysql://192.168.3.30:3306/hasor?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

启动类

1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableHasor() // 在Spring 中启用 Hasor
@EnableHasorWeb() // 将 hasor-web 配置到 Spring 环境中,Dataway 的 UI 是通过 hasor-web 提供服务。
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}

Hasor Module

Module 是使用 Hasor 的统一入口,它的地位类似于 java 的 main 方法。

1
2
3
4
5
6
7
8
9
10
@DimModule // 标记Mode
@Component // 注入Spring
public class HasorMode implements Module {
@Autowired
private DataSource dataSource; // 使用数据库连接
@Override
public void loadModule(ApiBinder apiBinder) throws Throwable {
apiBinder.installModule(new JdbcModule(Level.Full,dataSource));
}
}

页面配置

访问http://localhost:8080/interface-ui/#/ 可以看到接口管理页面

添加一个接口,如图配置,下附代码,以供复制(这个接口只是为了演示并测试功能,并非必要信息

image-20200530100254752

代码逻辑区

1
2
3
4
5
6
7
8
// 使用 DataQL 拼接字符串
var orderBy = ${orderField} + " " + ${orderType};
// 声明一个可以注入的 SQL
var dataSet = @@sql(apiType,orderString) <%
select * from interface_info where api_type = #{apiType} order by ${orderString} ;
%>
// 执行这个 SQL,并返回结果
return dataSet(${apiType}, orderBy);

参数区

1
2
3
4
5
{
"apiType": "DataQL",
"orderField":"api_type",
"orderType":"desc"
}

备注

  • 接口发布后才能访问

附录