天天看點

[開源]簡潔好用的資料庫表結構文檔生成工具,支援多種主流資料庫

作者:一飛開源
一飛開源,介紹創意、新奇、有趣、實用的免費開源應用、系統、軟體、硬體及技術,一個探索、發現、分享、使用與互動交流的開源技術社群平台。緻力于打造活力開源社群,共建開源新生态!

一、開源項目簡介

[開源]簡潔好用的資料庫表結構文檔生成工具,支援多種主流資料庫

企業級開發過程中,一顆永不生鏽的螺絲釘。

screw (螺絲釘) 英:[skruː] ~ 簡潔好用的資料庫表結構文檔生成工具

二、開源協定

使用LGPL-3.0開源協定

三、界面展示

文檔截圖

  • html
[開源]簡潔好用的資料庫表結構文檔生成工具,支援多種主流資料庫
[開源]簡潔好用的資料庫表結構文檔生成工具,支援多種主流資料庫
  • word
[開源]簡潔好用的資料庫表結構文檔生成工具,支援多種主流資料庫
  • markdwon
[開源]簡潔好用的資料庫表結構文檔生成工具,支援多種主流資料庫
[開源]簡潔好用的資料庫表結構文檔生成工具,支援多種主流資料庫

四、功能概述

作者自述

在企業級開發中、我們經常會有編寫資料庫表結構文檔的時間付出,從業以來,待過幾家企業,關于資料庫表結構文檔狀态:要麼沒有、要麼有、但都是手寫、後期運維開發,需要手動進行維護到文檔中,很是繁瑣、如果忘記一次維護、就會給以後工作造成很多困擾、無形中制造了很多坑留給自己和後人,于是萌生了要自己寫一個插件工具的想法,但由于自己前期在程式設計上沒有很多造詣,且能力偏低,有想法并不能很好實作,随着工作閱曆的增加,和知識的不斷儲備,終于在2020年的3月中旬開始進行編寫,4月上旬完成初版,想完善差不多在開源,但由于工作太忙,業餘時間不足,沒有在進行完善,到了6月份由于工作原因、頻繁設計和更改資料庫、經常使用自己寫的此插件、節省了很多時間,解決了很多問題 ,在僅有且不多的業餘時間中、進行開源準備,于2020年6月22日,開源,歡迎大家使用、建議、并貢獻。

  關于名字,想一個太難了,好在我這個聰明的小腦瓜靈感一現,怎麼突出它的小,但重要呢?從小就學過雷鋒的螺絲釘精神,摘自雷鋒日記:雖然是細小的螺絲釘,是個細微的小齒輪,然而如果缺了它,那整個的機器就無法運轉了,慢說是缺了它,即使是一枚小螺絲釘沒擰緊,一個小齒輪略有破損,也要使機器的運轉發生故障的...,感覺自己寫的這個工具,很有這意味,雖然很小、但是開發中缺了它還不行,于是便起名為screw(螺絲釘)。

特點

  • 簡潔、輕量、設計良好
  • 多資料庫支援
  • 多種格式文檔
  • 靈活擴充
  • 支援自定義模闆

資料庫支援

  • MySQL
  • MariaDB
  • TIDB
  • Oracle
  • SqlServer
  • PostgreSQL
  • Cache DB(2016)
  • H2 (開發中)
  • DB2 (開發中)
  • HSQL (開發中)
  • SQLite(開發中)
  • 瀚高(開發中)
  • 達夢 (開發中)
  • 虛谷 (開發中)
  • 人大金倉(開發中)

文檔生成支援

  • html
  • word
  • markdown

五、技術選型

使用方式

普通方式

  • 引入依賴
<dependency>
    <groupId>cn.smallbun.screw</groupId>
    <artifactId>screw-core</artifactId>
    <version>${lastVersion}</version>
 </dependency>           
  • 編寫代碼
/**
 * 文檔生成
 */
void documentGeneration() {
   //資料源
   HikariConfig hikariConfig = new HikariConfig();
   hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
   hikariConfig.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/database");
   hikariConfig.setUsername("root");
   hikariConfig.setPassword("password");
   //設定可以擷取tables remarks資訊
   hikariConfig.addDataSourceProperty("useInformationSchema", "true");
   hikariConfig.setMinimumIdle(2);
   hikariConfig.setMaximumPoolSize(5);
   DataSource dataSource = new HikariDataSource(hikariConfig);
   //生成配置
   EngineConfig engineConfig = EngineConfig.builder()
         //生成檔案路徑
         .fileOutputDir(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();
}           

Maven 插件

<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>           

擴充子產品

pojo生成功能

功能簡介

  pojo生成功能是基于screw延伸出的擴充子產品,目前處于初步開發的狀态。在日常的開發中,經過需求分析、模組化之後,往往會先在資料庫中建表,其次在進行代碼的開發。那麼pojo生成功能在這個階段就可以幫助大家節省一些重複勞動了。使用pojo生成功能可以直接根據資料庫生成對應的java pojo對象。這樣後續的修改,開發都會很友善。

資料庫支援

  • MySQL

使用方式

  • 引入依賴
<dependency>
    <groupId>cn.smallbun.screw</groupId>
    <artifactId>screw-extension</artifactId>
    <version>${lastVersion}</version>
 </dependency>           
  • 編寫代碼
/**
 * pojo生成
 */
void pojoGeneration() {
    //資料源
    HikariConfig hikariConfig = new HikariConfig();
    hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
    hikariConfig.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/screw");
    hikariConfig.setUsername("screw");
    hikariConfig.setPassword("screw");
    //設定可以擷取tables remarks資訊
    hikariConfig.addDataSourceProperty("useInformationSchema", "true");
    hikariConfig.setMinimumIdle(2);
    hikariConfig.setMaximumPoolSize(5);
    DataSource dataSource = new HikariDataSource(hikariConfig);

    ProcessConfig processConfig = ProcessConfig.builder()
        //指定生成邏輯、當存在指定表、指定表字首、指定表字尾時,将生成指定表,其餘表不生成、并跳過忽略表配置
        //根據名稱指定表生成
        .designatedTableName(new ArrayList<>())
        //根據表字首生成
        .designatedTablePrefix(new ArrayList<>())
        //根據表字尾生成
        .designatedTableSuffix(new ArrayList<>()).build();

    //設定生成pojo相關配置
    PojoConfiguration config = new PojoConfiguration();
    //設定檔案存放路徑
    config.setPath("/cn/smallbun/screw/");
    //設定包名
    config.setPackageName("cn.smallbun.screw");
    //設定是否使用lombok
    config.setUseLombok(false);
    //設定資料源
    config.setDataSource(dataSource);
    //設定命名政策
    config.setNameStrategy(new HumpNameStrategy());
    //設定表過濾邏輯
    config.setProcessConfig(processConfig);
    //執行生成
    new PojoExecute(config).execute();
}           

六、源碼位址

通路一飛開源:https://code.exmay.com/

繼續閱讀