永不生鏽的螺絲釘!一款簡潔好用的數據庫表結構文檔生成器

是個陳序員 2024-04-07 14:22:42

大家好,我是 Java陳序員。

在企業級開發中,我們經常會有編寫數據庫表結構文檔的需求,常常需要手寫維護文檔,很是繁瑣。

今天,給大家介紹一款數據庫表結構文檔生成工具。

關注微信公衆號:【Java陳序員】,獲取開源項目分享、AI副業分享、超200本經典計算機電子書籍等。

項目介紹

screw —— 螺絲釘(代表企業級開發中一顆永不生鏽的螺絲釘),是一款簡潔好用的數據庫表結構文檔生成工具。

screw 主打簡潔、輕量,支持多種數據庫、多種格式文檔,可自定義模板進行靈活拓展。

支持 MySQL、MariaDB、TIDB、Oracle 多種數據庫。

支持生成 HTML、Word、MarkDown 三種格式的文檔。

快速上手

screw 有普通方式、Maven 插件的兩種方式來生成文檔。

普通方式

1、引入依賴

<!-- 引入數據庫驅動,這裏以 MySQL 爲例 --><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version></dependency><!-- 引入 screw --><dependency> <groupId>cn.smallbun.screw</groupId> <artifactId>screw-core</artifactId> <version>1.0.5</version> </dependency>

2、編寫代碼

public DocumentGeneration { /** * 文檔生成 */ @Test public void documentGeneration() { // 文檔生成路徑 String fileOutputPath = "D:\\database"; // 數據源 HikariConfig hikariConfig = new HikariConfig(); // 指定數據庫驅動 hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver"); // 設置數據庫連接地址 hikariConfig.setJdbcUrl("jdbc:mysql://localhost:3306/database"); // 設置數據庫用戶 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(fileOutputPath) // 打開目錄 .openOutputDir(true) // 文件類型 HTML、WORD、MD 三種類型 .fileType(EngineFileType.HTML) // 生成模板實現 .produceType(EngineTemplateType.freemarker) // 自定義文件名稱 .fileName("Document") .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(); }}

3、執行代碼輸出文檔

Maven 插件

1、引入依賴

<build> <plugins> <plugin> <groupId>cn.smallbun.screw</groupId> <artifactId>screw-maven-plugin</artifactId> <version>1.0.5</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>root</username> <!-- 數據庫密碼 --> <password>password</password> <!-- 數據庫驅動 --> <driverClassName>com.mysql.cj.jdbc.Driver</driverClassName> <!-- 數據庫連接地址 --> <jdbcUrl>jdbc:mysql://127.0.0.1:3306/xxxx</jdbcUrl> <!-- 生成的文件類型 HTML、WORD、MD 三種類型 --> <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>

2、執行插件

3、使用 Maven 插件執行的方式會將文檔輸出到項目根目錄的 doc 目錄下

文檔截圖

HTML 類型文檔

Word 類型文檔

MarkDown 類型文檔

自從用了 screw 後,編寫數據庫文檔信息就很方便了,一鍵生成,剩下的時間就可以用來摸魚了~

大家如果下次有需要編寫數據庫文檔,可以考慮使用 screw ,建議先把本文收藏起來,下次就不會找不到了~

最後,貼上項目地址:

https://github.com/pingfangushi/screw最後

推薦的開源項目已經收錄到 GitHub 項目,歡迎 Star:

https://github.com/chenyl8848/great-open-source-project

或者訪問網站,進行在線浏覽:

https://chencoding.top:8090/#/

大家的點贊、收藏和評論都是對作者的支持,如文章對你有幫助還請點贊轉發支持下,謝謝!

0 阅读:35

是個陳序員

簡介:感謝大家的關注