数据库表结构文档生成工具

数据库表结构文档生成工具

我们经常会有编写数据库表结构文档的时间付出,关于数据库表结构文档状态:

要么没有、要么有、但都是手写、后期运维开发,需要手动进行维护到文档中,很是繁琐、

如果忘记一次维护、就会给以后工作造成很多困扰。

推荐一款插件用来生成数据库文档。

screw插件

screw特点

  • 简洁、轻量、设计良好
  • 多数据库支持
  • 多种格式文档
  • 灵活扩展
  • 支持自定义模板

数据库支持

  • MySQL
  • MariaDB
  • TIDB
  • Oracle
  • SqlServer
  • PostgreSQL
  • Cache DB

文档生成支持

  • html
  • word
  • markdwon

文档截图

SpringBoot整合screw生成数据库文档

方法一:代码生成

1.1 添加依赖

添加screw需要的依赖,检查是否最新版本,链接地址->最新版本

<!--freemarker模版-->
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.30</version>
</dependency>
 
<!--screw依赖-->
<dependency>
    <groupId>cn.smallbun.screw</groupId>
    <artifactId>screw-core</artifactId>
    <version>1.0.2</version>
</dependency>

<!--mysql -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

1.2 测试类代码

package com.groot.screw;
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 org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;


@SpringBootTest
public class ScrewApplicationTests {
    
    

	@Test
	public void contextLoads() {
    
    
		//数据源:HikariCP 线程池, SpringBoot 2.0开始内置了HikariCP,2.0之前的版本需要引入依赖
		HikariConfig hikariConfig = new HikariConfig();
		// com.mysql.jdbc.Driver MySQL5驱动;com.mysql.cj.jdbc.Driver MySQL6之后的驱动
		hikariConfig.setDriverClassName("com.mysql.jdbc.Driver");
		hikariConfig.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/oct?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8");
		hikariConfig.setUsername("root");
		hikariConfig.setPassword("1q2w3e");
		//设置可以获取tables remarks信息
		hikariConfig.addDataSourceProperty("useInformationSchema", "true");
		hikariConfig.setMinimumIdle(2);
		hikariConfig.setMaximumPoolSize(5);
		DataSource dataSource = new HikariDataSource(hikariConfig);

		// 1、生成文件配置
		EngineConfig engineConfig = EngineConfig.builder()
				//生成文件路径(改成自己的生成路径)
				.fileOutputDir("/Users/Manager/Desktop")
				//生成后是否立即打开目录
				.openOutputDir(true)
				//文件类型 有HTML、WORD、MD三种枚举选择
				.fileType(EngineFileType.WORD)
				//生成模板实现
				.produceType(EngineTemplateType.freemarker).build();

		// 忽略表名(可选)
		List<String> ignoreTableName = Arrays.asList("aa", "test_group");
		// 忽略表前缀(可选)
		List<String> ignorePrefix = Collections.singletonList("czb_");
		// 忽略表后缀(可选)
		List<String> ignoreSuffix = Arrays.asList("_test", "_test1");

		// 2、配置想要忽略的表(可选)
		ProcessConfig processConfig = ProcessConfig.builder()
				.ignoreTableName(ignoreTableName)
				.ignoreTablePrefix(ignorePrefix)
				.ignoreTableSuffix(ignoreSuffix)
				.build();

		// 3、生成文档配置(包含以下自定义版本号、标题、描述(数据库名 + 描述 = 文件名)等配置连接)
		Configuration config = Configuration.builder()
				.version("1.0.0")
				.title("数据库文档")
				.description("数据库设计文档生成")
				.dataSource(dataSource)
				.engineConfig(engineConfig)
				.produceConfig(processConfig).build();

		// 4、执行生成
		new DocumentationExecute(config).execute();
	}
}

方法二:通过插件的形式生成文档

在pom.xml文件中加入如下配置

<build>
    <plugins>
        <plugin>
            <groupId>cn.smallbun.screw</groupId>
            <artifactId>screw-maven-plugin</artifactId>
            <version>1.0.2</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>5.1.48</version>
                </dependency>
            </dependencies>
            <configuration>
                <!--username-->
                <username>数据库帐号</username>
                <!--password-->
                <password>数据库密码</password>
                <!--driver  com.mysql.jdbc.Driver MySQL5驱动;com.mysql.cj.jdbc.Driver MySQL6之后的驱动-->
                <driverClassName>com.mysql.jdbc.Driver</driverClassName>
                <!--jdbc url-->
                <jdbcUrl>jdbc:mysql://127.0.0.1:3306/数据库名</jdbcUrl>
                <!--生成文件类型 HTML、WORD、MD三种选择-->
                <fileType>HTML</fileType>
                <!--文件输出目录-->
                <fileOutputDir>E:</fileOutputDir>
                <!--打开文件输出目录-->
                <openOutputDir>true</openOutputDir>
                <!--生成模板-->
                <produceType>freemarker</produceType>
                <!--描述-->
                <description>数据库文档生成</description>
                <!--版本-->
                <version>1.0.0</version>
                <!--标题-->
                <title>数据库文档</title>
            </configuration>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

之后运行maven插件生成即可

screw 码云地址:https://gitee.com/leshalv/screw

screwdemo码云地址:https://gitee.com/VincentBlog/Groot.git

下图为我上传到码云的项目:若不想新建项目可以从码云上获取到我的项目,然后修改数据库账号和密码即可

猜你喜欢

转载自blog.csdn.net/trouble0914/article/details/107741911