前言

Github:https://github.com/HealerJean

博客:http://blog.healerjean.com

来源:

1、https://my.oschina.net/mdxlcj/blog/4341399

2、https://mp.weixin.qq.com/s/Nh9lEpJlMn1ndVmA5BKGQg

1、pom

<!--数据库文档生成 最新版本 https://mvnrepository.com/artifact/cn.smallbun.screw/screw-core-->
<dependency>
  <groupId>cn.smallbun.screw</groupId>
  <artifactId>screw-core</artifactId>
  <version>1.0.5</version>
</dependency>
<dependency>
  <groupId>org.freemarker</groupId>
  <artifactId>freemarker</artifactId>
  <version>2.3.30</version>
</dependency>

2、数据源配置


####################################
### DB
####################################
spring.datasource.druid.url=jdbc:mysql://localhost:3306/healerjean?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource.druid.username=root
spring.datasource.druid.password=123456
####################################


####################################
### DB markdown
####################################
db.markdown.dir=/Users/healerjean/Desktop
####################################

3、DatabaseMarkdownConfig

package com.healerjean.proj.config;

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 com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.util.ArrayList;

@org.springframework.context.annotation.Configuration
  public class DatabaseMarkdownConfig {

    @Autowired
    private ApplicationContext applicationContext;
    @Value("${spring.datasource.druid.driver-class-name}")
    private String  driverClassName ;
    @Value("${spring.datasource.druid.url}")
    private String  jdbcUrl ;
    @Value("${spring.datasource.druid.username}")
    private String  username ;
    @Value("${spring.datasource.druid.password}")
    private String  password ;

    @Value("${db.markdown.dir}")
    private String  dbMarkdownDir ;


    @PostConstruct
    public void init() {
      //数据源
      HikariConfig hikariConfig = new HikariConfig();
      // hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
      hikariConfig.setDriverClassName(driverClassName);
      hikariConfig.setJdbcUrl(jdbcUrl);
      hikariConfig.setUsername(username);
      hikariConfig.setPassword(password);
      //设置可以获取tables remarks信息
      hikariConfig.addDataSourceProperty("useInformationSchema", "true");
      hikariConfig.setMinimumIdle(2);
      hikariConfig.setMaximumPoolSize(5);
      DataSource dataSource = new HikariDataSource(hikariConfig);
      //生成配置
      EngineConfig engineConfig = EngineConfig.builder()
        //生成文件路径
        .fileOutputDir(dbMarkdownDir)
        //打开目录
        .openOutputDir(true)
        //文件类型 (html/markdown/word)
        .fileType(EngineFileType.HTML)
        //生成模板实现
        .produceType(EngineTemplateType.freemarker)
        //自定义文件名称
        .fileName("自定义文件名称").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();
    }

  }

4、展示

image-20220325104335046

ContactAuthor