Screw一键生成数据库文档工具

Screw一键生成数据库文档工具,第1张

一、引入依赖
<!--数据库文档生成工具-->
<dependency>
   <groupId>cn.smallbun.screw</groupId>
   <artifactId>screw-core</artifactId>
   <version>1.0.3</version>
</dependency>
二、增加配置

这里提供两种常用配置:Mysql、Oracle

Mysql:
# 数据库连接
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/库名?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
spring.datasource.username=用户名
spring.datasource.password=密码
spring.datasource.xa.properties.usrInformationSchema=true

# 生成数据库文档路径
filePath=C:\\Users\\Administrator\\Desktop
#文件描述
description=数据库文档
Oracle:
spring.datasource.url=jdbc:oracle:thin:@111.198.10.22:2521:newnhis
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.username=nhis2015
spring.datasource.password=nhis2015
spring.datasource.xa.properties.usrInformationSchema=true

#生成数据库文档路径
filePath=C:\\Users\\Administrator\\Desktop
#文件描述
description=数据库文档
三、写代码啦 1.创建工具类

代码如下(示例):

package com.me.bathroom.utils;

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.me.bathroom.SpringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

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

/**
 * 数据库文档生成工具类
 * Screw
 */
@Component
public class DocumentGenerationUtil {

    ApplicationContext applicationContext = SpringUtils.getApplicationContext();

    public static void run(String description,String filePath){
        DocumentGenerationUtil documentGenerationUtil = new DocumentGenerationUtil();
        documentGenerationUtil.contextLoads(description,filePath);
    }

    private void contextLoads(String description,String filePath) {
        DataSource dataSourceMysql = applicationContext.getBean(DataSource.class);

        // 生成文件配置
        EngineConfig engineConfig = EngineConfig.builder()
                // 生成文件路径
                .fileOutputDir(filePath)
                // 打开目录
                .openOutputDir(false)
                // 文件类型(目前支持html、doc、MD格式,个人体验后还是html格式生成后看起来比较舒服,建议使用)
                .fileType(EngineFileType.HTML)
                // 生成模板实现
                .produceType(EngineTemplateType.freemarker).build();

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

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


    /**
     * 配置想要生成的表+ 配置想要忽略的表
     *
     * @return 生成表配置
     */
    public static ProcessConfig getProcessConfig() {
        // 忽略表名,需要忽略的表将表名放到list中即可
        List<String> ignoreTableName = Arrays.asList("aa", "test_group");
        // 忽略表前缀,如忽略a开头的数据库表
        List<String> ignorePrefix = Arrays.asList("a", "t");
        // 忽略表后缀
        List<String> ignoreSuffix = Arrays.asList("_test", "czb_");

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


}

2.注入ApplicationContext

此类需要放在启动类同级别,代码如下:

package com.me.bathroom;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component
@Lazy(false)
public class SpringUtils implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }
}

3.调用
package com.me.bathroom.controller;

import com.me.bathroom.utils.DocumentGenerationUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 生成当前数据库文档
 */
@RequestMapping("/documentGeneration")
@RestController
public class DocumentGenerationController {

    @Value("${filePath}")
    private String filePath;
    @Value("${description}")
    private String description;

    @RequestMapping("/run")
    public String documentGeneration(){
        try{
            DocumentGenerationUtil.run(description,filePath);
        }catch (Exception e){
            e.printStackTrace();
            return "500";
        }
        return "200";
    }
}

直接调用接口就好啦,附上效果图:


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/741975.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存