一个数据库文档自动生成工具-Screw
周末发现一款数据库文档自动生成的工具项目,试了一下还不错,推荐。源码简洁,使用简单,支持多种数据库,还支持自定义输出模板。为了简便,本文采用Spring Boot Web项目,可打成jar工具使用。
源项目地址:https://github.com/pingfangushi/screw
我的项目结构
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.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mingo.screw</groupId>
<artifactId>screw-jar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>screw-jar</name>
<description>Demo project for Spring Boot</description>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency>
<groupId>cn.smallbun.screw</groupId>
<artifactId>screw-core</artifactId>
<version>1.0.5</version>
</dependency>
</dependencies>
<build>
<finalName>screw-jar</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
server:
port: 8001
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
xa:
properties:
useInformationSchema: true
hikari:
connection-timeout: 60000
validation-timeout: 3000
idle-timeout: 60000
auto-commit: 'true'
minimum-idle: 5
maximum-pool-size: 5
pool-name: DatebookHikariCP
max-lifetime: 60000
url: >-
jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true&serverTimezone=UTC
username: root
password: 'root'
type: com.zaxxer.hikari.HikariDataSource
screw:
common:
organization:
organizationUrl:
title:
version: 1.0.0
description: '数据库设计文档生成'
fileOutputDir: 'C:\Users\mingo\Desktop\'
openOutputDir: false
fileType: HTML
produceType: freemarker
fileName: 'test'
ignoreTableName: test_table,test
designatedTableName:
designatedTablePrefix:
designatedTableSuffix:
ignorePrefix:
ignoreSuffix:
ScrewProperties.java
package com.mingo.screw.generate;
import cn.smallbun.screw.core.engine.EngineFileType;
import cn.smallbun.screw.core.engine.EngineTemplateType;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.List;
/**
* Screw配置
*/
@Data
@ConfigurationProperties(prefix = "screw")
public class ScrewProperties {
/**
* 生成文件路径
*/
private String fileOutputDir;
/**
* 打开目录
*/
private Boolean openOutputDir;
/**
* 文件类型
*/
private EngineFileType fileType;
/**
* 生成模板实现
*/
private EngineTemplateType produceType;
/**
* 自定义文件名称
*/
private String fileName;
/**
* 忽略表
*/
@Value("#{'${ignoreTableName:}'.empty ? null : '${ignoreTableName:}'.split(',')}")
private List<String> ignoreTableName;
/**
* 忽略表前缀
*/
@Value("#{'${ignorePrefix:}'.empty ? null : '${ignorePrefix:}'.split(',')}")
private List<String> ignorePrefix;
/**
* 忽略表后缀
*/
@Value("#{'${ignoreSuffix:}'.empty ? null : '${ignoreSuffix:}'.split(',')}")
private List<String> ignoreSuffix;
/**
* 根据名称指定表生成
*/
@Value("#{'${designatedTableName:}'.empty ? null : '${designatedTableName:}'.split(',')}")
private List<String> designatedTableName;
/**
* 根据表前缀生成
*/
@Value("#{'${designatedTablePrefix:}'.empty ? null : '${designatedTablePrefix:}'.split(',')}")
private List<String> designatedTablePrefix;
/**
* 根据表后缀生成
*/
@Value("#{'${designatedTableSuffix:}'.empty ? null : '${designatedTableSuffix:}'.split(',')}")
private List<String> designatedTableSuffix;
/**
* 文档一般信息
*/
private Common common = new Common();
/**
* static
*/
@Data
public static class Common {
private String organization;
private String organizationUrl;
private String title;
/**
* 版本
*/
private String version = "1.0.0";
/**
* 描述
*/
private String description = "数据库设计文档生成";
}
}
GenerateDbDoc.java
package com.mingo.screw.generate;
import cn.smallbun.screw.core.Configuration;
import cn.smallbun.screw.core.engine.EngineConfig;
import cn.smallbun.screw.core.execute.DocumentationExecute;
import cn.smallbun.screw.core.process.ProcessConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
/**
* 数据库文档生成
*/
@Slf4j
@Component
public class GenerateDbDoc implements ApplicationRunner {
@Autowired
private ApplicationContext ctx;
@Autowired
private ScrewProperties screwProperties;
@Autowired
private HikariDataSource dataSource;
/**
* 程序启动后执行
*
* @param args
* @throws Exception
*/
@Override
public void run(ApplicationArguments args) throws Exception {
documentGeneration();
// 安全关闭应用
((ConfigurableApplicationContext) ctx).close();
}
/**
* 文档生成
*/
private void documentGeneration() {
// 表的备注信息 配置中spring.datasource.xa.properties.useInformationSchema=true没有生效
dataSource.addDataSourceProperty("useInformationSchema", "true");
// 生成配置
EngineConfig engineConfig = EngineConfig.builder()
.fileOutputDir(screwProperties.getFileOutputDir())
.openOutputDir(screwProperties.getOpenOutputDir())
.fileType(screwProperties.getFileType())
.produceType(screwProperties.getProduceType())
.fileName(screwProperties.getFileName())
.build();
// 指定生成逻辑、当存在指定表、指定表前缀、指定表后缀时,将生成指定表,其余表不生成、并跳过忽略表配置
ProcessConfig processConfig = ProcessConfig.builder()
.designatedTableName(screwProperties.getDesignatedTableName())
.designatedTablePrefix(screwProperties.getDesignatedTablePrefix())
.designatedTableSuffix(screwProperties.getDesignatedTableSuffix())
.ignoreTableName(screwProperties.getIgnoreTableName())
.ignoreTablePrefix(screwProperties.getIgnorePrefix())
.ignoreTableSuffix(screwProperties.getIgnoreSuffix())
.build();
// 配置
Configuration config = Configuration.builder()
.version(screwProperties.getCommon().getVersion())
.description(screwProperties.getCommon().getDescription())
//数据源
.dataSource(dataSource)
//生成配置
.engineConfig(engineConfig)
//生成配置
.produceConfig(processConfig)
.build();
//执行生成
new DocumentationExecute(config).execute();
}
}
运行结果(HTML)
这里代码比较简单,我没做过多解释。原作者的文档例子更简洁清晰,可以去看下。本文只是在文档生成后关闭了项目((ConfigurableApplicationContext) ctx).close()。
打包成screw-jar.jar运行
运行目录
命令运行:java -jar screw-jar.jar
原创
Doflamingo
https://www.cnblogs.com/doflamingo