关于java:实用一键生成数据库文档堪称数据库界的Swagger

本文收录在集体博客:www.chengxy-nds.top,技术材料共享,同提高

最近部门订单业务调整,收拢其余业务线的下单入口,做个对立大订单平台。须要梳理各业务线的数据表,但每个业务线库都有近百张和订单相干的表,挨个表一个一个字段的弄脑瓜子嗡嗡的。

为了不反复 CV 操作,抱着一丝心愿开始在GitHub里找,看看有没有什么工具能够用,后果就真的发现了宝藏,screw(螺丝钉),竟然能够生成数据库文档,优良啊~。

一、数据库反对

  • [x] MySQL
  • [x] MariaDB
  • [x] TIDB
  • [x] Oracle
  • [x] SqlServer
  • [x] PostgreSQL
  • [x] Cache DB

二、配置

1、pom文件

引入screw外围包,HikariCP数据库连接池,HikariCP号称性能最出色的数据库连接池。

<!-- screw外围 -->
<dependency>
    <groupId>cn.smallbun.screw</groupId>
    <artifactId>screw-core</artifactId>
    <version>1.0.3</version>
</dependency>

<!-- 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>

2、配置数据源

配置数据源,设置 useInformationSchema 能够获取tables正文信息。

spring.datasource.url=jdbc:mysql://45.93.1.5:3306/fire?useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.xa.properties.useInformationSchema=true

3、screw 外围配置

screw有两种执行形式,第一种是pom文件配置,另一种是代码执行。

 <build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>cn.smallbun.screw</groupId>
            <artifactId>screw-maven-plugin</artifactId>
            <version>1.0.3</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>123456</password>
                <!--driver-->
                <driverClassName>com.mysql.cj.jdbc.Driver</driverClassName>
                <!--jdbc url-->
                <jdbcUrl>jdbc:mysql://41.92.6.5:3306/fire</jdbcUrl>
                <!--生成文件类型-->
                <fileType>HTML</fileType>
                <!--关上文件输入目录-->
                <openOutputDir>false</openOutputDir>
                <!--生成模板-->
                <produceType>freemarker</produceType>
                <!--文档名称 为空时:将采纳[数据库名称-形容-版本号]作为文档名称-->
                <!--<docName>测试文档名称</docName>-->
                <!--形容-->
                <description>数据库文档生成</description>
                <!--版本-->
                <version>${project.version}</version>
                <!--题目-->
                <title>fire数据库文档</title>
            </configuration>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

配置完当前在 maven project->screw 双击执行ok。

代码生成形式也非常简单。

@SpringBootTest
public class ScrewApplicationTests {

    @Autowired
    ApplicationContext applicationContext;

    @Test
    void contextLoads() {
        DataSource dataSourceMysql = applicationContext.getBean(DataSource.class);
        // 生成文件配置
        EngineConfig engineConfig = EngineConfig.builder()
                // 生成文件门路,本人mac本地的地址,这里须要本人更换下门路
                .fileOutputDir("D:/")
                // 关上目录
                .openOutputDir(false)
                // 文件类型
                .fileType(EngineFileType.HTML)
                // 生成模板实现
                .produceType(EngineTemplateType.freemarker).build();
        // 生成文档配置(蕴含以下自定义版本号、形容等配置连贯)
        Configuration config = Configuration.builder()
                .version("1.0.3")
                .description("生成文档信息形容")
                .dataSource(dataSourceMysql)
                .engineConfig(engineConfig)
                .produceConfig(getProcessConfig())
                .build();
        // 执行生成
        new DocumentationExecute(config).execute();
    }

    /**
     * 配置想要生成的表+ 配置想要疏忽的表
     *
     * @return 生成表配置
     */
    public static ProcessConfig getProcessConfig() {
        // 疏忽表名
        List<String> ignoreTableName = Arrays.asList("a", "test_group");
        // 疏忽表前缀,如疏忽a结尾的数据库表
        List<String> ignorePrefix = Arrays.asList("a", "t");
        // 疏忽表后缀
        List<String> ignoreSuffix = Arrays.asList("_test", "czb_");
        return ProcessConfig.builder()
                //依据名称指定表生成
                .designatedTableName(Arrays.asList("fire_user"))
                //依据表前缀生成
                .designatedTablePrefix(new ArrayList<>())
                //依据表后缀生成
                .designatedTableSuffix(new ArrayList<>())
                //疏忽表名
                .ignoreTableName(ignoreTableName)
                //疏忽表前缀
                .ignoreTablePrefix(ignorePrefix)
                //疏忽表后缀
                .ignoreTableSuffix(ignoreSuffix).build();
    }
}

4、文档格局

screwHTMLDOCMD 三种格局的文档。

代码中的批改

.fileType(EngineFileType.HTML)

或者pom文件

<fileType>MD</fileType>

DOC文档款式

HTML文档款式

MD文档款式

不得不说这个工具是真TM好用,提前完成工作,有点傲娇有木有!

原创不易,焚烧秀发输入内容,如果有一丢丢播种,点个赞激励一下吧!

习惯在VX看技术文章,想要获取更多Java资源的同学,能够关注我的公众号:程序员内点事,暗号:[666]

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理