目录
在企业级开发中、我们经常会有编写数据库表结构文档的时间付出,从业以来,待过几家企业,关于数据库表结构文档状态:要么没有、要么有、但都是手写、后期运维开发,需要手动进行维护到文档中,很是繁琐、如果忘记一次维护、就会给以后工作造成很多困扰、无形中制造了很多坑留给自己和后人,就对于这个问题,有一位程序猿大佬以此设置了一个开源的插件工具,来解决这些问题,并造福大家
从小就学过雷锋的螺丝钉精神,摘自雷锋日记:虽然是细小的螺丝钉,是个细微的小齿轮,然而如果缺了它,那整个的机器就无法运转了,慢说是缺了它,即使是一枚小螺丝钉没拧紧,一个小齿轮略有破损,也要使机器的运转发生故障的,这个工具,很有这意味,虽然很小、但是开发中缺了它还不行,于是便起名为screw(螺丝钉)
简洁、轻量、设计良好
多数据库支持
多种格式文档
灵活扩展
支持自定义模板
-
- <dependency>
- <groupId>cn.smallbun.screwgroupId>
- <artifactId>screw-coreartifactId>
- <version>1.0.4version>
- dependency>
ps:由于我在这里使用的是mysql数据库,所以还需要mysql的依赖
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>8.0.20version>
- dependency>
- dependencies>
注意:普通方式实现需要引入以下依赖,方便测试代码中的方法功能可以实现
- <dependency>
- <groupId>com.zaxxergroupId>
- <artifactId>HikariCPartifactId>
- <version>3.4.5version>
- dependency>
HikariCP 是一个高性能的 JDBC 连接池组件,号称性能最好的后起之秀,是一个基于BoneCP做了不少的改进和优化的高性能JDBC连接池。
完整pom.xml文件代码如下:
- "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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
-
- <groupId>com.tangyuangroupId>
- <artifactId>code-generatorartifactId>
- <version>1.0-SNAPSHOTversion>
- <packaging>warpackaging>
-
- <name>code-generator Maven Webappname>
-
- <url>http://www.example.comurl>
-
- <properties>
- <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
- <maven.compiler.source>1.8maven.compiler.source>
- <maven.compiler.target>1.8maven.compiler.target>
- <tomcat.version>8.5tomcat.version>
- properties>
-
- <dependencies>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.11version>
- <scope>testscope>
- dependency>
-
- <dependency>
- <groupId>cn.smallbun.screwgroupId>
- <artifactId>screw-coreartifactId>
- <version>1.0.4version>
- dependency>
-
-
- <dependency>
- <groupId>com.zaxxergroupId>
- <artifactId>HikariCPartifactId>
- <version>3.4.5version>
- dependency>
-
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>8.0.20version>
- dependency>
- dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-clean-pluginartifactId>
- <version>3.0.0version>
- plugin>
- <plugin>
- <groupId>cn.smallbun.screwgroupId>
- <artifactId>screw-maven-pluginartifactId>
- <version>1.0.4version>
- <dependencies>
-
- <dependency>
- <groupId>com.zaxxergroupId>
- <artifactId>HikariCPartifactId>
- <version>3.4.5version>
- dependency>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>8.0.20version>
- dependency>
- <dependency>
- <groupId>cn.smallbun.screwgroupId>
- <artifactId>screw-coreartifactId>
- <version>1.0.4version>
- dependency>
- dependencies>
- plugin>
- plugins>
- build>
- project>
③:编写一个测试代码,并设置好主程序入口
- package test;
-
- 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 唐渊
- * @create 2022-08-01 11:38
- */
- public class test {
-
- public static void main(String[] args) {
- //数据源
- HikariConfig hikariConfig = new HikariConfig();
- hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
- hikariConfig.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/medical?serverTimezone=GMT%2B8");
- hikariConfig.setUsername("root");
- hikariConfig.setPassword("1234");
- //设置可以获取tables remarks信息
- hikariConfig.addDataSourceProperty("useInformationSchema", "true");
- hikariConfig.setMinimumIdle(2);
- hikariConfig.setMaximumPoolSize(5);
- DataSource dataSource = new HikariDataSource(hikariConfig);
- //生成配置
- EngineConfig engineConfig = EngineConfig.builder()
- //生成文件路径
- .fileOutputDir("C:\\Users\\Administrator\\Desktop\\数据表文档\\")//数据导出的地址
- //打开目录
- .openOutputDir(true)
- //文件类型
- // .fileType(EngineFileType.HTML)
- //.fileType(EngineFileType.WORD)
- .fileType(EngineFileType.MD)
- //生成模板实现
- .produceType(EngineTemplateType.freemarker).build();
- //自定义文件名称
- //.fileName("自定义文件名称").build();
-
- //忽略表
- ArrayList
ignoreTableName = new ArrayList<>(); - ignoreTableName.add("test_user");
- ignoreTableName.add("test_group");
- //忽略表前缀
- ArrayList
ignorePrefix = new ArrayList<>(); - ignorePrefix.add("test_");
- //忽略表后缀
- ArrayList
ignoreSuffix = new ArrayList<>(); - ignoreSuffix.add("_test");
- ProcessConfig processConfig = ProcessConfig.builder()
- //指定生成逻辑、当存在指定表、指定表前缀、指定表后缀时,将生成指定表,其余表不生成、并跳过忽略表配置
- //根据名称指定表生成
- .designatedTableName(new ArrayList<>())
- //根据表前缀生成
- .designatedTablePrefix(new ArrayList<>())
- //根据表后缀生成
- .designatedTableSuffix(new ArrayList<>())
- //忽略表名
- .ignoreTableName(ignoreTableName)
- //忽略表前缀
- .ignoreTablePrefix(ignorePrefix)
- //忽略表后缀
- .ignoreTableSuffix(ignoreSuffix).build();
- //配置
- Configuration config = Configuration.builder()
- //版本
- .version("1.0.0")
- //描述
- .description("数据库设计文档生成")
- //数据源
- .dataSource(dataSource)
- //生成配置
- .engineConfig(engineConfig)
- //生成配置
- .produceConfig(processConfig)
- .build();
- //执行生成
- new DocumentationExecute(config).execute();
- }
-
-
- /**ps:
- * java.sql.SQLException: The server time zone value
- * 是由于我们默认的时区比东八区少了八个小时。
- * 在使用的项目中,设计到数据库的信息时,在url中 加入:?serverTimezone=GMT%2B8
- */
-
- /**
- * screw - Exception during pool initialization.
- */
- }
测试结果:
html类型

