天天看點

還在手動整理資料庫文檔?試試這個工具

還在手動整理資料庫文檔?試試這個工具

簡介

在企業級開發中、我們經常會有編寫資料庫表結構文檔的時間付出,從業以來,待過幾家企業,關于資料庫表結構文檔狀态:要麼沒有、要麼有、但都是手寫、後期運維開發,需要手動進行維護到文檔中,很是繁瑣、如果忘記一次維護、就會給以後工作造成很多困擾、無形中制造了很多坑留給自己和後人,于是需要一個插件工具

screw

來維護。

screw 特點

  • 簡潔、輕量、設計良好。不需要 powerdesigner 這種重量的模組化工具
  • 多資料庫支援 。支援市面常見的資料庫類型 MySQL、Oracle、SqlServer
  • 多種格式文檔。支援 MD、HTML、WORD 格式
  • 靈活擴充。支援使用者自定義模闆和展示樣式

支援資料庫類型

[✔️] MySQL

[✔️] MariaDB

[✔️] TIDB

[✔️] Oracle

[✔️] SqlServer

[✔️] PostgreSQL

[✔️] Cache DB

依賴

這裡以 mysql8 資料庫為例子

<!--資料庫文檔核心依賴-->
  <dependency>
      <groupId>cn.smallbun.screw</groupId>
      <artifactId>screw-core</artifactId>
      <version>1.0.2</version>
  </dependency>
  <!-- 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>           

1. 通過自定義代碼配置文檔生成

@Test
public void shouldAnswerWithTrue() {
    //資料源
    HikariConfig hikariConfig = new HikariConfig();
    hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
    hikariConfig.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test");
    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("/Users/lengleng")
            //打開目錄
            .openOutputDir(true)
            //檔案類型
            .fileType(EngineFileType.HTML)
            //生成模闆實作
            .produceType(EngineTemplateType.freemarker).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()
            //忽略表名
            .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. 通過插件的形式生成文檔

<build>
    <plugins>
        <plugin>
            <groupId>cn.smallbun.screw</groupId>
            <artifactId>screw-maven-plugin</artifactId>
            <version>1.0.2</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>root</password>
                <!--driver-->
                <driverClassName>com.mysql.cj.jdbc.Driver</driverClassName>
                <!--jdbc url-->
                <jdbcUrl>jdbc:mysql://127.0.0.1:3306/test</jdbcUrl>
                <!--生成檔案類型-->
                <fileType>HTML</fileType>
                <!--檔案輸出目錄-->
                <fileOutputDir>/Users/lengleng</fileOutputDir>
                <!--打開檔案輸出目錄-->
                <openOutputDir>false</openOutputDir>
                <!--生成模闆-->
                <produceType>freemarker</produceType>
                <!--描述-->
                <description>資料庫文檔生成</description>
                <!--版本-->
                <version>${project.version}</version>
                <!--标題-->
                <title>資料庫文檔</title>
            </configuration>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>           
項目推薦: Spring Cloud 、Spring Security OAuth2的RBAC權限管理系統 歡迎關注