当前位置:网站首页>The use of the database table structure document generation tool screw
The use of the database table structure document generation tool screw
2022-08-03 07:54:00 【12 Programmers】
目录
一、screw介绍
日常的开发工作中,经常会和数据库打交道,In some scenarios may needThe structure of the database table document
,今天分享个简洁好用的Database table structure document generation toolsscrew
.
1.简介
screw是一个简洁好用的The database table structure document generation tools .
screw目前支持MySQL、Oracle、SqlServer、MariaDB、PostgreSQL等数据库,生成文档目前支持html、word、markdown文档格式.
github: GitHub - pingfangushi/screw: 简洁好用的数据库表结构文档生成器
gitee: screw: 简洁好用的数据库表结构文档工具,支持MySQL/MariaDB/SqlServer/Oracle/PostgreSQL/TIDB/CacheDB 数据库.
2.特点
screw的特点:
- 简洁、轻量、设计良好
- 多数据库支持
- 多种格式文档
- 灵活扩展
- 支持自定义模板
3.文档格式
生成文档目前支持html、word、markdown文档格式.
- html
- word
- markdown
二、生成方式
下面以MySQL数据库为例来简单介绍下.
新建测试库test及表t_user、t_product.
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户id',
`name` varchar(255) DEFAULT NULL COMMENT '名称',
`age` int(10) DEFAULT NULL COMMENT '年龄',
`remarks` varchar(255) DEFAULT NULL COMMENT '备注',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`create_user` varchar(255) DEFAULT NULL COMMENT '创建人',
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
`update_user` varchar(255) DEFAULT NULL COMMENT '更新人',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户信息表';
CREATE TABLE `t_product` (
`product_id` int(11) NOT NULL,
`title` varchar(255) DEFAULT NULL COMMENT '标题',
`sub_title` varchar(255) DEFAULT NULL COMMENT '副标题',
`sale_price` decimal(10,2) DEFAULT NULL COMMENT '商品售价',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`create_by` int(11) DEFAULT NULL COMMENT '创建者',
`update_time` datetime DEFAULT NULL COMMENT '修改时间',
`update_by` int(11) DEFAULT NULL COMMENT '修改者',
PRIMARY KEY (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='商品表';
2.1 方式1:使用 Java 代码
2.1.1 引入依赖
引入screw的maven依赖:
<!--The introduction of the database table structure document generatorScrew依赖-->
<dependency>
<groupId>cn.smallbun.screw</groupId>
<artifactId>screw-core</artifactId>
<version>1.0.5</version>
</dependency>
除此之外,还需引入数据库相关的依赖:
<!-- Hikari数据库连接池 Also can use alibabaDruid、C3p0等数据库连接池 -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
<!--mysql驱动-->
<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>
2.1.2 编写代码
package com.example.screwdemo.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.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;
import java.util.ArrayList;
/** * @author qzz */
public class DocumentGenerator {
//以下static final修饰的参数 如果是基于SpringBoot项目,可以在配置文件中配置,通过@Value注解获取
/** * MySQL驱动 6.0以前的使用com.mysql.jdbc.Driver,6.0以后的使用com.mysql.cj.jdbc.Driver 我这边使用的是mysql-5.7.25版本的 */
private static final String Driver_Class_Name = "com.mysql.jdbc.Driver";
/** * 数据库URL characterEncoding=UTF-8: Prevent generated document after the code */
private static final String DB_URL = "jdbc:mysql://localhost:3306/test?&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSl=false";
/** * MySQL数据库账号密码 */
private static final String DB_USERNAME = "root";
private static final String DB_PASSWORD = "root";
/** * Generate database document file path According to the machine computer configure */
private static final String FILE_OUTPUT_DIR = "F:\\dbDoc\\";
/** * screw配置的文件名称,The database document name */
private static final String DOC_FILE_NAME = "数据库设计文档生成";
/** * screw配置的文件类型 HTML->HTML文件 WORD->WORD文件 MD->Markdown文件 */
private static final EngineFileType FILE_OUTPUT_TYPE = EngineFileType.WORD;
/** * 版本 */
private static final String DOC_VERSION = "1.0.0";
/** * 描述 */
private static final String DOC_DESCRIPTION = "数据库设计文档生成";
public static void main(String[] args) {
generatorDocument();
}
public static void generatorDocument() {
//数据源 创建HikariConfig配置类
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName(Driver_Class_Name);
hikariConfig.setJdbcUrl(DB_URL);
hikariConfig.setUsername(DB_USERNAME);
hikariConfig.setPassword(DB_PASSWORD);
//#设置useInformationSchema 可以获取tables表注释信息 The explanation of database tables and columns field has、生成文档没有说明
hikariConfig.addDataSourceProperty("useInformationSchema", "true");
hikariConfig.setMinimumIdle(2);
hikariConfig.setMaximumPoolSize(5);
DataSource dataSource = new HikariDataSource(hikariConfig);
//创建screw的引擎配置
EngineConfig engineConfig = EngineConfig.builder()
//生成文件路径
.fileOutputDir(FILE_OUTPUT_DIR)
//打开目录
.openOutputDir(true)
//文件类型 HTML->HTML文件 WORD->WORD文件 MD->Markdown文件
.fileType(FILE_OUTPUT_TYPE)
//生成模板实现
.produceType(EngineTemplateType.freemarker)
//自定义文件名称,The database document name
.fileName(DOC_FILE_NAME).build();
//创建screw的处理配置,可忽略
//忽略表
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");
// Need to generate a document database table 如果designatedTablePrefixSetting value is not empty,则 designatedTableName In the name of the table to remove prefixes,Or it will repeat generation,And the name of the table and field is wrong
ArrayList<String> designatedTableName = new ArrayList<>();
// designatedTableName.add("user");
// designatedTableName.add("product");
// Need to generate a document database table prefix
ArrayList<String> designatedTablePrefix = new ArrayList<>();
designatedTablePrefix.add("t_");
ProcessConfig processConfig = ProcessConfig.builder()
//指定生成逻辑、当存在指定表、指定表前缀、指定表后缀时,将生成指定表,其余表不生成、并跳过忽略表配置
//根据名称指定表生成
// .designatedTableName(new ArrayList<>())
.designatedTableName(designatedTableName)
//根据表前缀生成
// .designatedTablePrefix(new ArrayList<>())
.designatedTablePrefix(designatedTablePrefix)
//根据表后缀生成
.designatedTableSuffix(new ArrayList<>())
//忽略表名
.ignoreTableName(ignoreTableName)
//忽略表前缀
.ignoreTablePrefix(ignorePrefix)
//忽略表后缀
.ignoreTableSuffix(ignoreSuffix).build();
//创建screw的配置
Configuration config = Configuration.builder()
//版本
.version(DOC_VERSION)
//描述
.description(DOC_DESCRIPTION)
//数据源
.dataSource(dataSource)
//生成配置
.engineConfig(engineConfig)
//生成配置
.produceConfig(processConfig)
.build();
//执行screw,生成数据库文档
new DocumentationExecute(config).execute();
}
}
注意点:
- If you want to generate the database所有表The database document,
可忽略screw的处理配置
- 如果要生成指定表The database document,
需要配置screw的处理配置,
Can specify the name of the table、指定表前缀 - 指定表名、Specify the table prefix to use:
- 如果指定表名
designatedTableName
中的表名 Table prefix is included in the,You do not need to configure specify the table prefix; - 如果指定表名
designatedTableName
中的表名 Do not include the table prefix,Configuration is needed to specify table prefix;
- 如果指定表名
示例:
Such as exist in the database tablet_user 和 t_product等多张表,Requiring only exportt_user 和 t_productThe database document,screwProcessing configuration of the specified table and specify the table prefix approach has the following two kinds of:
方式一:指定表
// Need to generate a document database table
ArrayList<String> designatedTableName = new ArrayList<>();
designatedTableName.add("t_user");
designatedTableName.add("t_product");
方式二:Specify the table with a specified table prefix
// Need to generate a document database table
ArrayList<String> designatedTableName = new ArrayList<>();
designatedTableName.add("user");
designatedTableName.add("product");
// Need to generate a document database table prefix
ArrayList<String> designatedTablePrefix = new ArrayList<>();
designatedTablePrefix.add("t_");
2.1.3 测试
2.2 方式2:使用 Spring Boot 整合screw
2.2.1 引入依赖
引入screw的maven依赖:
<!--The introduction of the database table structure document generatorScrew依赖-->
<dependency>
<groupId>cn.smallbun.screw</groupId>
<artifactId>screw-core</artifactId>
<version>1.0.5</version>
</dependency>
除此之外,还需引入数据库相关的依赖:
<!-- Hikari数据库连接池 Also can use alibabaDruid、C3p0等数据库连接池 -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
<!--mysql驱动-->
<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>
2.2.2 配置数据源信息
application.yml:
# 服务端口
server:
port: 8083
# 数据源配置
spring:
datasource:
name: test
url: jdbc:mysql://localhost:3306/test?&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSl=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
xa:
properties:
#设置useInformationSchema 可以获取tables表注释信息 The explanation of database tables and columns field has、生成文档没有说明
useInformationSchema: true
# Generate database document file path
FILE_OUTPUT_DIR: F:\dbDoc\
# 版本
DOC_VERSION: 1.0.0
# screw配置的文件名称,The database document name
DOC_FILE_NAME: xx数据库设计文档
# 描述
DOC_DESCRIPTION: 数据库设计文档生成
2.2.3 编写代码
generatorService.java:
package com.example.screwdemo.service;
/** * 生成 数据库表结构文档 * @author qzz */
public interface GeneratorService {
/** * 生成 数据库表结构文档 */
void generatorDocument();
}
generatorServiceImpl.java:
package com.example.screwdemo.service.impl;
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.example.screwdemo.service.GeneratorService;
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 org.springframework.stereotype.Service;
import javax.sql.DataSource;
import java.util.ArrayList;
/** * 生成 数据库表结构文档 * @author qzz */
@Service
public class GeneRatorServiceImpl implements GeneratorService {
@Autowired
private ApplicationContext applicationContext;//Injection application context
/** * MySQL驱动 */
@Value("${spring.datasource.driver-class-name}")
private String Driver_Class_Name;
/** * 数据库URL characterEncoding=UTF-8: Prevent generated document after the code */
@Value("${spring.datasource.url}")
private String DB_URL;
/** * MySQL数据库账号密码 */
@Value("${spring.datasource.username}")
private String DB_USERNAME = "root";
@Value("${spring.datasource.password}")
private String DB_PASSWORD = "root";
/** * Generate database document file path According to the machine computer configure */
@Value("${FILE_OUTPUT_DIR}")
private String FILE_OUTPUT_DIR;
/** * 版本 */
@Value("${DOC_VERSION}")
private String DOC_VERSION;
/** * screw配置的文件类型 HTML->HTML文件 WORD->WORD文件 MD->Markdown文件 */
private static final EngineFileType FILE_OUTPUT_TYPE = EngineFileType.WORD;
/** * screw配置的文件名称,The database document name */
@Value("${DOC_FILE_NAME}")
private String DOC_FILE_NAME;
/** * 描述 */
@Value("${DOC_DESCRIPTION}")
private String DOC_DESCRIPTION;
/** * 生成 数据库表结构文档 */
@Override
public void generatorDocument() {
//获取数据源 This approach toNo qualifying bean of type 'javax.sql.DataSource' available
// DataSource dataSource = applicationContext.getBean(DataSource.class);
//获取数据源 创建HikariConfig配置类
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName(Driver_Class_Name);
hikariConfig.setJdbcUrl(DB_URL);
hikariConfig.setUsername(DB_USERNAME);
hikariConfig.setPassword(DB_PASSWORD);
//#设置useInformationSchema 可以获取tables表注释信息 The explanation of database tables and columns field has、生成文档没有说明
hikariConfig.addDataSourceProperty("useInformationSchema", "true");
hikariConfig.setMinimumIdle(2);
hikariConfig.setMaximumPoolSize(5);
DataSource dataSource = new HikariDataSource(hikariConfig);
//创建screw的引擎配置
EngineConfig engineConfig = EngineConfig.builder()
//生成文件路径
.fileOutputDir(FILE_OUTPUT_DIR)
//打开目录
.openOutputDir(true)
//文件类型 HTML->HTML文件 WORD->WORD文件 MD->Markdown文件
.fileType(FILE_OUTPUT_TYPE)
//生成模板实现
.produceType(EngineTemplateType.freemarker)
//自定义文件名称,The database document name
.fileName(DOC_FILE_NAME).build();
//创建screw的配置:生成文档配置(包含以下自定义版本号、描述等配置连接)
Configuration config = Configuration.builder()
//版本
.version(DOC_VERSION)
//描述
.description(DOC_DESCRIPTION)
//数据源
.dataSource(dataSource)
//生成配置
.engineConfig(engineConfig)
//生成配置
.produceConfig(getProcessConfig())
.build();
//执行screw,生成数据库文档
new DocumentationExecute(config).execute();
}
/** * 配置想要生成的表、想要忽略的表 * @return */
private ProcessConfig getProcessConfig() {
//创建screw的处理配置,可忽略
//忽略表
ArrayList<String> ignoreTableName = new ArrayList<>();
ignoreTableName.add("test_user");
ignoreTableName.add("test_group");
//忽略表前缀,如忽略test_开头的数据库表
ArrayList<String> ignorePrefix = new ArrayList<>();
ignorePrefix.add("test_");
//忽略表后缀
ArrayList<String> ignoreSuffix = new ArrayList<>();
ignoreSuffix.add("_test");
// Need to generate a document database table 如果designatedTablePrefixSetting value is not empty,则 designatedTableName In the name of the table to remove prefixes,Or it will repeat generation,And the name of the table and field is wrong
ArrayList<String> designatedTableName = new ArrayList<>();
// designatedTableName.add("user");
// designatedTableName.add("product");
// Need to generate a document database table prefix
ArrayList<String> designatedTablePrefix = new ArrayList<>();
designatedTablePrefix.add("t_");
//指定生成逻辑、当存在指定表、指定表前缀、指定表后缀时,将生成指定表,其余表不生成、并跳过忽略表配置
ProcessConfig processConfig = ProcessConfig.builder()
//根据名称指定表生成
// .designatedTableName(new ArrayList<>())
.designatedTableName(designatedTableName)
//根据表前缀生成
// .designatedTablePrefix(new ArrayList<>())
.designatedTablePrefix(designatedTablePrefix)
//根据表后缀生成
.designatedTableSuffix(new ArrayList<>())
//忽略表名
.ignoreTableName(ignoreTableName)
//忽略表前缀
.ignoreTablePrefix(ignorePrefix)
//忽略表后缀
.ignoreTableSuffix(ignoreSuffix).build();
return processConfig;
}
}
编写controller测试方法:
package com.example.screwdemo.controller;
import com.example.screwdemo.service.GeneratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/** * Database table structure document generation tools * @author qzz */
@RestController
@RequestMapping("/generator")
public class GeneratorController {
@Autowired
private GeneratorService generatorService;
/** * 生成数据库表结构文档 */
@RequestMapping("/generatorDocument")
public void generatorDocument(){
generatorService.generatorDocument();
}
}
2.2.4 启动测试
postman Access to the browser input 链接 http://localhost:8083/generator/generatorDocument,执行效果:
三、完整代码
可点击此处下载
边栏推荐
- 第四章:架构,Architecture
- 推荐系统-排序层-精排模型:LR、GBDT、Wide&Deep、DCN、DIN、DIEN、MMOE、PLE
- 差分(前缀和的逆运算)
- 解决移动端有纵向滚动条但是不能滚动的问题
- 开放域OOD主要数据集、评价指标汇总
- Data warehouse buried point system and attribution practice
- 【多线程进阶】--- 常见锁策略,CAS,synchronized底层工作原理,JUC,线程安全的集合类,死锁
- 前缀和(区间和,子矩阵的和)
- DSP Trick:向量长度估算
- mysqlbinlog: unknown variable 'default-character-set=utf8'
猜你喜欢
随机推荐
帆软11版本参数联动为null查询全部
学习Glide 常用场景的写法 +
jolt语法
数据库表结构文档 生成工具screw的使用
测试用例设计方法之因果图详解
Roson的Qt之旅#103 QML之标签导航控件TabBar
【云原生--Kubernetes】Pod容器与镜像拉取策略
前缀和(区间和,子矩阵的和)
解决登录vCenter提示“当前网站安全证书不受信任“
七夕和程序员有毛关系?
pyspark df secondary sorting
idea远程debug
加速FinOps实践,为企业降本增效
Postman will return to the interface to generate a json file to the local
千万级别的表分页查询非常慢,怎么办?
CDGA|如何加强数字政府建设?
excel高级绘图技巧100讲(二十一)- Excel层叠柱形图
如何使用电子邮件营销在五个步骤中增加产品评论
华为设备配置BFD与接口联动(触发与BFD联动的接口物理状态变为Down)
HCIP笔记整理 2022/7/20