SpringBoot集成screw實(shí)現(xiàn)數(shù)據(jù)庫(kù)表結(jié)構(gòu)文檔生成
一、簡(jiǎn)介
日常的開(kāi)發(fā)工作中,經(jīng)常會(huì)和數(shù)據(jù)庫(kù)打交道,在某些場(chǎng)景可能會(huì)需要數(shù)據(jù)庫(kù)表結(jié)構(gòu)的文檔, screw(螺絲釘)是一款簡(jiǎn)潔好用的數(shù)據(jù)庫(kù)表結(jié)構(gòu)文檔生成工具。支持多數(shù)據(jù)庫(kù)、自定義模板、多種格式文檔輸出,具備簡(jiǎn)潔、輕量、設(shè)計(jì)良好、靈活擴(kuò)展的特點(diǎn)。目前支持MySQL、Oracle、SqlServer、MariaDB、PostgreSQL等數(shù)據(jù)庫(kù),生成文檔目前支持html、word、markdown文檔格式。
二、效果圖展示


三、使用方式
引入依賴(lài)jar:
<!--screw數(shù)據(jù)庫(kù)表結(jié)構(gòu)文檔生成工具-->
<dependency>
<groupId>cn.smallbun.screw</groupId>
<artifactId>screw-core</artifactId>
<version>1.0.3</version>
</dependency>核心代碼:
package com.wonders.common.utils;
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.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import javax.sql.DataSource;
import java.util.ArrayList;
/**
* @Description: TODO:screw數(shù)據(jù)庫(kù)表結(jié)構(gòu)文檔生成工具
* @Author: yyalin
* 參考網(wǎng)站:https://blog.csdn.net/weixin_49613823/article/details/128600130
* @CreateDate: 2022/4/27 14:51
* @Version: V1.0
*/
@Slf4j
public class ScrewUtils {
public static void runScrew() {
//MYSQL數(shù)據(jù)源
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
// //hikariConfig.setJdbcUrl("jdbc:mysql://127.0.0.1:3308/test?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&autoReconnect=true");
// //TIDB連接
//hikariConfig.setJdbcUrl("jdbc:mysql://10.1.50.190:4000/CDR?useUnicode=true&characterEncoding=utf8&useSSL=false&zeroDateTimeBehavior=convertToNull");
hikariConfig.setJdbcUrl("jdbc:mysql://10.1.51.215:3306/KTHDC_MDM?useUnicode=true&characterEncoding=utf8&useSSL=false&zeroDateTimeBehavior=convertToNull");
hikariConfig.setUsername("root");
hikariConfig.setPassword("123456");
//ORACLE數(shù)據(jù)源
// HikariConfig hikariConfig = new HikariConfig();
// hikariConfig.setDriverClassName("oracle.jdbc.driver.OracleDriver");
// hikariConfig.setJdbcUrl("jdbc:oracle:thin:@10.1.51.175:1521/jcpt");
// hikariConfig.setUsername("CDR");
// hikariConfig.setPassword("CDR");
//設(shè)置可以獲取tables remarks信息 表中文注釋
hikariConfig.addDataSourceProperty("useInformationSchema", "true");
hikariConfig.setMinimumIdle(2);
hikariConfig.setMaximumPoolSize(5);
DataSource dataSource = new HikariDataSource(hikariConfig);
//生成配置
EngineConfig engineConfig = EngineConfig.builder()
//生成文件路徑
.fileOutputDir("D:\\\\docFile")
//打開(kāi)目錄
.openOutputDir(true)
//文件類(lèi)型
.fileType(EngineFileType.WORD) //word或html MD
//生成模板實(shí)現(xiàn)
.produceType(EngineTemplateType.freemarker)
//自定義文件名稱(chēng)
.build();
//配置
Configuration config = Configuration.builder()
//版本
.version("V1.0")
//描述
.description("數(shù)據(jù)庫(kù)設(shè)計(jì)文檔")
//數(shù)據(jù)源
.dataSource(dataSource)
//生成配置
.engineConfig(engineConfig)
//生成配置
.produceConfig(getProcessConfig())
.build();
//執(zhí)行生成
new DocumentationExecute(config).execute();
}
/**
* 功能描述:配置生成對(duì)應(yīng)表的篩選條件
* @MethodName: getProcessConfig
* @MethodParam: []
* @Return: cn.smallbun.screw.core.process.ProcessConfig
* @Author: yyalin
* @CreateDate: 2022/4/27 15:15
*/
public static ProcessConfig getProcessConfig() {
//忽略表
ArrayList<String> ignoreTableName = new ArrayList<>();
// ignoreTableName.add("message");
//忽略表前綴
ArrayList<String> ignorePrefix = new ArrayList<>();
// ignorePrefix.add("act_");
// ignorePrefix.add("flw_");
//忽略表后綴
ArrayList<String> ignoreSuffix = new ArrayList<>();
// ignoreSuffix.add("_dictionary");
ArrayList<String> designatedTableList = new ArrayList<>();
// designatedTableList.add("PDCA_DISINFECTION_SUPPLY");
// designatedTableList.add("PDCA_SURGICAL_INSTRUMENTS");
// designatedTableList.add("PUB_DIAG_CATA_MAP");
ProcessConfig processConfig = ProcessConfig.builder()
//指定生成邏輯、當(dāng)存在指定表、指定表前綴、指定表后綴時(shí),將生成指定表,其余表不生成、并跳過(guò)忽略表配置
//根據(jù)名稱(chēng)指定表生成
// .designatedTableName(new ArrayList<>())
.designatedTableName(designatedTableList)
//根據(jù)表前綴生成
.designatedTablePrefix(new ArrayList<>())
//根據(jù)表后綴生成
.designatedTableSuffix(new ArrayList<>())
//忽略表名
.ignoreTableName(ignoreTableName)
//忽略表前綴
.ignoreTablePrefix(ignorePrefix)
//忽略表后綴
.ignoreTableSuffix(ignoreSuffix).build();
return processConfig;
}
//臨時(shí)使用
public static void main(String[] args) {
runScrew();
}
}注意:可以通過(guò)設(shè)置忽略表名稱(chēng)、忽略表前綴、忽略表后綴等方式過(guò)濾不需要帶出的相關(guān)表,從而達(dá)到指定導(dǎo)出的效果。
同樣,可以與springboot更好地集成,只需將上述的參數(shù)配置在yml中即可,通過(guò)@Value("${spring.datasource.driver-class-name}")的方式獲取即可。
四、方法補(bǔ)充
除了上文的內(nèi)容,下面小編為大家整理了screw工具的其他應(yīng)用,希望對(duì)大家有所幫助
1.引入庫(kù)
pom文件中引入jar依賴(lài)
<!-- 數(shù)據(jù)庫(kù)設(shè)計(jì)文檔生成工具-->
<dependency>
<groupId>cn.smallbun.screw</groupId>
<artifactId>screw-core</artifactId>
<version>1.0.5</version>
</dependency>
2.通過(guò)命令行工具使用
下載cn.smallbun.screw的JAR文件,并將其添加到項(xiàng)目中。
在命令行中執(zhí)行以下命令:
java -jar screw.jar -url jdbc:mysql://localhost:3306/database -driver com.mysql.jdbc.Driver -username root -password 123456 -type html -outDri doc
上述命令使用MySQL數(shù)據(jù)庫(kù)作為示例,根據(jù)實(shí)際情況修改數(shù)據(jù)庫(kù)連接相關(guān)參數(shù)。通過(guò)-type指定輸出文檔的格式,通過(guò)-outDri指定輸出路徑。
3.通過(guò)Java代碼使用
代碼如下(下面用的是postgres數(shù)據(jù)庫(kù)):
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.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
public class DatabaseDesignDocUtil {
public static void main(String[] args) {
documentGeneration();
}
/**
* 文檔生成
*/
public static void documentGeneration() {
//數(shù)據(jù)源
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName("org.postgresql.Driver");
hikariConfig.setJdbcUrl("jdbc:postgresql://39.164.52.80:5432/work_face");
hikariConfig.setUsername("postgres");
hikariConfig.setPassword("POSTGRES");
//設(shè)置可以獲取tables remarks信息
hikariConfig.addDataSourceProperty("useInformationSchema", "true");
hikariConfig.setMinimumIdle(2);
hikariConfig.setMaximumPoolSize(5);
DataSource dataSource = new HikariDataSource(hikariConfig);
//生成配置
EngineConfig engineConfig = EngineConfig.builder()
//生成文件路徑
.fileOutputDir("C:\\Users\\tarzan\\Desktop\\doc")
//打開(kāi)目錄
.openOutputDir(true)
//文件類(lèi)型
.fileType(EngineFileType.WORD)
//生成模板實(shí)現(xiàn)
.produceType(EngineTemplateType.freemarker)
//自定義文件名稱(chēng)
.fileName("智能工作面數(shù)據(jù)庫(kù)設(shè)計(jì)文檔").build();
//忽略表
List<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()
//指定生成邏輯、當(dāng)存在指定表、指定表前綴、指定表后綴時(shí),將生成指定表,其余表不生成、并跳過(guò)忽略表配置
//根據(jù)名稱(chēng)指定表生成
.designatedTableName(new ArrayList<>())
//根據(jù)表前綴生成
.designatedTablePrefix(new ArrayList<>())
//根據(jù)表后綴生成
.designatedTableSuffix(new ArrayList<>())
//忽略表名
.ignoreTableName(ignoreTableName)
//忽略表前綴
.ignoreTablePrefix(ignorePrefix)
//忽略表后綴
.ignoreTableSuffix(ignoreSuffix).build();
//配置
Configuration config = Configuration.builder()
//版本
.version("1.0.0")
//描述
.description("數(shù)據(jù)庫(kù)設(shè)計(jì)文檔生成")
//數(shù)據(jù)源
.dataSource(dataSource)
//生成配置
.engineConfig(engineConfig)
//生成配置
.produceConfig(processConfig)
.build();
//執(zhí)行生成
new DocumentationExecute(config).execute();
}
}在上述示例中,我們創(chuàng)建了一個(gè)Configuration對(duì)象,配置了數(shù)據(jù)庫(kù)連接信息和文檔輸出選項(xiàng)。然后,通過(guò)DocumentationExecute執(zhí)行文檔生成操作。需要改成mysql等別的數(shù)據(jù)庫(kù)的,請(qǐng)?jiān)诖a中改下驅(qū)動(dòng)配置和連接配置。
到此這篇關(guān)于SpringBoot集成screw實(shí)現(xiàn)數(shù)據(jù)庫(kù)表結(jié)構(gòu)文檔生成的文章就介紹到這了,更多相關(guān)SpringBoot screw數(shù)據(jù)庫(kù)文檔生成內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot集成screw反向生成數(shù)據(jù)庫(kù)說(shuō)明文檔
- SpringBoot中集成screw(螺絲釘)實(shí)現(xiàn)數(shù)據(jù)庫(kù)表結(jié)構(gòu)文檔生成方法
- SpringBoot集成screw實(shí)現(xiàn)數(shù)據(jù)庫(kù)文檔生成的代碼示例
- SpringBoot整合screw實(shí)現(xiàn)自動(dòng)生成數(shù)據(jù)庫(kù)設(shè)計(jì)文檔
- SpringBoot整合screw實(shí)現(xiàn)數(shù)據(jù)庫(kù)文檔自動(dòng)生成的示例代碼
- Java使用screw來(lái)對(duì)比數(shù)據(jù)庫(kù)表和字段差異
相關(guān)文章
Sentinel的熔斷降級(jí)、資源規(guī)則詳解與實(shí)例
這篇文章主要介紹了Sentinel的熔斷降級(jí)、資源規(guī)則詳解與實(shí)例,Sentinel是阿里巴巴開(kāi)源的一款流量控制和熔斷降級(jí)的框架,它主要用于保護(hù)分布式系統(tǒng)中的服務(wù)穩(wěn)定性,Sentinel通過(guò)對(duì)服務(wù)進(jìn)行流量控制和熔斷降級(jí),可以有效地保護(hù)系統(tǒng)的穩(wěn)定性,需要的朋友可以參考下2023-09-09
springboot如何接收text/plain參數(shù)
這篇文章主要介紹了springboot如何接收text/plain參數(shù)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-06-06
Java?9中List.of()的使用示例及注意事項(xiàng)
Java 9引入了一個(gè)新的靜態(tài)工廠方法List.of(),用于創(chuàng)建不可變的列表對(duì)象,這篇文章主要介紹了Java?9中List.of()的使用示例及注意事項(xiàng)的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-03-03
SpringMVC的注解@RequestMapping屬性及使用
這篇文章主要為大家介紹了SpringMVC注解@RequestMapping屬性及使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
MybatisPlus中的多表?xiàng)l件排序查詢(xún)
這篇文章主要介紹了MybatisPlus中的多表?xiàng)l件排序查詢(xún),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
詳解PipedInputStream和PipedOutputStream_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了管道PipedInputStream和PipedOutputStream,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
通過(guò)實(shí)例解析Spring Ioc項(xiàng)目實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了Spring Ioc項(xiàng)目實(shí)踐過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
System.identityHashCode和hashCode的區(qū)別及說(shuō)明
String調(diào)用hashCode()和System.identityHashCode()返回值不同是因?yàn)镾tring重寫(xiě)了hashCode()方法,而System.identityHashCode()返回對(duì)象的內(nèi)存地址哈希值;Test調(diào)用兩個(gè)方法返回值相同是因?yàn)門(mén)est沒(méi)有重寫(xiě)hashCode()方法,因此兩者調(diào)用底層的JVM_IHashCode方法返回相同值2024-11-11

