介绍:Screw是一个用于生成数据库文档的开源工具。它支持从数据库中提取表结构、列信息、索引等元数据,然后将其生成为可视化的文档,Screw支持多种数据库,包括MySQL、Oracle等。
官网地址:https://gitee.com/leshalv/screw
目录
介绍:Screw是一个用于生成数据库文档的开源工具。它支持从数据库中提取表结构、列信息、索引等元数据,然后将其生成为可视化的文档,Screw支持多种数据库,包括MySQL、Oracle等。
一:添加依赖: 在Spring Boot项目的pom.xml文件中添加Screw的依赖:
方式一:使用代码生成文档
1:添加依赖: 在Spring Boot项目的pom.xml
文件中添加Screw的依赖:
<dependency>
<groupId>cn.smallbun.screw</groupId>
<artifactId>screw-core</artifactId>
<version>1.0.5</version>
</dependency>
2、编写代码工具类
package com.xs.wastesorting.utils;
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 com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
/**
* 生成数据库文档工具类
*/
public class DatabaseDesignDocUtil {
public static void main(String[] args) {
//执行方法
documentGeneration();
}
/**
* 文档生成
*/
public static void documentGeneration() {
//数据源
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
//数据库连接地址 xxxx是需要连接的数据库名称
hikariConfig.setJdbcUrl("jdbc:mysql://192.168.1.110:3306/xxxx");
//数据库用户名
hikariConfig.setUsername("root");
//数据库密码
hikariConfig.setPassword("root");
//设置可以获取tables remarks信息
hikariConfig.addDataSourceProperty("useInformationSchema", "true");
hikariConfig.setMinimumIdle(2);
hikariConfig.setMaximumPoolSize(5);
DataSource dataSource = new HikariDataSource(hikariConfig);
//生成配置
EngineConfig engineConfig = EngineConfig.builder()
//生成文件路径
.fileOutputDir("G:\\dbDoc\\")
//打开目录
.openOutputDir(true)
//文件类型
.fileType(EngineFileType.WORD)
//生成模板实现
.produceType(EngineTemplateType.freemarker)
//自定义文件名称
.fileName("萧山垃圾分类数据库设计文档").build();
//忽略表
List<String> ignoreTableName = new ArrayList<>();
ignoreTableName.add("test_user");
ignoreTableName.add("test_group");
//忽略表前缀
ArrayList<String> ignorePrefix = new ArrayList<>();
ignorePrefix.add("ia_");
ignorePrefix.add("ie_");
ignorePrefix.add("sys_");
ignorePrefix.add("im_");
//忽略表后缀
ArrayList<String> ignoreSuffix = new ArrayList<>();
ignoreSuffix.add("_test");
ProcessConfig processConfig = ProcessConfig.builder()
//指定生成逻辑、当存在指定表、指定表前缀、指定表后缀时,将生成指定表,其余表不生成、并跳过忽略表配置
//根据名称指定表生成
.designatedTableName(new ArrayList<>())
//根据表前缀生成
.designatedTablePrefix(new ArrayList<>())
//根据表后缀生成
.designatedTableSuffix(new ArrayList<>())
//忽略表名
.ignoreTableName(ignoreTableName)
//忽略表前缀
.ignoreTablePrefix(ignorePrefix)
//忽略表后缀
.ignoreTableSuffix(ignoreSuffix).build();
//配置
Configuration config = Configuration.builder()
//版本
.version("1.0.0")
//描述
.description("数据库设计文档生成")
//数据源
.dataSource(dataSource)
//生成配置
.engineConfig(engineConfig)
//生成配置
.produceConfig(processConfig)
.build();
//执行生成
new DocumentationExecute(config).execute();
}
}
3、生成后的样子
4、生成html文件
import cn.smallbun.screw.core.Configuration;
import cn.smallbun.screw.core.execute.DocumentationExecute;
import cn.smallbun.screw.core.process.ProcessConfig;
import cn.smallbun.screw.core.process.ProcessConfigBuilder;
import cn.smallbun.screw.core.process.ProcessFileType;
import cn.smallbun.screw.core.process.ProcessManager;
import cn.smallbun.screw.core.process.container.AbstractContainer;
import cn.smallbun.screw.core.process.container.DefaultFileOutput;
import cn.smallbun.screw.core.process.container.DefaultTableStructure;
import cn.smallbun.screw.core.process.container.FileOutput;
import cn.smallbun.screw.core.process.control.ProcessControlConfig;
import cn.smallbun.screw.core.util.StringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@SpringBootApplication
public class DatabaseDocGeneratorApplication {
public static void main(String[] args) {
SpringApplication.run(DatabaseDocGeneratorApplication.class, args);
// 数据库连接配置
DataSource dataSource = // 获取您的数据源;
// 文档生成配置
Configuration config = Configuration.builder()
.version("1.0.0")
.dataSource(dataSource)
.engineConfig(getEngineConfig())
.produceConfig(getProcessConfig())
.build();
// 文档生成执行
new DocumentationExecute(config).execute();
}
private static ProcessConfig getProcessConfig() {
ProcessConfig processConfig = ProcessConfig.builder()
.fileOutput(getFileOutput())
.processType(ProcessFileType.HTML) // 生成HTML格式的文档
.build();
processConfig.getPackageConfig().setUrl("your.package.name"); // 设置需要生成文档的包路径
return processConfig;
}
private static FileOutput getFileOutput() {
return new DefaultFileOutput("database-documentation", ""); // 指定输出目录
}
private static AbstractContainer getEngineConfig() {
ProcessControlConfig processControlConfig = ProcessControlConfig.builder()
.designatedTableName(new ArrayList<>())
.ignoreTableName(Arrays.asList("your_ignore_table_1", "your_ignore_table_2")) // 忽略不生成文档的表名
.build();
return new DefaultTableStructure(processControlConfig);
}
}
最后直接运行main方法即可生成数据库设计文档!
方式二:使用maven插件生成
1、注意:使用maven插件依旧要引入screw依赖
<dependency>
<groupId>cn.smallbun.screw</groupId>
<artifactId>screw-core</artifactId>
<version>1.0.5</version>
</dependency>
2、引入maven插件
<build>
<plugins>
<plugin>
<groupId>cn.smallbun.screw</groupId>
<artifactId>screw-maven-plugin</artifactId>
<version>${lastVersion}</version>
<dependencies>
<!-- HikariCP -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
<!--mysql driver-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
</dependencies>
<configuration>
<!--username-->
<username>登录名</username>
<!--password-->
<password>数据库密码</password>
<!--driver-->
<driverClassName>com.mysql.cj.jdbc.Driver</driverClassName>
<!--jdbc url 数据库连接地址-->
<jdbcUrl>jdbc:mysql://127.0.0.1:3306/xxxx?serverTimezone=UTC</jdbcUrl>
<!--生成文件类型-->
<fileType>HTML</fileType>
<!--打开文件输出目录-->
<openOutputDir>false</openOutputDir>
<!--生成模板-->
<produceType>freemarker</produceType>
<!--文档名称 为空时:将采用[数据库名称-描述-版本号]作为文档名称-->
<fileName>测试文档名称</fileName>
<!--描述-->
<description>数据库文档生成</description>
<!--版本-->
<version>${project.version}</version>
<!--标题-->
<title>数据库文档</title>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
3、运行maven插件
生成html文档