掘金 后端 ( ) • 2024-04-07 17:47

大家好,我是 Java陈序员

在企业级开发中,我们经常会有编写数据库表结构文档的需求,常常需要手写维护文档,很是繁琐。

今天,给大家介绍一款数据库表结构文档生成工具。

关注微信公众号:【Java陈序员】,获取开源项目分享、AI副业分享、超200本经典计算机电子书籍等。

项目介绍

screw —— 螺丝钉(代表企业级开发中一颗永不生锈的螺丝钉),是一款简洁好用的数据库表结构文档生成工具。

screw 主打简洁、轻量,支持多种数据库、多种格式文档,可自定义模板进行灵活拓展。

  • 支持 MySQL、MariaDB、TIDB、Oracle 多种数据库

  • 支持生成 HTML、Word、MarkDown 三种格式的文档

快速上手

screw 普通方式Maven 插件的两种方式来生成文档。

普通方式

1、引入依赖

<!-- 引入数据库驱动,这里以 MySQL 为例 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.29</version>
</dependency>

<!-- 引入 screw -->
<dependency>
    <groupId>cn.smallbun.screw</groupId>
    <artifactId>screw-core</artifactId>
    <version>1.0.5</version>
 </dependency>

2、编写代码

public class DocumentGeneration {

    /**
     * 文档生成
     */
    @Test
    public void documentGeneration() {

        // 文档生成路径
        String fileOutputPath = "D:\\database";

        // 数据源
        HikariConfig hikariConfig = new HikariConfig();
        // 指定数据库驱动
        hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
        // 设置数据库连接地址
        hikariConfig.setJdbcUrl("jdbc:mysql://localhost:3306/database");
        // 设置数据库用户
        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(fileOutputPath)
                // 打开目录
                .openOutputDir(true)
                // 文件类型 HTML、WORD、MD 三种类型
                .fileType(EngineFileType.HTML)
                // 生成模板实现
                .produceType(EngineTemplateType.freemarker)
                // 自定义文件名称
                .fileName("Document")
                .build();

        // 忽略表
        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");

        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、执行代码输出文档

Maven 插件

1、引入依赖

<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>root</username>
                <!-- 数据库密码 -->
                <password>password</password>
                <!-- 数据库驱动 -->
                <driverClassName>com.mysql.cj.jdbc.Driver</driverClassName>
                <!-- 数据库连接地址 -->
                <jdbcUrl>jdbc:mysql://127.0.0.1:3306/xxxx</jdbcUrl>
                <!-- 生成的文件类型 HTML、WORD、MD 三种类型 -->
                <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>

2、执行插件

3、使用 Maven 插件执行的方式会将文档输出到项目根目录的 doc 目录下

文档截图

HTML 类型文档

Word 类型文档

MarkDown 类型文档

自从用了 screw 后,编写数据库文档信息就很方便了,一键生成,剩下的时间就可以用来摸鱼了~

大家如果下次有需要编写数据库文档,可以考虑使用 screw ,建议先把本文收藏起来,下次就不会找不到了~

最后,贴上项目地址:

https://github.com/pingfangushi/screw

最后

推荐的开源项目已经收录到 GitHub 项目,欢迎 Star

https://github.com/chenyl8848/great-open-source-project

或者访问网站,进行在线浏览:

https://chencoding.top:8090/#/

大家的点赞、收藏和评论都是对作者的支持,如文章对你有帮助还请点赞转发支持下,谢谢!