周末发现一款数据库文档自动生成的工具项目,试了一下还不错,推荐。源码简洁,使用简单,支持多种数据库,还支持自定义输出模板。为了简便,本文采用Spring Boot Web项目,可打成jar工具使用。
我的项目结构
pom.xml
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.0.RELEASE
com.mingo.screw
screw-jar
0.0.1-SNAPSHOT
screw-jar
Demo project for Spring Boot
jar
1.8
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-starter-jdbc
mysql
mysql-connector-java
runtime
com.zaxxer
HikariCP
cn.smallbun.screw
screw-core
1.0.5
screw-jar
org.springframework.boot
spring-boot-maven-plugin
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 ignoreTableName;
/**
* 忽略表前缀
*/
@Value("#{'${ignorePrefix:}'.empty ? null : '${ignorePrefix:}'.split(',')}")
private List ignorePrefix;
/**
* 忽略表后缀
*/
@Value("#{'${ignoreSuffix:}'.empty ? null : '${ignoreSuffix:}'.split(',')}")
private List ignoreSuffix;
/**
* 根据名称指定表生成
*/
@Value("#{'${designatedTableName:}'.empty ? null : '${designatedTableName:}'.split(',')}")
private List designatedTableName;
/**
* 根据表前缀生成
*/
@Value("#{'${designatedTablePrefix:}'.empty ? null : '${designatedTablePrefix:}'.split(',')}")
private List designatedTablePrefix;
/**
* 根据表后缀生成
*/
@Value("#{'${designatedTableSuffix:}'.empty ? null : '${designatedTableSuffix:}'.split(',')}")
private List 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