1. 程式人生 > >實用!一鍵生成資料庫文件,堪稱資料庫界的Swagger

實用!一鍵生成資料庫文件,堪稱資料庫界的Swagger

>本文收錄在個人部落格:[www.chengxy-nds.top](http://www.chengxy-nds.top),技術資料共享,同進步 最近部門訂單業務調整,收攏其他業務線的下單入口,做個統一大訂單平臺。需要梳理各業務線的資料表,但每個業務線庫都有近百張和訂單相關的表,挨個表一個一個欄位的弄腦瓜子嗡嗡的。 為了不重複 CV 操作,抱著一絲希望開始在`GitHub`裡找,看看有沒有什麼工具可以用,結果就真的發現了寶藏,`screw`(螺絲釘),居然可以生成資料庫文件,優秀啊~。 ![資料庫文件圖](https://img-blog.csdnimg.cn/20200802211105463.png) #### 一、資料庫支援 - [x] MySQL - [x] MariaDB - [x] TIDB - [x] Oracle - [x] SqlServer - [x] PostgreSQL - [x] Cache DB #### 二、配置 #### 1、pom檔案 引入`screw`核心包,`HikariCP`資料庫連線池,`HikariCP`號稱效能最出色的資料庫連線池。 ```javascript cn.smallbun.screw
screw-core 1.0.3
com.zaxxer HikariCP 3.4.5 mysql mysql-connector-java 8.0.20 ``` #### 2、配置資料來源 配置資料來源,設定 `useInformationSchema` 可以獲取`tables`註釋資訊。 ```javascript spring.datasource.url=jdbc:mysql://45.93.1.5:3306/fire?useUnicode=true&characterEncoding=UTF-8&useSSL=false spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.xa.properties.useInformationSchema=true ``` #### 3、screw 核心配置 `screw`有兩種執行方式,第一種是`pom`檔案配置,另一種是程式碼執行。 ```javascript org.springframework.boot
spring-boot-maven-plugin
cn.smallbun.screw screw-maven-plugin 1.0.3 com.zaxxer HikariCP 3.4.5 mysql
mysql-connector-java 8.0.20
root 123456 com.mysql.cj.jdbc.Driver jdbc:mysql://41.92.6.5:3306/fire HTML false freemarker 資料庫文件生成 ${project.version} fire資料庫文件 compile run
``` 配置完以後在 `maven project`->`screw` 雙擊執行ok。 ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200802211843999.png) 程式碼生成方式也非常簡單。 ```javascript @SpringBootTest public class ScrewApplicationTests { @Autowired ApplicationContext applicationContext; @Test void contextLoads() { DataSource dataSourceMysql = applicationContext.getBean(DataSource.class); // 生成檔案配置 EngineConfig engineConfig = EngineConfig.builder() // 生成檔案路徑,自己mac本地的地址,這裡需要自己更換下路徑 .fileOutputDir("D:/") // 開啟目錄 .openOutputDir(false) // 檔案型別 .fileType(EngineFileType.HTML) // 生成模板實現 .produceType(EngineTemplateType.freemarker).build(); // 生成文件配置(包含以下自定義版本號、描述等配置連線) Configuration config = Configuration.builder() .version("1.0.3") .description("生成文件資訊描述") .dataSource(dataSourceMysql) .engineConfig(engineConfig) .produceConfig(getProcessConfig()) .build(); // 執行生成 new DocumentationExecute(config).execute(); } /** * 配置想要生成的表+ 配置想要忽略的表 * * @return 生成表配置 */ public static ProcessConfig getProcessConfig() { // 忽略表名 List ignoreTableName = Arrays.asList("a", "test_group"); // 忽略表字首,如忽略a開頭的資料庫表 List ignorePrefix = Arrays.asList("a", "t"); // 忽略表字尾 List ignoreSuffix = Arrays.asList("_test", "czb_"); return ProcessConfig.builder() //根據名稱指定表生成 .designatedTableName(Arrays.asList("fire_user")) //根據表字首生成 .designatedTablePrefix(new ArrayList<>()) //根據表字尾生成 .designatedTableSuffix(new ArrayList<>()) //忽略表名 .ignoreTableName(ignoreTableName) //忽略表字首 .ignoreTablePrefix(ignorePrefix) //忽略表字尾 .ignoreTableSuffix(ignoreSuffix).build(); } } ``` #### 4、文件格式 `screw` 有 `HTML`、`DOC`、`MD` 三種格式的文件。 程式碼中的修改 ```javascript .fileType(EngineFileType.HTML) ``` 或者`pom`檔案 ```javascript MD ``` `DOC`文件樣式 ![work文件](https://img-blog.csdnimg.cn/20200802212736806.png) `HTML`文件樣式 ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200802212834219.png) `MD`文件樣式 ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/2020080221325983.png) 不得不說這個工具是真TM好用,提前完成任務,有點傲嬌有木有! ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20200802190829629.png) **原創不易,燃燒秀髮輸出內容,如果有一丟丟收穫,點個贊鼓勵一下吧!** 整理了幾百本各類技術電子書,送給小夥伴們。關注公號回覆【666】自行領取。和一些小夥伴們建了一個技術交流群,一起探討技術、分享技術資料,旨在共同學習進步,如果感興趣就加入我們吧! ![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91c2VyLWdvbGQtY2RuLnhpdHUuaW8vMjAyMC8yLzQvMTcwMGU0Mjk1MDQzMjQ0Yg?x-oss-process=image/form