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> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.0.RELEASE</version> <relativePath>/</relativePath> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>net.hasor</groupId> <artifactId>hasor-spring</artifactId> <version>4.1.8</version> </dependency> <dependency> <groupId>net.hasor</groupId> <artifactId>hasor-dataway</artifactId> <version>4.1.8</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </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
| HASOR_DATAQL_DATAWAY=true
HASOR_DATAQL_DATAWAY_ADMIN=true
HASOR_DATAQL_DATAWAY_API_URL=/api/
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() @EnableHasorWeb() 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/#/ 可以看到接口管理页面
添加一个接口,如图配置,下附代码,以供复制(这个接口只是为了演示并测试功能,并非必要信息)

代码逻辑区
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" }
|
备注
附录