hj_screw导出数据库表文档

发布时间 2023-06-12 12:01:58作者: 独孤~华剑
 # 要不要排除掉依赖,需要视项目导入的依赖情况而定.
# 这要排除掉依赖,主要是因为项目使用的log4j2排除了boot自身的日志
具体如下:
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
                <!-- 屏蔽 tomcat容器 -->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--  undertow 容器  https://www.bilibili.com/video/av544954453/?p=2  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
所以~下面这俩需要排除下依赖
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 下面这俩是用来导出数据库文档的 -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.32</version>
        </dependency>
        <dependency>
            <groupId>cn.smallbun.screw</groupId>
            <artifactId>screw-core</artifactId>
            <version>1.0.5</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>

            </exclusions>
        </dependency>
        <!-- 上面这俩是用来导出数据库文档的 -->
pom引入

项目数据源连接.如果用的 mybatis-plus多数据源也是不行的哦.配置示例如下

pom
 <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>
        <!-- 多数据源支持 配置了这个 就要配主数据库哦-->
        <!-- Error querying database.  Cause: com.baomidou.dynamic.datasource.exception.CannotFindDataSourceException: dynamic-datasource can not find primary datasource-->
        <!-- https://mvnrepository.com/artifact/com.baomidou/dynamic-datasource-spring-boot-starter -->
        <!--<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
        </dependency>-->
需要注释掉这个多数据源~
yml
spring:
  # mysql 主从配置
  datasource:
    dynamic:
      # 设置默认的数据源或者数据源组,默认值即为master
      primary: master_1
      datasource:
        master_1:
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://120.78.129.105:3308/hj_boot?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
          username: root
          password: 123456
        slave_1:
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://120.78.129.105:3309/hj_boot?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
          username: root
          password: 123456
        master_2:
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://120.78.129.105:3310/hj_boot?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
          username: root
          password: 123456
        slave_2:
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://120.78.129.105:3311/hj_boot?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
          username: root
          password: 123456
    hikari:
      connection-timeout: 30000     # 等待连接池分配链接的最大时长(毫秒),超过这个时长还没有可用的连接则发生 SQLException,默认:30 秒
      minimum-idle: 2               # 最小空闲连接数
      maximum-pool-size: 10         # 最大连接数
      auto-commit: true             # 自动提交
      idle-timeout: 600000          # 连接超时的最大时长(毫秒),超时则被释放(retired),默认:10 分钟
      max-lifetime: 1800000         # 连接的生命时长(毫秒),超时而且没被使用则被释放(retired),默认: 30 分钟
      connection-test-query: SELECT 1
      pool-name: HjHikariPool
# 上面这种行不通~怎么才可行懒的去研究
主从多数据源不行的啦

正确能导出的示例如下:

pom
  <!-- 下面这俩是用来导出数据库文档的 -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.32</version>
        </dependency>
        <dependency>
            <groupId>cn.smallbun.screw</groupId>
            <artifactId>screw-core</artifactId>
            <version>1.0.5</version>
        </dependency>
 <!-- 上面这俩是用来导出数据库文档的 -->
yml配置单数据源
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://120.78.129.105:3307/hj_boot?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
    username: root
    password: 123456
    hikari:
      connection-timeout: 30000     # 等待连接池分配链接的最大时长(毫秒),超过这个时长还没有可用的连接则发生 SQLException,默认:30 秒
      minimum-idle: 2               # 最小空闲连接数
      maximum-pool-size: 10         # 最大连接数
      auto-commit: true             # 自动提交
      idle-timeout: 600000          # 连接超时的最大时长(毫秒),超时则被释放(retired),默认:10 分钟
      max-lifetime: 1800000         # 连接的生命时长(毫秒),超时而且没被使用则被释放(retired),默认: 30 分钟
      connection-test-query: SELECT 1
      pool-name: HjHikariPool
View Code

记得依赖关系的正确处理~

然后写个测试类.导出文件 可选 .md  .html .doc

package com.hj.server;

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 jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author hj
 * @Date 2023/6/12
 * @Description:
 */
@SpringBootTest(classes = HjServerApplication.class)
public class ScrewTest {

    @Resource
    private ApplicationContext applicationContext;

    @Test
    public void testOne() {
        System.out.println("开始测试~");
        DataSource dataSourceMysql = applicationContext.getBean(DataSource.class);
        // 生成文件配置
        EngineConfig engineConfig = EngineConfig.builder()
                // 生成文件路径,自己mac本地的地址,这里需要自己更换下路径
                .fileOutputDir("D:\\hj\\frame\\boot\\hj-server")
                // 打开目录
                .openOutputDir(false)
                // 文件类型
                .fileType(EngineFileType.WORD)
                // 生成模板实现
                .produceType(EngineTemplateType.freemarker).build();

        // 生成文档配置(包含以下自定义版本号、描述等配置连接)
        Configuration config = Configuration.builder()
                .version("1.0.5")
                .description("生成~文档信息描述")
                .dataSource(dataSourceMysql)
                .engineConfig(engineConfig)
                .produceConfig(getProcessConfig())
                .build();

        // 执行生成
        new DocumentationExecute(config).execute();
        System.out.println("测试完毕");
    }

    /**
     * 配置想要生成的表+ 配置想要忽略的表
     *
     * @return 生成表配置
     */
    public ProcessConfig getProcessConfig() {

        // 忽略表名
        List<String> ignoreTableName = Arrays.asList("lol", "dnf");
        // 忽略表前缀,如忽略a开头的数据库表
        List<String> ignorePrefix = Arrays.asList("abc_", "def");
        // 忽略表后缀
        List<String> ignoreSuffix = Arrays.asList("_test", "screw_");

        return ProcessConfig.builder()
                //根据名称指定表生成
                .designatedTableName(new ArrayList<>())
                //根据表前缀生成
                .designatedTablePrefix(new ArrayList<>())
                //根据表后缀生成
                .designatedTableSuffix(new ArrayList<>())
                //忽略表名
                .ignoreTableName(ignoreTableName)
                //忽略表前缀
                .ignoreTablePrefix(ignorePrefix)
                //忽略表后缀
                .ignoreTableSuffix(ignoreSuffix).build();
    }


}
Test-Class