领导临时要数据库文档怎么办?

很多时候,我们为了着急忙慌赶项目进度,很容易忽略整理文档这件事

某一天,领导心血来潮,要搞一次突击检查, 想看看我们的数据库设计的是否规范, 但他又不想亲自去数据库查验(毕竟这么大领导)

那么,我们该怎么办?

第一种方法:离职,世界那么大,我想去看看(我相信一般人不会这么做)

也许你可以试试下面这种方法

此方法大概属于奇技淫巧,建议在工作中多用

基本思路就是通过Java代码自动生成数据库表结构文档

在Java中正好有个包可以实现这个功能

  1. POM文件引入相关包

...
<dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.30</version>
        </dependency>
  <dependency>
      <groupId>cn.smallbun.screw</groupId>
      <artifactId>screw-core</artifactId>
      <version>1.0.3</version>
  </dependency>
  1. 配置数据库
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/xxx?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false&allowMultiQueries=true&allowPublicKeyRetrieval=true
    username: root
    password: xxx
    driver-class-name: com.mysql.cj.jdbc.Driver
  1. 编写转换代码

package com.example.createdoc;

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 org.assertj.core.util.Lists;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * <h1>数据库表文档生成</h1>
 * */
@SpringBootTest
@RunWith(SpringRunner.class)
public class Table2DocTest {

    @Resource
    private DataSource dataSource;

    @Test
    public void buildDBDoc() {

        EngineConfig engineConfig = EngineConfig.builder()
                // 生成文件路径
                .fileOutputDir("D:\\temp")
                .openOutputDir(false)
                // 文件类型 支持 world,html, md
                .fileType(EngineFileType.WORD)
                .produceType(EngineTemplateType.freemarker).build();

        // 生成文档配置, 包含自定义版本号、描述等等
        Configuration config = Configuration.builder()
                .version("1.0.0")
                .description("瞎编数据库")
                .dataSource(dataSource)
                .engineConfig(engineConfig)
                .produceConfig(
                    // 表的生成规则和忽略规则在这里配置
                     ProcessConfig.builder()
                            .designatedTableName(Lists.newArrayList())
                            .designatedTablePrefix(Lists.newArrayList())
                            .designatedTableSuffix(Lists.newArrayList())
                            .ignoreTableName(Lists.newArrayList())
                            .ignoreTablePrefix(Lists.newArrayList())
                            .ignoreTableSuffix(Lists.newArrayList())
                            .build()
                ).build();

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

    
}

我们用的这个组件 screw 可以实现生成doc、md、html类型的文档,生成的目录在代码中可配置

就这样,该下班下班,该下课下课

posted on 2023-10-06 15:37  指尖,写不尽  阅读(125)  评论(0编辑  收藏  举报

导航