1. 程式人生 > 其它 >堪稱資料庫界的 Swagger!一鍵生成資料庫文件。。。

堪稱資料庫界的 Swagger!一鍵生成資料庫文件。。。

1、簡介

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

2、特點

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

3、資料庫支援

4、文件生成支援

5、文件截圖

html

word

markdwon

6、使用方式

普通方式

  • 引入依賴
<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>

7、擴充套件模組

pojo生成功能

  • 功能簡介

pojo生成功能是基於screw延伸出的擴充套件模組,目前處於初步開發的狀態。在日常的開發中,經過需求分析、建模之後,往往會先在資料庫中建表,其次在進行程式碼的開發。

那麼pojo生成功能在這個階段就可以幫助大家節省一些重複勞動了。使用pojo生成功能可以直接根據資料庫生成對應的java pojo物件。這樣後續的修改,開發都會很方便。

  • 資料庫支援

[x] 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();
}

8、常見問題

  • 生成後文檔亂碼?

MySQL:URL加入?characterEncoding=UTF-8。

  • Caused by: java.lang.NoSuchFieldError: VERSION_2_3_30?

檢查專案freemarker依賴,這是由於版本過低造成的,升級版本為2.3.30即可。

  • java.lang.AbstractMethodError: oracle.jdbc.driver.T4CConnection.getSchema()Ljava/lang/String;

這是因為oracle驅動版本過低造成的,刪除或遮蔽目前驅動版本,驅動添加升級為以下版本:

<dependency>
   <groupId>com.oracle.ojdbc</groupId>
   <artifactId>ojdbc8</artifactId>
   <version>19.3.0.0</version>
</dependency>
<dependency>
   <groupId>cn.easyproject</groupId>
   <artifactId>orai18n</artifactId>
   <version>12.1.0.2.0</version>
</dependency>
  • MySQL資料庫表和列欄位有說明、生成文件沒有說明?

URL連結加入useInformationSchema=true即可。

  • java.lang.AbstractMethodError: com.mysql.jdbc.JDBC4Connection.getSchema()Ljava/lang/String;

這是因為mysql驅動版本過低造成的,升級mysql驅動版本為最新即可。

專案地址:https://gitee.com/leshalv/screw

近期熱文推薦:

1.1,000+ 道 Java面試題及答案整理(2022最新版)

2.勁爆!Java 協程要來了。。。

3.Spring Boot 2.x 教程,太全了!

4.別再寫滿屏的爆爆爆炸類了,試試裝飾器模式,這才是優雅的方式!!

5.《Java開發手冊(嵩山版)》最新發布,速速下載!

覺得不錯,別忘了隨手點贊+轉發哦!