word类型

markdwon类型

-
-
-
- <build>
- <plugins>
- <plugin>
- <groupId>cn.smallbun.screwgroupId>
- <artifactId>screw-maven-pluginartifactId>
- <version>1.0.4version>
- <dependencies>
-
- <dependency>
- <groupId>com.zaxxergroupId>
- <artifactId>HikariCPartifactId>
- <version>3.4.5version>
- dependency>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>8.0.20version>
- dependency>
- dependencies>
- <configuration>
-
- <username>rootusername>
-
- <password>1234password>
-
- <driverClassName>com.mysql.cj.jdbc.DriverdriverClassName>
-
- <jdbcUrl>jdbc:mysql://127.0.0.1:3306/db_shopping?serverTimezone=GMT%2B8jdbcUrl>
-
- <fileType>WORDfileType>
-
- <openOutputDir>falseopenOutputDir>
-
- <produceType>freemarkerproduceType>
-
- <fileName>fileName>
-
- <description>数据库文档生成description>
-
- <version>${project.version}version>
-
- <title>数据库文档title>
- configuration>
- <executions>
- <execution>
- <phase>compilephase>
- <goals>
- <goal>rungoal>
- goals>
- execution>
- executions>
- plugin>
- plugins>
- build>
与普通方式不同,maven插件的运行方式是不相同的,运行方式如下:


可以点击,在当前面板查看,也可以找到项目的存放文件,进行查看
screw是一个不错的插件,可以很轻松的解决数据库表结构文档文件,各位大佬如果有兴趣的话,可以阅读阅读探索探索,欢迎各位大佬前来斧正
