实用!一键生成数据库文档,堪称数据库界的Swagger

漫画编程

共 4328字,需浏览 9分钟

 · 2020-08-08

最近部门订单业务调整,收拢其他业务线的下单入口,做个统一大订单平台。需要梳理各业务线的数据表,但每个业务线库都有近百张和订单相关的表,挨个表一个一个字段的弄脑瓜子嗡嗡的。
为了不重复 CV 操作,抱着一丝希望开始在GitHub里找,看看有没有什么工具可以用,结果就真的发现了宝藏,screw(螺丝钉),居然可以生成数据库文档,优秀啊~。
数据库文档图

一、数据库支持

    MySQL

    MariaDB

    TIDB

    Oracle

    SqlServer

    PostgreSQL

    Cache DB

二、配置

1、pom文件

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

<dependency>
    <groupId>cn.smallbun.screwgroupId>

    <artifactId>screw-coreartifactId>

    <version>1.0.3version>
dependency>


<dependency>
    <groupId>com.zaxxergroupId>

    <artifactId>HikariCPartifactId>
    <version>3.4.5version>
dependency>


<dependency>
    <groupId>mysqlgroupId>

    <artifactId>mysql-connector-javaartifactId>
    <version>8.0.20version>
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文件配置,另一种是代码执行。
 
    <plugins>
        <plugin>
            <groupId>org.springframework.bootgroupId>

            <artifactId>spring-boot-maven-pluginartifactId>

        plugin>
        <plugin>
            <groupId>cn.smallbun.screwgroupId>
            <artifactId>screw-maven-pluginartifactId>
            <version>1.0.3version>
            <dependencies>
                
                <dependency>
                    <groupId>com.zaxxergroupId>
                    <artifactId>HikariCPartifactId>
                    <version>3.4.5version>
                dependency>
                
                <dependency>
                    <groupId>mysqlgroupId>
                    <artifactId>mysql-connector-javaartifactId>
                    <version>8.0.20version>
                dependency>
            dependencies>
            <configuration>
                
                <username>rootusername>
                
                <password>123456password>
                
                <driverClassName>com.mysql.cj.jdbc.DriverdriverClassName>
                
                <jdbcUrl>jdbc:mysql://41.92.6.5:3306/firejdbcUrl>
                
                <fileType>HTMLfileType>
                
                <openOutputDir>falseopenOutputDir>
                
                <produceType>freemarkerproduceType>
                
                
                
                <description>数据库文档生成description>
                
                <version>${project.version}version>
                
                <title>fire数据库文档title>
            configuration>
            <executions>
                <execution>
                    <phase>compilephase>
                    <goals>
                        <goal>rungoal>
                    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、文档格式

screw 有 HTMLDOCMD 三种格式的文档。
代码中的修改
.fileType(EngineFileType.HTML)
或者pom文件
MD</fileType>
DOC文档样式
DOC文档
HTML文档样式
HTML文档
MD文档样式
MD文档


demo 的 GitHub 地址:
https://github.com/chengxy-nds/Springboot-Notebook/tree/master/springboot-database-doc

推荐阅读:


喜欢我可以给我设为星标哦

好文章,我“在看”
浏览 28
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报