讓SpringBoot不需要Controller、Service、DAO、Mapper,臥槽!這款工具絕了!
往期熱門文章:
1、Spring Data JPA 和 MyBatis 誰(shuí)更強(qiáng)? 2、Nginx一網(wǎng)打盡:動(dòng)靜分離、壓縮、緩存、黑白名單、跨域、高可用、性能優(yōu)化 3、一個(gè) SpringBoot 項(xiàng)目能處理多少請(qǐng)求? 4、使用 try-catch 捕獲異常真的會(huì)影響性能? 5、圖文詳解 Java 泛型,寫得太好了!
第一步:引入相關(guān)依賴
<dependency>
<groupId>net.hasor</groupId>
<artifactId>hasor-spring</artifactId>
<version>4.1.3</version>
</dependency>
<dependency>
<groupId>net.hasor</groupId>
<artifactId>hasor-dataway</artifactId>
<version>4.1.3-fix20200414</version><!-- 4.1.3 包存在UI資源缺失問題 -->
</dependency>
第二步:配置 Dataway,并初始化數(shù)據(jù)表
# 是否啟用 Dataway 功能(必選:默認(rèn)false)
HASOR_DATAQL_DATAWAY=true
# 是否開啟 Dataway 后臺(tái)管理界面(必選:默認(rèn)false)
HASOR_DATAQL_DATAWAY_ADMIN=true
# dataway API工作路徑(可選,默認(rèn):/api/)
HASOR_DATAQL_DATAWAY_API_URL=/api/
# dataway-ui 的工作路徑(可選,默認(rèn):/interface-ui/)
HASOR_DATAQL_DATAWAY_UI_URL=/interface-ui/
# SQL執(zhí)行器方言設(shè)置(可選,建議設(shè)置)
HASOR_DATAQL_FX_PAGE_DIALECT=mysql
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 '狀態(tài):0草稿,1發(fā)布,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 '接口的請(qǐng)求/響應(yīng)數(shù)據(jù)結(jié)構(gòu)',
`api_sample` mediumtext NULL COMMENT '請(qǐng)求/響應(yīng)/請(qǐng)求頭樣本數(shù)據(jù)',
`api_create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '創(chuàng)建時(shí)間',
`api_gmt_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '修改時(shí)間',
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 '狀態(tài):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 '原始查詢腳本,僅當(dāng)類型為SQL時(shí)不同',
`pub_schema` mediumtext NULL COMMENT '接口的請(qǐng)求/響應(yīng)數(shù)據(jù)結(jié)構(gòu)',
`pub_sample` mediumtext NULL COMMENT '請(qǐng)求/響應(yīng)/請(qǐng)求頭樣本數(shù)據(jù)',
`pub_release_time`datetime DEFAULT CURRENT_TIMESTAMP COMMENT '發(fā)布時(shí)間(下線不更新)',
PRIMARY KEY (`pub_id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COMMENT='Dataway API 發(fā)布?xì)v史。';
create index idx_interface_release on interface_release (pub_api_id);
第三步:配置數(shù)據(jù)源
<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>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
# db
spring.datasource.url=jdbc:mysql://xxxxxxx:3306/example
spring.datasource.username=xxxxx
spring.datasource.password=xxxxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type:com.alibaba.druid.pool.DruidDataSource
# 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
第四步:把數(shù)據(jù)源設(shè)置到 Hasor 容器中
@DimModule
@Component
public class ExampleModule implements SpringModule {
@Autowired
private DataSource dataSource = null;
@Override
public void loadModule(ApiBinder apiBinder) throws Throwable {
// .DataSource form Spring boot into Hasor
apiBinder.installModule(new JdbcModule(Level.Full, this.dataSource));
}
}
第五步:在SprintBoot 中啟用 Hasor
@EnableHasor()
@EnableHasorWeb()
@SpringBootApplication(scanBasePackages = { "net.example.hasor" })
public class ExampleApplication {
public static void main(String[] args) {
SpringApplication.run(ExampleApplication.class, args);
}
}
第六步:?jiǎn)?dòng)應(yīng)用
_ _ ____ _
| | | | | _ \ | |
| |__| | __ _ ___ ___ _ __ | |_) | ___ ___ | |_
| __ |/ _` / __|/ _ \| '__| | _ < / _ \ / _ \| __|
| | | | (_| \__ \ (_) | | | |_) | (_) | (_) | |_
|_| |_|\__,_|___/\___/|_| |____/ \___/ \___/ \__|
2020-04-14 13:52:59.696 [main] INFO n.h.core.context.TemplateAppContext - loadModule class net.hasor.dataway.config.DatawayModule
2020-04-14 13:52:59.697 [main] INFO n.hasor.dataway.config.DatawayModule - dataway api workAt /api/
2020-04-14 13:52:59.697 [main] INFO n.h.c.e.AbstractEnvironment - var -> HASOR_DATAQL_DATAWAY_API_URL = /api/.
2020-04-14 13:52:59.704 [main] INFO n.hasor.dataway.config.DatawayModule - dataway admin workAt /interface-ui/
2020-04-14 13:52:59.716 [main] INFO net.hasor.core.binder.ApiBinderWrap - mapingTo[901d38f22faa419a8593bb349905ed0e] -> bindType ‘class net.hasor.dataway.web.ApiDetailController’ mappingTo: ‘[/interface-ui/api/api-detail]’.
2020-04-14 13:52:59.716 [main] INFO net.hasor.core.binder.ApiBinderWrap - mapingTo[c6eb9f3b3d4c4c8d8a4f807435538172] -> bindType ‘class net.hasor.dataway.web.ApiHistoryListController’ mappingTo: ‘[/interface-ui/api/api-history]’.
2020-04-14 13:52:59.717 [main] INFO net.hasor.core.binder.ApiBinderWrap - mapingTo[eb841dc72ad54023957233ef602c4327] -> bindType ‘class net.hasor.dataway.web.ApiInfoController’ mappingTo: ‘[/interface-ui/api/api-info]’.
2020-04-14 13:52:59.717 [main] INFO net.hasor.core.binder.ApiBinderWrap - mapingTo[96aebb46265245459ae21d558e530921] -> bindType ‘class net.hasor.dataway.web.ApiListController’ mappingTo: ‘[/interface-ui/api/api-list]’.
2020-04-14 13:52:59.718 [main] INFO net.hasor.core.binder.ApiBinderWrap - mapingTo[7467c07f160244df8f228321f6262d3d] -> bindType ‘class net.hasor.dataway.web.ApiHistoryGetController’ mappingTo: ‘[/interface-ui/api/get-history]’.
2020-04-14 13:52:59.719 [main] INFO net.hasor.core.binder.ApiBinderWrap - mapingTo[97d8da5363c741ba99d87c073a344412] -> bindType ‘class net.hasor.dataway.web.DisableController’ mappingTo: ‘[/interface-ui/api/disable]’.
2020-04-14 13:52:59.720 [main] INFO net.hasor.core.binder.ApiBinderWrap - mapingTo[8ddc3316ef2642dfa4395ca8ac0fff04] -> bindType ‘class net.hasor.dataway.web.SmokeController’ mappingTo: ‘[/interface-ui/api/smoke]’.
2020-04-14 13:52:59.720 [main] INFO net.hasor.core.binder.ApiBinderWrap - mapingTo[cc06c5fb343b471aacedc58fb2fe7bf8] -> bindType ‘class net.hasor.dataway.web.SaveApiController’ mappingTo: ‘[/interface-ui/api/save-api]’.
2020-04-14 13:52:59.720 [main] INFO net.hasor.core.binder.ApiBinderWrap - mapingTo[7915b2b1f89a4e73891edab0264c9bd4] -> bindType ‘class net.hasor.dataway.web.PublishController’ mappingTo: ‘[/interface-ui/api/publish]’.
2020-04-14 13:52:59.721 [main] INFO net.hasor.core.binder.ApiBinderWrap - mapingTo[0cfa34586455414591bdc389bff23ccb] -> bindType ‘class net.hasor.dataway.web.PerformController’ mappingTo: ‘[/interface-ui/api/perform]’.
2020-04-14 13:52:59.721 [main] INFO net.hasor.core.binder.ApiBinderWrap - mapingTo[37fe4af3e2994acb8deb72d21f02217c] -> bindType ‘class net.hasor.dataway.web.DeleteController’ mappingTo: ‘[/interface-ui/api/delete]’.
第七步:訪問接口管理頁(yè)面進(jìn)行接口配置
第八步:新建一個(gè)接口
var query = @@sql()<%
select * from interface_info
%>
return query()
最后總結(jié)
Dataway 官方手冊(cè):https://www.hasor.net/web/dataway/about.html
Dataway 在 OSC 上的項(xiàng)目地址,歡迎收藏:
https://www.oschina.net/p/dataway
DataQL 手冊(cè)地址:https://www.hasor.net/web/dataql/what_is_dataql.html
Hasor 項(xiàng)目的首頁(yè):https://www.hasor.net/web/index.html
往期熱門文章:
1、面試官:Spring 中的 Service 有多個(gè)實(shí)現(xiàn)類,怎么注入? 2、大公司為什么禁止在 Spring Boot 項(xiàng)目中使用 @Autowired 注解? 3、重磅!IDEA 版 Postman 新版發(fā)布,太炸了! 4、京東又開源一款新框架,用起來真優(yōu)雅! 5、面試官:Spring 中的 Service 有多個(gè)實(shí)現(xiàn)類,怎么注入? 6、SpringBoot 生產(chǎn)中 16 條最佳實(shí)踐 7、CTO 說,禁用使用 kill -9 關(guān)閉程序! 8、Spring Boot自帶的工具類,太好用了! 9、現(xiàn)如今上海還有哪些牛B的互聯(lián)網(wǎng)公司? 10、告別混亂代碼:SpringBoot 后端接口規(guī)范
評(píng)論
圖片
表情
