mysql生成有规律的别名_一个数据库文档自动生成工具-Screw

周末发现一款数据库文档自动生成的工具项目,试了一下还不错,推荐。源码简洁,使用简单,支持多种数据库,还支持自定义输出模板。为了简便,本文采用Spring Boot Web项目,可打成jar工具使用。

我的项目结构

8a2861a96af9d017345f08983f348899.png

pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.3.0.RELEASE

com.mingo.screw

screw-jar

0.0.1-SNAPSHOT

screw-jar

Demo project for Spring Boot

jar

1.8

org.springframework.boot

spring-boot-starter-web

org.projectlombok

lombok

true

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.springframework.boot

spring-boot-starter-jdbc

mysql

mysql-connector-java

runtime

com.zaxxer

HikariCP

cn.smallbun.screw

screw-core

1.0.5

screw-jar

org.springframework.boot

spring-boot-maven-plugin

application.yml

server:

port: 8001

spring:

datasource:

driver-class-name: com.mysql.cj.jdbc.Driver

xa:

properties:

useInformationSchema: true

hikari:

connection-timeout: 60000

validation-timeout: 3000

idle-timeout: 60000

auto-commit: 'true'

minimum-idle: 5

maximum-pool-size: 5

pool-name: DatebookHikariCP

max-lifetime: 60000

url: >-

jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true&serverTimezone=UTC

username: root

password: 'root'

type: com.zaxxer.hikari.HikariDataSource

screw:

common:

organization:

organizationUrl:

title:

version: 1.0.0

description: '数据库设计文档生成'

fileOutputDir: 'C:\Users\mingo\Desktop\'

openOutputDir: false

fileType: HTML

produceType: freemarker

fileName: 'test'

ignoreTableName: test_table,test

designatedTableName:

designatedTablePrefix:

designatedTableSuffix:

ignorePrefix:

ignoreSuffix:

ScrewProperties.java

package com.mingo.screw.generate;

import cn.smallbun.screw.core.engine.EngineFileType;

import cn.smallbun.screw.core.engine.EngineTemplateType;

import lombok.Data;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.List;

/**

* Screw配置

*/

@Data

@ConfigurationProperties(prefix = "screw")

public class ScrewProperties {

/**

* 生成文件路径

*/

private String fileOutputDir;

/**

* 打开目录

*/

private Boolean openOutputDir;

/**

* 文件类型

*/

private EngineFileType fileType;

/**

* 生成模板实现

*/

private EngineTemplateType produceType;

/**

* 自定义文件名称

*/

private String fileName;

/**

* 忽略表

*/

@Value("#{'${ignoreTableName:}'.empty ? null : '${ignoreTableName:}'.split(',')}")

private List ignoreTableName;

/**

* 忽略表前缀

*/

@Value("#{'${ignorePrefix:}'.empty ? null : '${ignorePrefix:}'.split(',')}")

private List ignorePrefix;

/**

* 忽略表后缀

*/

@Value("#{'${ignoreSuffix:}'.empty ? null : '${ignoreSuffix:}'.split(',')}")

private List ignoreSuffix;

/**

* 根据名称指定表生成

*/

@Value("#{'${designatedTableName:}'.empty ? null : '${designatedTableName:}'.split(',')}")

private List designatedTableName;

/**

* 根据表前缀生成

*/

@Value("#{'${designatedTablePrefix:}'.empty ? null : '${designatedTablePrefix:}'.split(',')}")

private List designatedTablePrefix;

/**

* 根据表后缀生成

*/

@Value("#{'${designatedTableSuffix:}'.empty ? null : '${designatedTableSuffix:}'.split(',')}")

private List designatedTableSuffix;

/**

* 文档一般信息

*/

private Common common = new Common();

/**

* static

*/

@Data

public static class Common {

private String organization;

private String organizationUrl;

private String title;

/**

* 版本

*/

private String version = "1.0.0";

/**

* 描述

*/

private String description = "数据库设计文档生成";

}

}

GenerateDbDoc.java

package com.mingo.screw.generate;

import cn.smallbun.screw.core.Configuration;

import cn.smallbun.screw.core.engine.EngineConfig;

import cn.smallbun.screw.core.execute.DocumentationExecute;

import cn.smallbun.screw.core.process.ProcessConfig;

import com.zaxxer.hikari.HikariDataSource;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.ApplicationArguments;

import org.springframework.boot.ApplicationRunner;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.stereotype.Component;

/**

* 数据库文档生成

*/

@Slf4j

@Component

public class GenerateDbDoc implements ApplicationRunner {

@Autowired

private ApplicationContext ctx;

@Autowired

private ScrewProperties screwProperties;

@Autowired

private HikariDataSource dataSource;

/**

* 程序启动后执行

*

* @param args

* @throws Exception

*/

@Override

public void run(ApplicationArguments args) throws Exception {

documentGeneration();

// 安全关闭应用

((ConfigurableApplicationContext) ctx).close();

}

/**

* 文档生成

*/

private void documentGeneration() {

// 表的备注信息 配置中spring.datasource.xa.properties.useInformationSchema=true没有生效

dataSource.addDataSourceProperty("useInformationSchema", "true");

// 生成配置

EngineConfig engineConfig = EngineConfig.builder()

.fileOutputDir(screwProperties.getFileOutputDir())

.openOutputDir(screwProperties.getOpenOutputDir())

.fileType(screwProperties.getFileType())

.produceType(screwProperties.getProduceType())

.fileName(screwProperties.getFileName())

.build();

// 指定生成逻辑、当存在指定表、指定表前缀、指定表后缀时,将生成指定表,其余表不生成、并跳过忽略表配置

ProcessConfig processConfig = ProcessConfig.builder()

.designatedTableName(screwProperties.getDesignatedTableName())

.designatedTablePrefix(screwProperties.getDesignatedTablePrefix())

.designatedTableSuffix(screwProperties.getDesignatedTableSuffix())

.ignoreTableName(screwProperties.getIgnoreTableName())

.ignoreTablePrefix(screwProperties.getIgnorePrefix())

.ignoreTableSuffix(screwProperties.getIgnoreSuffix())

.build();

// 配置

Configuration config = Configuration.builder()

.version(screwProperties.getCommon().getVersion())

.description(screwProperties.getCommon().getDescription())

//数据源

.dataSource(dataSource)

//生成配置

.engineConfig(engineConfig)

//生成配置

.produceConfig(processConfig)

.build();

//执行生成

new DocumentationExecute(config).execute();

}

}

运行结果(HTML)

252d30d4b8beb0753c2420fe5333edb2.png

这里代码比较简单,我没做过多解释。原作者的文档例子更简洁清晰,可以去看下。本文只是在文档生成后关闭了项目((ConfigurableApplicationContext) ctx).close()。

打包成screw-jar.jar运行

运行目录

2e8d736f08f91f45e8122a3f7b2d080f.png

命令运行:java -jar screw-jar.jar

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值