Github上一款一键生成数据库文档的大利器!

介绍

今天看了 Guide老哥 公众号(需要的朋友可以关注一波: JavaGuide)中的一篇文章,发现了一款好用的数据库文档生成工具。

在项目中开发中,有没有遇到过编写数据库说明文档。一般情况下,数据库说明文档中有着大量的数据库表结构,如果手动进行维护,将会耗费大量时间,这样就不能愉快的进行摸鱼了。

所以呢,为了解决这个问题,Github 上的大佬开源了一款数据库表结构文档自动生成工具 —— screw
在这里插入图片描述
screw项目地址:https://github.com/pingfangushi/screw

screw 翻译过来的意思就是螺丝钉,作者希望这个工具能够像螺丝钉一样切实地帮助到我们的开发工作。

目前的话,screw 已经支持市面上大部分常见的数据库比如 MySQL、MariaDB、Oracle、SqlServer、PostgreSQL、TiDB。

快速开始

表结构脚本

为了验证 screw 自动生成数据库表结构文档的效果,我们首先创建一个简单的存放博客数据的数据库表。

CREATE TABLE `screw-demo-blog` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `title` varchar(255) NOT NULL COMMENT '博客标题',
  `content` longtext NOT NULL COMMENT '博客内容',
  `description` varchar(255) DEFAULT NULL COMMENT '博客简介',
  `cover` varchar(255) DEFAULT NULL COMMENT '博客封面图片地址',
  `views` int(11) NOT NULL DEFAULT '0' COMMENT '博客阅读次数',
  `user_id` bigint(20) DEFAULT '0' COMMENT '发表博客的用户ID',
  `channel_id` bigint(20) NOT NULL COMMENT '博客分类ID',
  `recommend` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否推荐',
  `top` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否置顶',
  `comment` bit(1) NOT NULL DEFAULT b'1' COMMENT '是否开启评论',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8mb4 COMMENT='博客';

基于 Java 代码

引入 screw 及相关依赖

创建一个普通的maven项目,导入以下依赖

<!--screw-->
<dependency>
    <groupId>cn.smallbun.screw</groupId>
    <artifactId>screw-core</artifactId>
    <version>1.0.5</version>
</dependency>
<!-- HikariCP -->
<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>3.4.5</version>
</dependency>
<!--MySQL-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.20</version>
</dependency>

你可以通过下面的地址在 mvnrepository 获取最新版本的 screw。

https://mvnrepository.com/artifact/cn.smallbun.screw/screw-core

代码编写

生成数据库文档的代码的整个代码逻辑还是比较简单的,我们只需要经过下面 5 步即可:

1、获取数据库源

/**
 * 获取数据库源
 */
private static DataSource getDataSource() {
    //数据源
    HikariConfig hikariConfig = new HikariConfig();
    hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
    hikariConfig.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test");
    hikariConfig.setUsername("root");
    hikariConfig.setPassword("root");
    //设置可以获取tables remarks信息
    hikariConfig.addDataSourceProperty("useInformationSchema", "true");
    hikariConfig.setMinimumIdle(2);
    hikariConfig.setMaximumPoolSize(5);
    return new HikariDataSource(hikariConfig);
}

2、获取文件生成配置

/**
 * 获取文件生成配置
 */
private static EngineConfig getEngineConfig() {
    //生成配置
    return EngineConfig.builder()
            //生成文件路径
            .fileOutputDir("/Users/guide/Documents/代码示例/screw-demo/doc")
            //打开目录
            .openOutputDir(true)
            //文件类型
            .fileType(EngineFileType.HTML)
            //生成模板实现
            .produceType(EngineTemplateType.freemarker)
            //自定义文件名称
            .fileName("数据库结构文档").build();
}

注意,如果不配置生成文件路径的话,默认也会存放在项目的 doc 目录下。

另外,我们这里指定生成的文件格式为 HTML。除了 HTML 之外,screw 还支持 Word 、Markdown 这两种文件格式。

