在項目中,我們經常需要整理數據庫表結構文檔。
一般情況下,我們都是手動整理數據庫表結構文檔,當表結構有變動的時候,自己手動進行維護。
數據庫表少的時候還好,數據庫表多了之後,手動整理和維護數據庫表結構文檔簡直不要太麻煩,而且,還非常容易出錯!
有沒有什麼好用的工具幫助我們自動生成數據庫表結構文檔呢?
當然有!Github 上就有一位朋友開源了一款數據庫表結構文檔自動生成工具—— screw 。
項目地址:https://github.com/pingfangushi/screw 。
screw 翻譯過來的意思就是螺絲釘,作者希望這個工具能夠像螺絲釘一樣切實地幫助到我們的開發工作。
目前的話,screw 已經支持市面上大部分常見的數據庫比如 MySQL、MariaDB、Oracle、SqlServer、PostgreSQL、TiDB。
另外,screw 使用起來也非常簡單,根據官網提示,不用 10 分鐘就能成功在本地使用起來!
快速入門
爲了驗證 screw 自動生成數據庫表結構文檔的效果,我們首先創建一個簡單的存放博客數據的數據庫表。
CREATE TABLE `blog` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主鍵',
`title` varchar(255) NOT NULL COMMENT '博客標題',
`content` longtext NOT NULL COMMENT '博客內容',
`description` varchar(255) DEFAULT NULL COMMENT '博客簡介',
`cover` varchar(255) DEFAULT NULL COMMENT '博客封面圖片地址',
`views` int(11) NOT NULL DEFAULT '0' COMMENT '博客閱讀次數',
`user_id` bigint(20) DEFAULT '0' COMMENT '發表博客的用戶ID',
`channel_id` bigint(20) NOT NULL COMMENT '博客分類ID',
`recommend` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否推薦',
`top` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否置頂',
`comment` bit(1) NOT NULL DEFAULT b'1' COMMENT '是否開啓評論',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '創建時間',
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新時間',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8mb4 COMMENT='博客';
基於 Java 代碼
引入依賴
創建一個普通的 Maven 項目即可!然後引入 screw、HikariCP、MySQL 這 3 個依賴。
<!--screw-->
<dependency>
<groupId>cn.smallbun.screw</groupId>
<artifactId>screw-core</artifactId>
<version>1.0.5</version>
</dependency>
<!-- HikariCP -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
<!--MySQL-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
你可以通過下面的地址在 mvnrepository 獲取最新版本的 screw。
https://mvnrepository.com/artifact/cn.smallbun.screw/screw-core
編寫代碼
生成數據庫文檔的代碼的整個代碼邏輯還是比較簡單的,我們只需要經過下面 5 步即可:
// 1.獲取數據源
DataSource dataSource = getDataSource();
// 2.獲取數據庫文檔生成配置(文件路徑、文件類型)
EngineConfig engineConfig = getEngineConfig();
// 3.獲取數據庫表的處理配置,可忽略
ProcessConfig processConfig = getProcessConfig();
// 4.Screw 完整配置
Configuration config = getScrewConfig(dataSource, engineConfig, processConfig);
// 5.執行生成數據庫文檔
new DocumentationExecute(config).execute();
1、獲取數據庫源
對數據庫以及數據庫連接池進行相關配置。務必將數據庫相關的配置修改成你自己的。
/**
* 獲取數據庫源
*/
private static DataSource getDataSource() {
//數據源
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
hikariConfig.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/javaguide-blog");
hikariConfig.setUsername("root");
hikariConfig.setPassword("123456");
//設置可以獲取tables remarks信息
hikariConfig.addDataSourceProperty("useInformationSchema", "true");
hikariConfig.setMinimumIdle(2);
hikariConfig.setMaximumPoolSize(5);
return new HikariDataSource(hikariConfig);
}
2、獲取文件生成配置
這一步會指定數據庫文檔生成的位置、文件類型以及文件名稱。
/**
* 獲取文件生成配置
*/
private static EngineConfig getEngineConfig() {
//生成配置
return EngineConfig.builder()
//生成文件路徑
.fileOutputDir("/Users/guide/Documents/代碼示例/screw-demo/doc")
//打開目錄
.openOutputDir(true)
//文件類型
.fileType(EngineFileType.HTML)
//生成模板實現
.produceType(EngineTemplateType.freemarker)
//自定義文件名稱
.fileName("數據庫結構文檔").build();
}
如果不配置生成文件路徑的話,默認也會存放在項目的 doc
目錄下。
另外,我們這裏指定生成的文件格式爲 HTML。除了 HTML 之外,screw 還支持 Word 、Markdown 這兩種文件格式。
不太建議生成 Word 格式,比較推薦 Markdown 格式。
3、獲取數據庫表的處理配置
這一步你可以指定忽略生成哪些表。
/**
* 獲取數據庫表的處理配置,可忽略
*/
private static ProcessConfig getProcessConfig() {
return ProcessConfig.builder()
// 指定只生成 blog 表
.designatedTableName(new ArrayList<>(Collections.singletonList("blog")))
.build();
}
還可以指定只生成哪些表。
private static ProcessConfig getProcessConfig() {
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");
return ProcessConfig.builder()
//忽略表名
.ignoreTableName(ignoreTableName)
//忽略表前綴
.ignoreTablePrefix(ignorePrefix)
//忽略表後綴
.ignoreTableSuffix(ignoreSuffix)
.build();
}
這一步也可以省略。如果不指定 ProcessConfig 的話,就會按照默認配置來!
4、生成 screw 完整配置
根據前面 3 步,生成 screw 完整配置。
private static Configuration getScrewConfig(DataSource dataSource, EngineConfig engineConfig, ProcessConfig processConfig) {
return Configuration.builder()
//版本
.version("1.0.0")
//描述
.description("數據庫設計文檔生成")
//數據源
.dataSource(dataSource)
//生成配置
.engineConfig(engineConfig)
//生成配置
.produceConfig(processConfig)
.build();
}
5、執行生成數據庫文檔
下圖就是生成的 HTML 格式的數據庫設計文檔。
基於 Maven 插件
除了基於 Java 代碼這種方式之外,你還可以通過 screw 提供的 Maven 插件來生成數據庫文檔。方法也非常簡單!
1、配置 Maven 插件
務必將數據庫相關的配置修改成你自己的。
<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-->
<username>root</username>
<!--password-->
<password>123456</password>
<!--driver-->
<driverClassName>com.mysql.cj.jdbc.Driver</driverClassName>
<!--jdbc url-->
<jdbcUrl>jdbc:mysql://127.0.0.1:3306/javaguide-blog</jdbcUrl>
<!--生成文件類型-->
<fileType>MD</fileType>
<!--打開文件輸出目錄-->
<openOutputDir>true</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、手動執行生成數據庫文檔
我們這裏指定生成的是 Markdown 格式。
下圖就是生成的 Markdown 格式的數據庫設計文檔,效果還是非常不錯的!
本文分享自微信公衆號 - 武培軒(wupeixuan404)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。