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

张春  金牌会员 | 2023-10-15 06:24:10 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 918|帖子 918|积分 2754

很多时候,我们为了着急忙慌赶项目进度,很容易忽略整理文档这件事
某一天,领导心血来潮,要搞一次突击检查, 想看看我们的数据库设计的是否规范, 但他又不想亲自去数据库查验(毕竟这么大领导)
那么,我们该怎么办?
第一种方法:离职,世界那么大,我想去看看(我相信一般人不会这么做)
也许你可以试试下面这种方法
此方法大概属于奇技淫巧,建议在工作中多用
基本思路就是通过Java代码自动生成数据库表结构文档
在Java中正好有个包可以实现这个功能

  • POM文件引入相关包
  1. ...
  2. <dependency>
  3.             <groupId>org.freemarker</groupId>
  4.             <artifactId>freemarker</artifactId>
  5.             <version>2.3.30</version>
  6.         </dependency>
  7.   <dependency>
  8.       <groupId>cn.smallbun.screw</groupId>
  9.       <artifactId>screw-core</artifactId>
  10.       <version>1.0.3</version>
  11.   </dependency>
复制代码

  • 配置数据库
  1. spring:
  2.   datasource:
  3.     url: jdbc:mysql://localhost:3306/xxx?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false&allowMultiQueries=true&allowPublicKeyRetrieval=true
  4.     username: root
  5.     password: xxx
  6.     driver-class-name: com.mysql.cj.jdbc.Driver
复制代码

  • 编写转换代码
  1. package com.example.createdoc;
  2. import cn.smallbun.screw.core.Configuration;
  3. import cn.smallbun.screw.core.engine.EngineConfig;
  4. import cn.smallbun.screw.core.engine.EngineFileType;
  5. import cn.smallbun.screw.core.engine.EngineTemplateType;
  6. import cn.smallbun.screw.core.execute.DocumentationExecute;
  7. import cn.smallbun.screw.core.process.ProcessConfig;
  8. import org.assertj.core.util.Lists;
  9. import org.junit.Test;
  10. import org.junit.runner.RunWith;
  11. import org.springframework.boot.test.context.SpringBootTest;
  12. import org.springframework.test.context.junit4.SpringRunner;
  13. import javax.annotation.Resource;
  14. import javax.sql.DataSource;
  15. /**
  16. * <h1>数据库表文档生成</h1>
  17. * */
  18. @SpringBootTest
  19. @RunWith(SpringRunner.class)
  20. public class Table2DocTest {
  21.     @Resource
  22.     private DataSource dataSource;
  23.     @Test
  24.     public void buildDBDoc() {
  25.         EngineConfig engineConfig = EngineConfig.builder()
  26.                 // 生成文件路径
  27.                 .fileOutputDir("D:\\temp")
  28.                 .openOutputDir(false)
  29.                 // 文件类型 支持 world,html, md
  30.                 .fileType(EngineFileType.WORD)
  31.                 .produceType(EngineTemplateType.freemarker).build();
  32.         // 生成文档配置, 包含自定义版本号、描述等等
  33.         Configuration config = Configuration.builder()
  34.                 .version("1.0.0")
  35.                 .description("瞎编数据库")
  36.                 .dataSource(dataSource)
  37.                 .engineConfig(engineConfig)
  38.                 .produceConfig(
  39.                     // 表的生成规则和忽略规则在这里配置
  40.                      ProcessConfig.builder()
  41.                             .designatedTableName(Lists.newArrayList())
  42.                             .designatedTablePrefix(Lists.newArrayList())
  43.                             .designatedTableSuffix(Lists.newArrayList())
  44.                             .ignoreTableName(Lists.newArrayList())
  45.                             .ignoreTablePrefix(Lists.newArrayList())
  46.                             .ignoreTableSuffix(Lists.newArrayList())
  47.                             .build()
  48.                 ).build();
  49.         // 执行生成
  50.         new DocumentationExecute(config).execute();
  51.     }
  52.    
  53. }
复制代码
我们用的这个组件 screw 可以实现生成doc、md、html类型的文档,生成的目录在代码中可配置

就这样,该下班下班,该下课下课
    微信公众号
  
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

张春

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表