首頁>技術>

# 介紹

平時很多時候,在交付時,需要交付資料庫設計文件。評審時候也需要檢視資料庫設計文件。平時開發時,經常會修改資料庫表,然後還要修改資料庫設計文件,一旦忘了,就會變得很麻煩,挨個排查,耽誤時間。所以這次介紹一款外掛,直接生成資料庫設計文件,並且支援MD,word,html等格式,而且還是開源,可以自己改造,裡面是模板化的freemarker。

這就是第一期的《小鑫發現》之SCREW資料庫文件生成

# 構建

這裡使用MAVEN外掛的方式進行生成。

新建一個Springboot的專案,為啥是Springboot的呢,沒啥,就會這個,方便。

## MAVEN 依賴

```

<properties>   <java.version>1.8</java.version>   <screw.version>1.0.5</screw.version>   <HikariCP.version>3.4.5</HikariCP.version>   <mysql-connector-java.version>8.0.22</mysql-connector-java.version></properties><dependencies>   <dependency>       <groupId>org.springframework.boot</groupId>       <artifactId>spring-boot-starter</artifactId>   </dependency>   <dependency>       <groupId>org.springframework.boot</groupId>       <artifactId>spring-boot-starter-test</artifactId>       <scope>test</scope>   </dependency>   <dependency>       <groupId>cn.smallbun.screw</groupId>       <artifactId>screw-core</artifactId>       <version>${screw.version}</version>   </dependency>   <dependency>       <groupId>com.zaxxer</groupId>       <artifactId>HikariCP</artifactId>       <version>${HikariCP.version}</version>   </dependency>   <dependency>       <groupId>mysql</groupId>       <artifactId>mysql-connector-java</artifactId>       <version>${mysql-connector-java.version}</version>   </dependency>   <dependency>       <groupId>org.projectlombok</groupId>       <artifactId>lombok</artifactId>       <version>1.18.16</version>       <scope>provided</scope>   </dependency></dependencies>

```

screw-core是外掛的核心,HikariCP號稱是最快的資料庫連線池。

依賴和外掛構建就完成了,沒想到這麼簡單,我也沒想到。

# 執行

這裡直接在IDEA裡,雙擊這個run。看看效果

執行日誌

然後當前專案上就會有doc資料夾,檢視裡面的文件

這裡使用的html,這裡暫時就用官方的圖片,更能看出來很多細節(主要是懶得在建立一個數據表了)

至此所有的文件就生成了,是不是方便,下一步就行改造,因為他還有很多配置項,透過程式碼更方便檢視。

> 主要是我這修改maven總提示我構建,煩

# 改造

在test檔案下,有個測試類,在裡面進行改造。

方便配置化,使用一個配置類,和配置檔案。

配置檔案

```

gen:  db:    driverClassName: com.mysql.cj.jdbc.Driver    ip: jdbc:mysql://127.0.0.1:3306/test1?characterEncoding=UTF-8    username: root    password: mima  engineConfig:    fileOutputDir: /Users/iOLO/dev/Java/dbword/doc    fileName: 資料庫設計文件    version: 1.0.0    description: 資料庫描述

```

配置類

```

package com.iolo.dbword.config;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;/** * @author iOLO * @date 2020/12/23 **/@Component@ConfigurationProperties(prefix = "gen")@Datapublic class GenConfig {    private DB db;    private EngineConfig engineConfig;    @Data    public static class DB {        private String ip;        private String driverClassName;        private String username;        private String password;    }    @Data    public static class EngineConfig {        private String fileOutputDir;        private String fileName;        private String version;        private String description;    }}

```

測試類

```

package com.iolo.dbword;import cn.smallbun.screw.core.Configuration;import cn.smallbun.screw.core.engine.EngineConfig;import cn.smallbun.screw.core.engine.EngineFileType;import cn.smallbun.screw.core.engine.EngineTemplateType;import cn.smallbun.screw.core.execute.DocumentationExecute;import cn.smallbun.screw.core.process.ProcessConfig;import com.iolo.dbword.config.GenConfig;import com.zaxxer.hikari.HikariConfig;import com.zaxxer.hikari.HikariDataSource;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import javax.sql.DataSource;@SpringBootTestclass DbwordApplicationTests {    @Autowired    GenConfig genConfig;    @Test    void contextLoads() {    }    @Test    void documentGeneration() {        //資料來源        HikariConfig hikariConfig = new HikariConfig();        hikariConfig.setDriverClassName(genConfig.getDb().getDriverClassName());        hikariConfig.setJdbcUrl(genConfig.getDb().getIp());        hikariConfig.setUsername(genConfig.getDb().getUsername());        hikariConfig.setPassword(genConfig.getDb().getPassword());        //設定可以獲取備註資訊資訊        hikariConfig.addDataSourceProperty("useInformationSchema", "true");        hikariConfig.setMinimumIdle(2);        hikariConfig.setMaximumPoolSize(5);        DataSource dataSource = new HikariDataSource(hikariConfig);        //生成配置        EngineConfig engineConfig = EngineConfig.builder()                //生成檔案路徑                .fileOutputDir(genConfig.getEngineConfig().getFileOutputDir())                //開啟目錄                .openOutputDir(true)                //檔案型別 HTML WORD MD                .fileType(EngineFileType.HTML)                //生成模板實現                .produceType(EngineTemplateType.freemarker)                //自定義檔名稱                .fileName(genConfig.getEngineConfig().getFileName()).build();        ProcessConfig processConfig = ProcessConfig.builder().build();                //配置        Configuration config = Configuration.builder()                //版本                .version(genConfig.getEngineConfig().getVersion())                //描述                .description(genConfig.getEngineConfig().getDescription())                //資料來源                .dataSource(dataSource)                //生成配置                .engineConfig(engineConfig)                //生成配置                .produceConfig(processConfig)                .build();        //執行生成        new DocumentationExecute(config).execute();    }}

```

然後執行測試方法,依然也可以文件,並且發現會有更多的配置引數,如下

```

       //忽略表        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();

```

# 參考連線

https://gitee.com/leshalv/screw

6
最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • springboot2.4配置MySQ8.0.22-jdbc