Day80-一键生成数据库文档-screw (螺丝钉)

介绍了一款名为screw的数据库表结构文档生成工具,通过简单的配置即可生成清晰的数据库设计文档,支持HTML等格式输出,并提供了Java依赖及Maven插件两种使用方式。

Day80-一键生成数据库文档!

🚀 screw (螺丝钉) 英:[skruː] ~ 简洁好用的数据库表结构文档生成工具

**Gitee地址:**https://gitee.com/XinShiDai/screw

使用方式

1、普通方式

  • 引入依赖
<dependency>
    <groupId>cn.smallbun.screw</groupId>
    <artifactId>screw-core</artifactId>
    <version>${lastVersion}</version>
 </dependency>
  • 编写代码
/**
 * 文档生成
 */
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、Maven插件

<build>
    <plugins>
        <plugin>
            <groupId>cn.smallbun.screw</groupId>
            <artifactId>screw-maven-plugin</artifactId>
            <version>${lastVersion}</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>password</password>
                <!--driver-->
                <driverClassName>com.mysql.cj.jdbc.Driver</driverClassName>
                <!--jdbc url-->
                <jdbcUrl>jdbc:mysql://127.0.0.1:3306/xxxx</jdbcUrl>
                <!--生成文件类型-->
                <fileType>HTML</fileType>
                <!--打开文件输出目录-->
                <openOutputDir>false</openOutputDir>
                <!--生成模板-->
                <produceType>freemarker</produceType>
                <!--文档名称 为空时:将采用[数据库名称-描述-版本号]作为文档名称-->
                <fileName>测试文档名称</fileName>
                <!--描述-->
                <description>数据库文档生成</description>
                <!--版本-->
                <version>${project.version}</version>
                <!--标题-->
                <title>数据库文档</title>
            </configuration>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
### 使用 Screw 工具从数据库设计生成 Excel 文档 #### 特性和功能概述 `cn.smallbun.screw` 是一个强大的开源工具,专门用于将复杂的数据库结构转换成易于理解的文档形式[^1]。尽管该工具主要用于生成 HTML 或者其他格式的文档来描述数据库及其字段信息,但通过一些额外的操作也可以间接支持导出到 Excel。 #### 准备工作 为了能够利用 `cn.smallbun.screw` 实现这一目标,首先需要确保已经安装并配置好了 Java 开发环境,并且下载了最新版本的 `screw.jar` 文件。此外还需要准备好要分析的目标数据库连接参数(URL, 用户名, 密码等)。 #### 配置与执行命令 创建一个新的 Java 类作为入口程序,在其中引入必要的依赖库并将它们打包在一起运行。以下是具体的代码片段: ```java import cn.smallbun.screw.core.Configuration; import cn.smallbun.screw.core.engine.EngineConfig; import cn.smallbun.screw.core.execute.DocumentationExecute; import cn.smallbun.screw.db.DatabaseWrapper; public class GenerateExcel { public static void main(String[] args){ // 设置数据库链接信息 DatabaseWrapper databaseWrapper = new DatabaseWrapper( "jdbc:mysql://localhost:3306/your_database", "root", "password" ); EngineConfig engineConfig = EngineConfig.builder() .fileOutputDir("output_directory") // 输出目录路径 .fileName("database_structure.xlsx")// 文件名称 .produceType("excel") // 生产类型设置为 excel .build(); Configuration config = Configuration.builder().version("v1.0").description("Database Structure Document").engine(engineConfig).build(); DocumentationExecute.execute(config,databaseWrapper); } } ``` 这段代码定义了一个名为 `GenerateExcel` 的类,它包含了启动整个过程所需的一切要素。注意这里特别指定了 `.produceType("excel")` 来指示希望得到的是 Excel 格式的输出文件而不是默认的HTML页面。 #### 结果处理 一旦上述代码被执行完毕之后,将会在指定的位置找到新生成的 Excel 文件 (`database_structure.xlsx`) ,里面记录着所选数据库内各个格的信息概览,包括但不限于列名、数据类型以及其他属性等内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

阿杰杰杰のblog

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

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

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

打赏作者

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

抵扣说明:

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

余额充值