3、获取数据库表的处理配置
可以在这里指定只生成哪些表结构(这个是可选项)

/**
 * 获取数据库表的处理配置,可忽略
 */
private static ProcessConfig getProcessConfig() {
    return ProcessConfig.builder()
      // 指定只生成 blog 表
      .designatedTableName(new ArrayList<>(Collections.singletonList("screw-demo-blog")))
      .build();
}

这里进行设置需要忽略生成的表结构(这个也是可选项)

private static ProcessConfig getProcessConfig() {
    ArrayList<String> ignoreTableName = new ArrayList<>();
    ignoreTableName.add("test_user");
    ignoreTableName.add("test_group");
    ArrayList<String> ignorePrefix = new ArrayList<>();
    ignorePrefix.add("test_");
    ArrayList<String> ignoreSuffix = new ArrayList<>();
    ignoreSuffix.add("_test");
    return ProcessConfig.builder()
            //忽略表名
            .ignoreTableName(ignoreTableName)
            //忽略表前缀
            .ignoreTablePrefix(ignorePrefix)
            //忽略表后缀
            .ignoreTableSuffix(ignoreSuffix)
            .build();
}

如果以上设置都没有注定 ProcessConfig 进行配置的话,就会按照默认配置来!

4、生成 screw 完整配置

private static Configuration getScrewConfig(DataSource dataSource, EngineConfig engineConfig, ProcessConfig processConfig) {
    return Configuration.builder()
            //版本
            .version("1.0.0")
            //描述
            .description("数据库设计文档生成")
            //数据源
            .dataSource(dataSource)
            //生成配置
            .engineConfig(engineConfig)
            //生成配置
            .produceConfig(processConfig)
            .build();
}

5、调用执行

private static Configuration getScrewConfig(DataSource dataSource, EngineConfig engineConfig, ProcessConfig processConfig) {
    return Configuration.builder()
            //版本
            .version("1.0.0")
            //描述
            .description("数据库设计文档生成")
            //数据源
            .dataSource(dataSource)
            //生成配置
            .engineConfig(engineConfig)
            //生成配置
            .produceConfig(processConfig)
            .build();
}

生成结果效果展示(.html):
在这里插入图片描述

基于 Maven 插件

除了基于 Java 代码这种方式之外,你还可以通过 screw 提供的 Maven 插件来生成数据库文档。个人感觉maven插件的方式更加简单!

引入screw及相关依赖

这一步操作还是一样,保持不变

<!--screw-->
<dependency>
    <groupId>cn.smallbun.screw</groupId>
    <artifactId>screw-core</artifactId>
    <version>1.0.5</version>
</dependency>
<!-- HikariCP -->
<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>3.4.5</version>
</dependency>
<!--MySQL-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.20</version>
</dependency>

配置maven-plugins

这里还是在以上的依赖 pom.xml 文件中进行配置:

<build>
    <plugins>
        <plugin>
            <groupId>cn.smallbun.screw</groupId>
            <artifactId>screw-maven-plugin</artifactId>
            <version>1.0.5</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>root</username>
                <!--password-->
                <password>root</password>
                <!--driver-->
                <driverClassName>com.mysql.cj.jdbc.Driver</driverClassName>
                <!--jdbc url-->
                <jdbcUrl>jdbc:mysql://127.0.0.1:3306/test</jdbcUrl>
                <!--生成文件类型-->
                <fileType>MD</fileType>
                <!--打开文件输出目录-->
                <openOutputDir>true</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>

手动执行生成数据库文档

在这里插入图片描述
生成结果效果展示(.md):
在这里插入图片描述

成品地址

这里呢,我已经把以上的示例代码上传到我码云仓库中,需要的朋友直接clone下来就可以运行使用。
本博客示例代码获取地址:https://gitee.com/zhuziccEE/screw-code.git


如果感觉文章对你有帮助,就给点个赞吧!

  • 16
    点赞
  • 91
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 16
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhuzicc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值