SpringBoot中集成screw(螺絲釘)實(shí)現(xiàn)數(shù)據(jù)庫(kù)表結(jié)構(gòu)文檔生成方法
場(chǎng)景
經(jīng)常會(huì)有編寫數(shù)據(jù)庫(kù)表結(jié)構(gòu)文檔的時(shí)間付出,那能否通過簡(jiǎn)單配置實(shí)現(xiàn)自動(dòng)生成。
screw
screw (螺絲釘) 英:[skru?] ~ 簡(jiǎn)潔好用的數(shù)據(jù)庫(kù)表結(jié)構(gòu)文檔生成工具。
https://gitee.com/leshalv/screw
特點(diǎn)
簡(jiǎn)潔、輕量、設(shè)計(jì)良好
多數(shù)據(jù)庫(kù)支持
多種格式文檔
靈活擴(kuò)展
支持自定義模板
數(shù)據(jù)庫(kù)支持
MySQL
MariaDB
TIDB
Oracle
SqlServer
PostgreSQL
Cache DB(2016)
H2 (開發(fā)中)
DB2 (開發(fā)中)
HSQL (開發(fā)中)
SQLite(開發(fā)中)
瀚高(開發(fā)中)
達(dá)夢(mèng) (開發(fā)中)
虛谷 (開發(fā)中)
人大金倉(cāng)(開發(fā)中)
文檔生成支持
html
word
markdown
實(shí)現(xiàn)
下面以連接mysql數(shù)據(jù)庫(kù)并生成html格式的數(shù)據(jù)庫(kù)結(jié)構(gòu)文檔為例
插件的使用方式除可以使用代碼外,還可以使用Maven插件的方式。
這里考慮將其集成到單獨(dú)的springboot項(xiàng)目中,并使用單元測(cè)試的方式需要時(shí)配置和運(yùn)行。
新建springboot項(xiàng)目,并引入screw的依賴
<dependency> <groupId>cn.smallbun.screw</groupId> <artifactId>screw-core</artifactId> <version>1.0.3</version> </dependency>
maven倉(cāng)庫(kù)地址
https://mvnrepository.com/artifact/cn.smallbun.screw/screw-core
然后生成文檔還需依賴
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.30</version> </dependency>
這里還需要連接mysql數(shù)據(jù)庫(kù)以及單元測(cè)試等的依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--MySQL驅(qū)動(dòng)--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
新建單元測(cè)試類
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 org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; import javax.sql.DataSource; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @SpringBootTest class ScrewTest { @Autowired ApplicationContext applicationContext; @Test void createDataBaseWorld() { DataSource dataSourceMysql = applicationContext.getBean(DataSource.class); // 生成文件配置 EngineConfig engineConfig = EngineConfig.builder() // 生成文件路徑,自己mac本地的地址,這里需要自己更換下路徑 .fileOutputDir("D:\\test\\badao\\") // 打開目錄 .openOutputDir(false) // 文件類型 .fileType(EngineFileType.HTML) // 生成模板實(shí)現(xiàn) .produceType(EngineTemplateType.freemarker).build(); // 生成文檔配置(包含以下自定義版本號(hào)、描述等配置連接) Configuration config = Configuration.builder() .version("1.0.3") .description("badao") .dataSource(dataSourceMysql) .engineConfig(engineConfig) .produceConfig(getProcessConfig()) .build(); // 執(zhí)行生成 new DocumentationExecute(config).execute(); } /** * 配置想要生成的表+ 配置想要忽略的表 * @return 生成表配置 */ public static ProcessConfig getProcessConfig(){ // 忽略表名 List<String> ignoreTableName = Arrays.asList("test_users","test1"); // 忽略表前綴,如忽略a開頭的數(shù)據(jù)庫(kù)表 List<String> ignorePrefix = Arrays.asList("qrtz","sys","gen"); // 忽略表后綴 List<String> ignoreSuffix = Arrays.asList("_log"); return ProcessConfig.builder() //根據(jù)名稱指定表生成 .designatedTableName(new ArrayList<>()) //根據(jù)表前綴生成 .designatedTablePrefix(new ArrayList<>()) //根據(jù)表后綴生成 .designatedTableSuffix(new ArrayList<>()) //忽略表名 .ignoreTableName(ignoreTableName) //忽略表前綴 .ignoreTablePrefix(ignorePrefix) //忽略表后綴 .ignoreTableSuffix(ignoreSuffix).build(); } }
這里要注意引入依賴的路徑。
然后這里需要在配置文件這里是yml中配置數(shù)據(jù)源
# 數(shù)據(jù)源 spring: application: name: badao-tcp-demo datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver dbcp2: min-idle: 5 # 數(shù)據(jù)庫(kù)連接池的最小維持連接數(shù) initial-size: 5 # 初始化連接數(shù) max-total: 5 # 最大連接數(shù) max-wait-millis: 150 # 等待連接獲取的最大超時(shí)時(shí)間
然后再上面單元測(cè)試中還可配置要忽略的表,指定前后綴等。
運(yùn)行該單元測(cè)試,到配置的指定目錄下查看
到此這篇關(guān)于SpringBoot中集成screw(螺絲釘)實(shí)現(xiàn)數(shù)據(jù)庫(kù)表結(jié)構(gòu)文檔生成的文章就介紹到這了,更多相關(guān)SpringBoot中集成screw(螺絲釘)實(shí)現(xiàn)數(shù)據(jù)庫(kù)表結(jié)構(gòu)文檔生成內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于SpringBoot和Vue3的博客平臺(tái)的用戶注冊(cè)與登錄功能實(shí)現(xiàn)
本教程將指導(dǎo)您如何使用Spring?Boot和Vue3實(shí)現(xiàn)用戶注冊(cè)與登錄功能。我們將使用Spring?Boot作為后端框架,Vue3作為前端框架,同時(shí)使用MySQL作為數(shù)據(jù)庫(kù),感興趣的朋友可以參考一下2023-04-04java實(shí)現(xiàn)文件讀寫與壓縮實(shí)例
這篇文章主要介紹了java實(shí)現(xiàn)文件讀寫與壓縮實(shí)例,有助于讀者加深對(duì)文件操作的理解,需要的朋友可以參考下2014-07-07SpringBoot?@RestControllerAdvice注解對(duì)返回值統(tǒng)一封裝的處理方法
這篇文章主要介紹了SpringBoot?@RestControllerAdvice注解對(duì)返回值統(tǒng)一封裝,使用@RestControllerAdvice對(duì)響應(yīng)進(jìn)行增強(qiáng),本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09深入了解Java?Synchronized鎖升級(jí)過程
java中的鎖是針對(duì)對(duì)象而言的,它鎖住的是一個(gè)對(duì)象,并且具有可重入的性質(zhì),下面這篇文章主要給大家介紹了關(guān)于Java?Synchronized鎖升級(jí)過程的相關(guān)資料,需要的朋友可以參考下2022-03-03springboot 實(shí)現(xiàn)mqtt物聯(lián)網(wǎng)的示例代碼
這篇文章主要介紹了springboot 實(shí)現(xiàn)mqtt物聯(lián)網(wǎng),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03在@Value注解內(nèi)使用SPEL自定義函數(shù)方式
這篇文章主要介紹了在@Value注解內(nèi)使用SPEL自定義函數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02