SpringBoot中集成screw(螺絲釘)實現(xiàn)數(shù)據庫表結構文檔生成方法
場景
經常會有編寫數(shù)據庫表結構文檔的時間付出,那能否通過簡單配置實現(xiàn)自動生成。
screw
screw (螺絲釘) 英:[skru?] ~ 簡潔好用的數(shù)據庫表結構文檔生成工具。
https://gitee.com/leshalv/screw
特點
簡潔、輕量、設計良好
多數(shù)據庫支持
多種格式文檔
靈活擴展
支持自定義模板
數(shù)據庫支持
MySQL
MariaDB
TIDB
Oracle
SqlServer
PostgreSQL
Cache DB(2016)
H2 (開發(fā)中)
DB2 (開發(fā)中)
HSQL (開發(fā)中)
SQLite(開發(fā)中)
瀚高(開發(fā)中)
達夢 (開發(fā)中)
虛谷 (開發(fā)中)
人大金倉(開發(fā)中)
文檔生成支持
html
word
markdown
實現(xiàn)
下面以連接mysql數(shù)據庫并生成html格式的數(shù)據庫結構文檔為例
插件的使用方式除可以使用代碼外,還可以使用Maven插件的方式。
這里考慮將其集成到單獨的springboot項目中,并使用單元測試的方式需要時配置和運行。
新建springboot項目,并引入screw的依賴
<dependency> <groupId>cn.smallbun.screw</groupId> <artifactId>screw-core</artifactId> <version>1.0.3</version> </dependency>
maven倉庫地址
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ù)據庫以及單元測試等的依賴
<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驅動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
新建單元測試類
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) // 生成模板實現(xiàn) .produceType(EngineTemplateType.freemarker).build(); // 生成文檔配置(包含以下自定義版本號、描述等配置連接) 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ù)據庫表 List<String> ignorePrefix = Arrays.asList("qrtz","sys","gen"); // 忽略表后綴 List<String> ignoreSuffix = Arrays.asList("_log"); return ProcessConfig.builder() //根據名稱指定表生成 .designatedTableName(new ArrayList<>()) //根據表前綴生成 .designatedTablePrefix(new ArrayList<>()) //根據表后綴生成 .designatedTableSuffix(new ArrayList<>()) //忽略表名 .ignoreTableName(ignoreTableName) //忽略表前綴 .ignoreTablePrefix(ignorePrefix) //忽略表后綴 .ignoreTableSuffix(ignoreSuffix).build(); } }
這里要注意引入依賴的路徑。
然后這里需要在配置文件這里是yml中配置數(shù)據源
# 數(shù)據源 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ù)據庫連接池的最小維持連接數(shù) initial-size: 5 # 初始化連接數(shù) max-total: 5 # 最大連接數(shù) max-wait-millis: 150 # 等待連接獲取的最大超時時間
然后再上面單元測試中還可配置要忽略的表,指定前后綴等。
運行該單元測試,到配置的指定目錄下查看
到此這篇關于SpringBoot中集成screw(螺絲釘)實現(xiàn)數(shù)據庫表結構文檔生成的文章就介紹到這了,更多相關SpringBoot中集成screw(螺絲釘)實現(xiàn)數(shù)據庫表結構文檔生成內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
基于SpringBoot和Vue3的博客平臺的用戶注冊與登錄功能實現(xiàn)
本教程將指導您如何使用Spring?Boot和Vue3實現(xiàn)用戶注冊與登錄功能。我們將使用Spring?Boot作為后端框架,Vue3作為前端框架,同時使用MySQL作為數(shù)據庫,感興趣的朋友可以參考一下2023-04-04SpringBoot?@RestControllerAdvice注解對返回值統(tǒng)一封裝的處理方法
這篇文章主要介紹了SpringBoot?@RestControllerAdvice注解對返回值統(tǒng)一封裝,使用@RestControllerAdvice對響應進行增強,本文結合實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09springboot 實現(xiàn)mqtt物聯(lián)網的示例代碼
這篇文章主要介紹了springboot 實現(xiàn)mqtt物聯(lián)網,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03