项目中使用screw生成数据库文档

1,860 阅读5分钟

这是我参与8月更文挑战的第23天,活动详情查看:8月更文挑战

随着代码量一天天上升,对业务的需求也逐渐了解,但是放眼整个项目,各种模块各种功能,许多服务许多表。因此要想短时间内更快的熟悉整个项目的数据结构,我们就要对数据库表在宏观上有一定的认识。而screw工具很好的满足了我们的需求,它可以将整个数据库的表输出为数据库表结构文档,无论是在做分析或是熟悉表结构都能提升很大效率。

screw项目的github地址为:screw

Screw

1. 数据库支持

screw是一个简洁好用的数据库表结构文档生成工具,它支持多种数据库并支持自定义模板输出文档,文档也是有多种文件类型选择。现今已经支持的数据库有:

  1. MySQL
  2. Oracle
  3. SqlServer
  4. PostgreSQL
  5. Cache DB
  6. MariaDB
  7. TIDB 目前该项目仍在持续开发中,也会有更多的数据库接入进来。

2. screw使用流程

2.1 Java代码方式

2.1.1 pom文件中引入screw相关依赖

创建一个SpringBoot项目,并引入web和mysql启动器,在项目的pom文件中引入screw的核心依赖和HikariCP数据库连接池依赖,screw在创建数据库文档时使用了HikariCP连接池。

<!-- screw核心 -->
<dependency>
    <groupId>cn.smallbun.screw</groupId>
    <artifactId>screw-core</artifactId>
    <version>1.0.5</version>
</dependency>
<!-- HikariCP -->
<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>3.4.5</version>
</dependency>

2.1.2 设置数据源信息

在SpringBoot的application.properties配置文件中配置需要创建数据库文档的数据源信息,如果想要生成表的备注信息,则使用useInformationSchema=true配置。

spring.datasource.url=jdbc:mysql://localhost:3306/fire?useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.xa.properties.useInformationSchema=true 

2.1.3 使用代码生成数据库文档

我们需要创建一个Java类,并在类中添加如下的官方代码方法,其中的一些配置内容我们可以根据需要进行编辑,如自定义配置文档生成路径和生成的文件格式等信息,最后执行方法就会在定义的目录下生成文档。

screw工具生成数据库文档支持html、word和markdowm三种格式。

/**
 * 文档生成方法
 */
void documentGeneration() {
   //获取数据源配置
   HikariConfig hikariConfig = new HikariConfig();
   hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
   hikariConfig.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/database");
   hikariConfig.setUsername("root");
   hikariConfig.setPassword("password");
   //设置可以获取tables remarks信息
   hikariConfig.addDataSourceProperty("useInformationSchema", "true");
   hikariConfig.setMinimumIdle(2);
   hikariConfig.setMaximumPoolSize(5);
   DataSource dataSource = new HikariDataSource(hikariConfig);
   //生成配置
   EngineConfig engineConfig = EngineConfig.builder()
         //生成文件路径
         .fileOutputDir(fileOutputDir)
         //打开目录
         .openOutputDir(true)
         //文件类型
         .fileType(EngineFileType.HTML)
         //生成模板实现
         .produceType(EngineTemplateType.freemarker)
         //自定义文件名称
         .fileName("自定义文件名称").build();

   //忽略表
   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");
   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();
}

2.2. 使用maven插件直接生成数据库文档

除了使用Java代码的方式,我们还可以直接在pom文件中以maven插件的方式加入screw配置,并在依赖加载完成后使用maven模块中的screw->run执行并生成数据库文档。

所有的依赖信息和输出文档路径、文件格式信息都在maven插件中进行定义。

 <build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>cn.smallbun.screw</groupId>
            <artifactId>screw-maven-plugin</artifactId>
            <version>1.0.3</version>
            <dependencies>
                <!-- HikariCP -->
                <dependency>
                    <groupId>com.zaxxer</groupId>
                    <artifactId>HikariCP</artifactId>
                    <version>3.4.5</version>
                </dependency>
                <!--mysql driver-->
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>8.0.20</version>
                </dependency>
            </dependencies>
            <configuration>
                <!--username-->
                <username>root</username>
                <!--password-->
                <password>123456</password>
                <!--driver-->
                <driverClassName>com.mysql.cj.jdbc.Driver</driverClassName>
                <!--jdbc url-->
                <jdbcUrl>jdbc:mysql://localhost:3306/fire</jdbcUrl>
                <!--生成文件类型-->
                <fileType>HTML</fileType>
                <!--打开文件输出目录-->
                <openOutputDir>false</openOutputDir>
                <!--生成模板-->
                <produceType>freemarker</produceType>
                <!--文档名称 为空时:将采用[数据库名称-描述-版本号]作为文档名称-->
                <!--<docName>测试文档名称</docName>-->
                <!--描述-->
                <description>数据库文档生成</description>
                <!--版本-->
                <version>${project.version}</version>
                <!--标题-->
                <title>fire数据库文档</title>
            </configuration>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

3. Maven无法下载依赖的问题

今天在引入screw依赖时,IDEA中的maven突然抽风,screw的依赖下载不下来,经过一番折腾,最终解决问题,也在此记录一下。

3.1 手动更新依赖信息

如果依赖没有下载成功,可能是由于IDEA的自动导入依赖产生了偏差,我们可以尝试以下方式:

  1. 在IDEA的maven栏中重新导入/下载依赖信息
  2. 在pom文件中移除依赖并重新添加,让maven重新导入
  3. 我们可以去maven本地仓库查找对应坐标是否已经存在依赖,如果存在不完整的依赖则需要删除依赖后重新导入

3.2 maven来源

当IDEA中项目中引入的依赖无法正常下载时,我们首先想到的应该是看一下IDEA中maven的配置是否正确。在IDEA中,打开文件 -> 设置,搜索maven,查看当前使用的maven配置信息。

  1. 如果是IDEA自带的Bundled(maven3),那么我们就要找到对应settings文件,考虑是否官方的maven工具配置不足,没有添加镜像信息等,导致远程依赖下载缓慢或者无法下载。
  2. 如果是自下载maven,要看一下官方的settingsrepository路径是否覆盖为本地的maven,以及本地maven中的settings配置文件中是否使用了国内镜像源。
  • 2019版本的IDEA使用3.6.1版本的maven可能会存在兼容问题,如果本地maven版本和IDEA不兼容,可以考虑下载更高版本或者暂时换为IDEA自带Maven进行使用。

4. 最后

好了,以上就是我在项目中的一些业务需要使用到了screw创建数据库文档的情况,并且在IDEA中引入依赖是产生的小问题的记录,如果你也有同样的需求或问题,希望可以帮助到你哦!