快速生成数据库文档--screw

对接各种业务线的时候,需要梳理各业务线的数据表,但每个业务涉及近百张表,脑瓜子嗡嗡的,为了不重复CV操作,screw(螺丝钉),居然可以生成数据库文档,下面开始实操。

暂时支持数据库:

  • MySQL

  • MariaDB

  • TIDB

  • Oracle

  • SqlServer

  • PostgreSQL

  • Cache DB

 一、创建springboot项目,引入mysql驱动以及jdbc,同时引入screw,pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>sqldoc</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sqldoc</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.30</version>
        </dependency>

        <!-- 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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

二、配置application.properties数据源,设置 useInformationSchema 可以获取tables注释信息

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/database?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=Aa123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.xa.properties.useInformationSchema=true

三、编写Test类:

package com.example.sqldoc;

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.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
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;

@SpringBootTest
class SqldocApplicationTests {

	@Autowired
	ApplicationContext applicationContext;

	@Test
	void contextLoads() {
		DataSource dataSourceMysql = applicationContext.getBean(DataSource.class);

		// 生成文件配置
		EngineConfig engineConfig = EngineConfig.builder()
				// 生成文件路径,自己mac本地的地址,这里需要自己更换下路径
				.fileOutputDir("d:")
				// 打开目录
				.openOutputDir(false)
				// 文件类型
				.fileType(EngineFileType.HTML)
				// 生成模板实现
				.produceType(EngineTemplateType.freemarker).build();

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

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


	/**
	 * 配置想要生成的表+ 配置想要忽略的表
	 * @return 生成表配置
	 */
	public static ProcessConfig getProcessConfig(){
		// 忽略表名
		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();
	}

}

四、运行测试类就可以生成数据库文档,简单快捷

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MichaelYZ111

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值