一鍵生成數據庫文檔,無需重復CV
在微信公眾號中發現了這個一篇文章,原文地址:https://mp.weixin.qq.com/s/oNd8rsEv343hxCbwbfFziw
不必要的贅述,直接來說怎么生成吧。根據原文的描述,我在將他細致到傻瓜化。
在GitHub中有這樣神奇的工具,
screw(螺絲釘),這個工具可以生成數據庫文檔,而且還可以支持三種輸出模式:WORD、MD、HTML

數據庫支持
- MySQL
- MariaDB
- TIDB
- Oracle
- SqlServer
- PostgreSQL
- Cache DB
配置
1、創建Spring項目




2、配置
引入screw核心包,HikariCP數據庫連接池,HikariCP號稱性能最出色的數據庫連接池。整個pom.xml內容如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.kdgc</groupId>
<artifactId>swagger</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>swagger</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- HikariCP -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
<!-- screw核心 -->
<dependency>
<groupId>cn.smallbun.screw</groupId>
<artifactId>screw-core</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.oracle.ojdbc/ojdbc8 -->
<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.3.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、配置數據源
在application.properties文件中的配置如下,我是測試了Oracle和Mysql
#spring.datasource.url=jdbc:mysql://192.168.11.11:3306/mas-party?useUnicode=true&characterEncoding=UTF-8&useSSL=false
#spring.datasource.username=root
#spring.datasource.password=123456
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.datasource.xa.properties.useInformationSchema=true
spring.datasource.platform=oracle
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@59.203.11.11:15211:credit
spring.datasource.username=creditfw
spring.datasource.password=credit
4、測試代碼
代碼生成方式也非常簡單,如圖SwaggerApplicationTests的代碼如下:
package com.kdgc.swagger;
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.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SwaggerApplicationTests {
@Autowired
ApplicationContext applicationContext;
@Test
public void contextLoads() {
DataSource dataSourceMysql = applicationContext.getBean(DataSource.class);
// 生成文件配置
EngineConfig engineConfig = EngineConfig.builder()
// 生成文件路徑,自己mac本地的地址,這里需要自己更換下路徑
.fileOutputDir("D:/")
// 打開目錄
.openOutputDir(false)
// 文件類型
.fileType(EngineFileType.HTML)
// 生成模板實現
.produceType(EngineTemplateType.freemarker).build();
// 生成文檔配置(包含以下自定義版本號、描述等配置連接)
Configuration config = Configuration.builder()
.version("1.0.0")
.description("數據庫文檔")
.dataSource(dataSourceMysql)
.engineConfig(engineConfig)
.produceConfig(getProcessConfig())
.build();
// 執行生成
new DocumentationExecute(config).execute();
}
/**
* 配置想要生成的表+ 配置想要忽略的表
*
* @return 生成表配置
*/
public static ProcessConfig getProcessConfig() {
// 忽略表名
// List<String> ignoreTableName = Arrays.asList("a", "test_group");
// 忽略表前綴,如忽略BASE_開頭的數據庫表
List<String> ignorePrefix = Arrays.asList("BASE_", "T_","SYS_");
// 忽略表后綴
// List<String> ignoreSuffix = Arrays.asList("_test", "czb_");
return ProcessConfig.builder()
//根據名稱指定表生成
//.designatedTableName(Arrays.asList("fire_user"))
//根據表前綴生成
.designatedTablePrefix(new ArrayList<>())
//根據表后綴生成
.designatedTableSuffix(new ArrayList<>())
//忽略表名
// .ignoreTableName(ignoreTableName)
//忽略表前綴
.ignoreTablePrefix(ignorePrefix)
//忽略表后綴
//.ignoreTableSuffix(ignoreSuffix)
.build();
}
}
以上為全部代碼,測試正常。
5、文檔格式
screw 有 HTML、DOC、MD 三種格式的文檔。
代碼中的修改
.fileType(EngineFileType.HTML)
DOC文檔樣式

MD文檔樣式

HTML文檔樣式

