• screw 一个简洁好用的数据库表结构文档生成工具


    目录

    一:基本概念

    名称由来:

    特点:  

    数据库支持

    文档生成支持类型

    二:使用操作

    方式一:普通方式

            ①:准备一个基本的maven项目

            ②:引入依赖

    方式二:Maven 插件

           在添加以上的依赖之后,再添加以下代码即可

    三:总结


    一:基本概念

         在企业级开发中、我们经常会有编写数据库表结构文档的时间付出,从业以来,待过几家企业,关于数据库表结构文档状态:要么没有、要么有、但都是手写、后期运维开发,需要手动进行维护到文档中,很是繁琐、如果忘记一次维护、就会给以后工作造成很多困扰、无形中制造了很多坑留给自己和后人,就对于这个问题,有一位程序猿大佬以此设置了一个开源的插件工具,来解决这些问题,并造福大家

        

    • 名称由来:

               从小就学过雷锋的螺丝钉精神,摘自雷锋日记:虽然是细小的螺丝钉,是个细微的小齿轮,然而如果缺了它,那整个的机器就无法运转了,慢说是缺了它,即使是一枚小螺丝钉没拧紧,一个小齿轮略有破损,也要使机器的运转发生故障的,这个工具,很有这意味,虽然很小、但是开发中缺了它还不行,于是便起名为screw(螺丝钉)

     

    • 特点:  

    1. 简洁、轻量、设计良好

    2. 多数据库支持

    3. 多种格式文档

    4. 灵活扩展

    5. 支持自定义模板

    • 数据库支持

    1. MySQL
    2. MariaDB
    3. TIDB
    4. Oracle
    5. SqlServer
    6. PostgreSQL


    • 文档生成支持类型

    1. html
    2. word
    3. markdown

    二:使用操作

    • 方式一:普通方式

            ①:准备一个基本的maven项目

            ②:引入依赖

    1. <dependency>
    2. <groupId>cn.smallbun.screwgroupId>
    3. <artifactId>screw-coreartifactId>
    4. <version>1.0.4version>
    5. dependency>

        ps:由于我在这里使用的是mysql数据库,所以还需要mysql的依赖

    1. <dependency>
    2. <groupId>mysqlgroupId>
    3. <artifactId>mysql-connector-javaartifactId>
    4. <version>8.0.20version>
    5. dependency>
    6. dependencies>

       注意:普通方式实现需要引入以下依赖,方便测试代码中的方法功能可以实现

    1. <dependency>
    2. <groupId>com.zaxxergroupId>
    3. <artifactId>HikariCPartifactId>
    4. <version>3.4.5version>
    5. dependency>

                 HikariCP 是一个高性能的 JDBC 连接池组件,号称性能最好的后起之秀,是一个基于BoneCP做了不少的改进和优化的高性能JDBC连接池。

    完整pom.xml文件代码如下:

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <groupId>com.tangyuangroupId>
    6. <artifactId>code-generatorartifactId>
    7. <version>1.0-SNAPSHOTversion>
    8. <packaging>warpackaging>
    9. <name>code-generator Maven Webappname>
    10. <url>http://www.example.comurl>
    11. <properties>
    12. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    13. <maven.compiler.source>1.8maven.compiler.source>
    14. <maven.compiler.target>1.8maven.compiler.target>
    15. <tomcat.version>8.5tomcat.version>
    16. properties>
    17. <dependencies>
    18. <dependency>
    19. <groupId>junitgroupId>
    20. <artifactId>junitartifactId>
    21. <version>4.11version>
    22. <scope>testscope>
    23. dependency>
    24. <dependency>
    25. <groupId>cn.smallbun.screwgroupId>
    26. <artifactId>screw-coreartifactId>
    27. <version>1.0.4version>
    28. dependency>
    29. <dependency>
    30. <groupId>com.zaxxergroupId>
    31. <artifactId>HikariCPartifactId>
    32. <version>3.4.5version>
    33. dependency>
    34. <dependency>
    35. <groupId>mysqlgroupId>
    36. <artifactId>mysql-connector-javaartifactId>
    37. <version>8.0.20version>
    38. dependency>
    39. dependencies>
    40. <build>
    41. <plugins>
    42. <plugin>
    43. <groupId>org.apache.maven.pluginsgroupId>
    44. <artifactId>maven-clean-pluginartifactId>
    45. <version>3.0.0version>
    46. plugin>
    47. <plugin>
    48. <groupId>cn.smallbun.screwgroupId>
    49. <artifactId>screw-maven-pluginartifactId>
    50. <version>1.0.4version>
    51. <dependencies>
    52. <dependency>
    53. <groupId>com.zaxxergroupId>
    54. <artifactId>HikariCPartifactId>
    55. <version>3.4.5version>
    56. dependency>
    57. <dependency>
    58. <groupId>mysqlgroupId>
    59. <artifactId>mysql-connector-javaartifactId>
    60. <version>8.0.20version>
    61. dependency>
    62. <dependency>
    63. <groupId>cn.smallbun.screwgroupId>
    64. <artifactId>screw-coreartifactId>
    65. <version>1.0.4version>
    66. dependency>
    67. dependencies>
    68. plugin>
    69. plugins>
    70. build>
    71. project>

        ③:编写一个测试代码,并设置好主程序入口

    1. package test;
    2. import cn.smallbun.screw.core.Configuration;
    3. import cn.smallbun.screw.core.engine.EngineConfig;
    4. import cn.smallbun.screw.core.engine.EngineFileType;
    5. import cn.smallbun.screw.core.engine.EngineTemplateType;
    6. import cn.smallbun.screw.core.execute.DocumentationExecute;
    7. import cn.smallbun.screw.core.process.ProcessConfig;
    8. import com.zaxxer.hikari.HikariConfig;
    9. import com.zaxxer.hikari.HikariDataSource;
    10. import javax.sql.DataSource;
    11. import java.util.ArrayList;
    12. /**
    13. * @author 唐渊
    14. * @create  2022-08-01 11:38
    15. */
    16. public class test {
    17. public static void main(String[] args) {
    18. //数据源
    19. HikariConfig hikariConfig = new HikariConfig();
    20. hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
    21. hikariConfig.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/medical?serverTimezone=GMT%2B8");
    22. hikariConfig.setUsername("root");
    23. hikariConfig.setPassword("1234");
    24. //设置可以获取tables remarks信息
    25. hikariConfig.addDataSourceProperty("useInformationSchema", "true");
    26. hikariConfig.setMinimumIdle(2);
    27. hikariConfig.setMaximumPoolSize(5);
    28. DataSource dataSource = new HikariDataSource(hikariConfig);
    29. //生成配置
    30. EngineConfig engineConfig = EngineConfig.builder()
    31. //生成文件路径
    32. .fileOutputDir("C:\\Users\\Administrator\\Desktop\\数据表文档\\")//数据导出的地址
    33. //打开目录
    34. .openOutputDir(true)
    35. //文件类型
    36. // .fileType(EngineFileType.HTML)
    37. //.fileType(EngineFileType.WORD)
    38. .fileType(EngineFileType.MD)
    39. //生成模板实现
    40. .produceType(EngineTemplateType.freemarker).build();
    41. //自定义文件名称
    42. //.fileName("自定义文件名称").build();
    43. //忽略表
    44. ArrayList ignoreTableName = new ArrayList<>();
    45. ignoreTableName.add("test_user");
    46. ignoreTableName.add("test_group");
    47. //忽略表前缀
    48. ArrayList ignorePrefix = new ArrayList<>();
    49. ignorePrefix.add("test_");
    50. //忽略表后缀
    51. ArrayList ignoreSuffix = new ArrayList<>();
    52. ignoreSuffix.add("_test");
    53. ProcessConfig processConfig = ProcessConfig.builder()
    54. //指定生成逻辑、当存在指定表、指定表前缀、指定表后缀时,将生成指定表,其余表不生成、并跳过忽略表配置
    55. //根据名称指定表生成
    56. .designatedTableName(new ArrayList<>())
    57. //根据表前缀生成
    58. .designatedTablePrefix(new ArrayList<>())
    59. //根据表后缀生成
    60. .designatedTableSuffix(new ArrayList<>())
    61. //忽略表名
    62. .ignoreTableName(ignoreTableName)
    63. //忽略表前缀
    64. .ignoreTablePrefix(ignorePrefix)
    65. //忽略表后缀
    66. .ignoreTableSuffix(ignoreSuffix).build();
    67. //配置
    68. Configuration config = Configuration.builder()
    69. //版本
    70. .version("1.0.0")
    71. //描述
    72. .description("数据库设计文档生成")
    73. //数据源
    74. .dataSource(dataSource)
    75. //生成配置
    76. .engineConfig(engineConfig)
    77. //生成配置
    78. .produceConfig(processConfig)
    79. .build();
    80. //执行生成
    81. new DocumentationExecute(config).execute();
    82. }
    83. /**ps:
    84. * java.sql.SQLException: The server time zone value
    85. * 是由于我们默认的时区比东八区少了八个小时。
    86. * 在使用的项目中,设计到数据库的信息时,在url中 加入:?serverTimezone=GMT%2B8
    87. */
    88. /**
    89. * screw - Exception during pool initialization.
    90. */
    91. }

       

    测试结果:

    • html类型

    • word类型

     

    • markdwon类型

    • 方式二:Maven 插件

           在添加以上的依赖之后,再添加以下代码即可

    1. <build>
    2. <plugins>
    3. <plugin>
    4. <groupId>cn.smallbun.screwgroupId>
    5. <artifactId>screw-maven-pluginartifactId>
    6. <version>1.0.4version>
    7. <dependencies>
    8. <dependency>
    9. <groupId>com.zaxxergroupId>
    10. <artifactId>HikariCPartifactId>
    11. <version>3.4.5version>
    12. dependency>
    13. <dependency>
    14. <groupId>mysqlgroupId>
    15. <artifactId>mysql-connector-javaartifactId>
    16. <version>8.0.20version>
    17. dependency>
    18. dependencies>
    19. <configuration>
    20. <username>rootusername>
    21. <password>1234password>
    22. <driverClassName>com.mysql.cj.jdbc.DriverdriverClassName>
    23. <jdbcUrl>jdbc:mysql://127.0.0.1:3306/db_shopping?serverTimezone=GMT%2B8jdbcUrl>
    24. <fileType>WORDfileType>
    25. <openOutputDir>falseopenOutputDir>
    26. <produceType>freemarkerproduceType>
    27. <fileName>fileName>
    28. <description>数据库文档生成description>
    29. <version>${project.version}version>
    30. <title>数据库文档title>
    31. configuration>
    32. <executions>
    33. <execution>
    34. <phase>compilephase>
    35. <goals>
    36. <goal>rungoal>
    37. goals>
    38. execution>
    39. executions>
    40. plugin>
    41. plugins>
    42. build>

              与普通方式不同,maven插件的运行方式是不相同的,运行方式如下:

     

     

               可以点击,在当前面板查看,也可以找到项目的存放文件,进行查看

    三:总结

            screw是一个不错的插件,可以很轻松的解决数据库表结构文档文件,各位大佬如果有兴趣的话,可以阅读阅读探索探索,欢迎各位大佬前来斧正

        

     

  • 相关阅读:
    Android和Linux的开发差异
    HTML换行标签是什么 - 编程
    LINUX网络FTP服务
    【WINDOWS / DOS 批处理】关于标签的执行流程
    云原生平台,让边缘应用玩出花!
    当年的java考试:Java景区预约登记管理系统(maven整合servlet)
    数据结构-静态链表创建
    国家加快培育数据要素市场的重要意义是什么
    Kotlin学习笔记(1)-- 基础语法
    ros2与turtlebot3仿真教程-turtlebot3导航
  • 原文地址:https://blog.csdn.net/qq_61313896/article/details/126394205