資料庫界的Swagger:一鍵生成資料庫文件!
阿新 • • 發佈:2022-05-06
對於開發的API文件,我們可以通過Swagger等工具來自動生成了。但是對於資料庫表結構的文件呢,在實際開發中在開發前我們一般會先設計好表結構,大家討論一下,
這個時候就很需要有個資料庫表結構的文件,如果常規操作就是一通無腦的 CV 大法,產出一份小几十頁的 Word 文件,這樣不僅容易出錯,而且如果表結構變了還需修改
Word文件,非常不方便。
這裡介紹並演示一個開源的 生成資料庫表結構的文件 的工具,可以幫我們高效的自動 生成資料庫表結構文件。
工具名稱: screw
。
開源地址: https://gitee.com/leshalv/screw。
有興趣的朋友可以研究下原始碼,沒興趣就直接拿來用就好了。
一、 screw 簡介
screw 是一個簡潔好用的資料庫表結構文件的生成工具 ,支援 MySQL、Oracle、PostgreSQL 等主流的關係資料庫。
生成的文件有 HTML、Word、Markdown 三種格式 ,示例如下圖所示:
1、Html演示效果
2、Word演示效果
3、Markdown演示效果
看完演示效果,我們接下來看看如果生成 資料庫文件,其實非常簡單,我們下面示範下。
二、快速入門
screw 有兩種方式 來生成文件,通過 Java程式碼 或者 Maven外掛 。
下面,我們來分別快速入門下。
1、使用 Java 程式碼的方式
1、引入pom檔案
<dependencies> <dependency> <groupId>cn.smallbun.screw</groupId> <artifactId>screw-core</artifactId> <version>1.0.5</version> </dependency> <!-- 資料庫連線 --> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency> </dependencies>
2、實現工具類
public class ScrewUtil { public static void main(String[] args) { ScrewUtil.documentGeneration(); } /** * 文件生成 */ static void documentGeneration() { //資料來源 HikariConfig hikariConfig = new HikariConfig(); hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver"); hikariConfig.setJdbcUrl("jdbc:mysql://localhost:3306/mall_order"); hikariConfig.setUsername("root"); hikariConfig.setPassword("root"); //設定可以獲取tables remarks資訊 hikariConfig.addDataSourceProperty("useInformationSchema", "true"); hikariConfig.setMinimumIdle(2); hikariConfig.setMaximumPoolSize(5); DataSource dataSource = new HikariDataSource(hikariConfig); //生成配置 EngineConfig engineConfig = EngineConfig.builder() //生成檔案路徑 .fileOutputDir("你的生成檔案地址") //開啟目錄 .openOutputDir(true) //檔案型別 .fileType(EngineFileType.HTML) //生成模板實現 .produceType(EngineTemplateType.freemarker) //自定義檔名稱 .fileName("商品資料庫表結構").build(); //忽略表 ArrayList<String> ignoreTableName = new ArrayList<>(); ignoreTableName.add("test_user"); ignoreTableName.add("test_group"); //忽略表字首 ArrayList<String> ignorePrefix = new ArrayList<>(); ignorePrefix.add("test_"); //忽略表字尾 ArrayList<String> ignoreSuffix = new ArrayList<>(); ignoreSuffix.add("_test"); ProcessConfig processConfig = ProcessConfig.builder() //指定生成邏輯、當存在指定表、指定表字首、指定表字尾時,將生成指定表,其餘表不生成、並跳過忽略表配置 //根據名稱指定表生成 .designatedTableName(new ArrayList<>()) //根據表字首生成 .designatedTablePrefix(new ArrayList<>()) //根據表字尾生成 .designatedTableSuffix(new ArrayList<>()) //忽略表名 .ignoreTableName(ignoreTableName) //忽略表字首 .ignoreTablePrefix(ignorePrefix) //忽略表字尾 .ignoreTableSuffix(ignoreSuffix).build(); //配置 Configuration config = Configuration.builder() //版本 .version("1.0.0") //描述 .description("商品資料庫設計文件") //資料來源 .dataSource(dataSource) //生成配置 .engineConfig(engineConfig) //生成配置 .produceConfig(processConfig) .build(); //執行生成 new DocumentationExecute(config).execute(); } }
2、Maven外掛方式
pom檔案新增外掛
<build>
<plugins>
<plugin>
<groupId>cn.smallbun.screw</groupId>
<artifactId>screw-maven-plugin</artifactId>
<version>${lastVersion}</version>
<dependencies>
<!-- HikariCP -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
<!--mysql driver-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
</dependencies>
<configuration>
<!--username-->
<username>root</username>
<!--password-->
<password>password</password>
<!--driver-->
<driverClassName>com.mysql.cj.jdbc.Driver</driverClassName>
<!--jdbc url-->
<jdbcUrl>jdbc:mysql://127.0.0.1:3306/xxxx</jdbcUrl>
<!--生成檔案型別-->
<fileType>HTML</fileType>
<!--開啟檔案輸出目錄-->
<openOutputDir>false</openOutputDir>
<!--生成模板-->
<produceType>freemarker</produceType>
<!--文件名稱 為空時:將採用[資料庫名稱-描述-版本號]作為文件名稱-->
<fileName>測試文件名稱</fileName>
<!--描述-->
<description>資料庫文件生成</description>
<!--版本-->
<version>${project.version}</version>
<!--標題-->
<title>資料庫文件</title>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
執行外掛
執行成功後,我們看到在當前專案中已經存在了,資料庫文件。
補充
有關screw工具的補充,可以檢視原始碼: https://gitee.com/leshalv/screw。
自己也把該篇文章的演示程式碼放到了github上,具體地址: https://github.com/yudiandemingzi/spring-boot-study