当前位置:   article > 正文

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. <!-- screw核心 -->
  2. <dependency>
  3. <groupId>cn.smallbun.screw</groupId>
  4. <artifactId>screw-core</artifactId>
  5. <version>1.0.4</version>
  6. </dependency>

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

  1. <!--mysql driver-->
  2. <dependency>
  3. <groupId>mysql</groupId>
  4. <artifactId>mysql-connector-java</artifactId>
  5. <version>8.0.20</version>
  6. </dependency>
  7. </dependencies>

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

  1. <dependency>
  2. <groupId>com.zaxxer</groupId>
  3. <artifactId>HikariCP</artifactId>
  4. <version>3.4.5</version>
  5. </dependency>

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

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

  1. <?xml version="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.0</modelVersion>
  5. <groupId>com.tangyuan</groupId>
  6. <artifactId>code-generator</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <packaging>war</packaging>
  9. <name>code-generator Maven Webapp</name>
  10. <!-- FIXME change it to the project's website -->
  11. <url>http://www.example.com</url>
  12. <properties>
  13. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14. <maven.compiler.source>1.8</maven.compiler.source>
  15. <maven.compiler.target>1.8</maven.compiler.target>
  16. <tomcat.version>8.5</tomcat.version>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>junit</groupId>
  21. <artifactId>junit</artifactId>
  22. <version>4.11</version>
  23. <scope>test</scope>
  24. </dependency>
  25. <!-- screw核心 -->
  26. <dependency>
  27. <groupId>cn.smallbun.screw</groupId>
  28. <artifactId>screw-core</artifactId>
  29. <version>1.0.4</version>
  30. </dependency>
  31. <!-- HikariCP -->
  32. <dependency>
  33. <groupId>com.zaxxer</groupId>
  34. <artifactId>HikariCP</artifactId>
  35. <version>3.4.5</version>
  36. </dependency>
  37. <!--mysql driver-->
  38. <dependency>
  39. <groupId>mysql</groupId>
  40. <artifactId>mysql-connector-java</artifactId>
  41. <version>8.0.20</version>
  42. </dependency>
  43. </dependencies>
  44. <build>
  45. <plugins>
  46. <plugin>
  47. <groupId>org.apache.maven.plugins</groupId>
  48. <artifactId>maven-clean-plugin</artifactId>
  49. <version>3.0.0</version>
  50. </plugin>
  51. <plugin>
  52. <groupId>cn.smallbun.screw</groupId>
  53. <artifactId>screw-maven-plugin</artifactId>
  54. <version>1.0.4</version>
  55. <dependencies>
  56. <!--HikariCP-->
  57. <dependency>
  58. <groupId>com.zaxxer</groupId>
  59. <artifactId>HikariCP</artifactId>
  60. <version>3.4.5</version>
  61. </dependency>
  62. <!-- mysql driver&ndash;&gt;-->
  63. <dependency>
  64. <groupId>mysql</groupId>
  65. <artifactId>mysql-connector-java</artifactId>
  66. <version>8.0.20</version>
  67. </dependency>
  68. <dependency>
  69. <groupId>cn.smallbun.screw</groupId>
  70. <artifactId>screw-core</artifactId>
  71. <version>1.0.4</version>
  72. </dependency>
  73. </dependencies>
  74. </plugin>
  75. </plugins>
  76. </build>
  77. </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<String> ignoreTableName = new ArrayList<>();
  45. ignoreTableName.add("test_user");
  46. ignoreTableName.add("test_group");
  47. //忽略表前缀
  48. ArrayList<String> ignorePrefix = new ArrayList<>();
  49. ignorePrefix.add("test_");
  50. //忽略表后缀
  51. ArrayList<String> 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. <!--方式二:Maven插件-->
  2. <build>
  3. <plugins>
  4. <plugin>
  5. <groupId>cn.smallbun.screw</groupId>
  6. <artifactId>screw-maven-plugin</artifactId>
  7. <version>1.0.4</version>
  8. <dependencies>
  9. <!-- HikariCP -->
  10. <dependency>
  11. <groupId>com.zaxxer</groupId>
  12. <artifactId>HikariCP</artifactId>
  13. <version>3.4.5</version>
  14. </dependency>
  15. <!--mysql driver-->
  16. <dependency>
  17. <groupId>mysql</groupId>
  18. <artifactId>mysql-connector-java</artifactId>
  19. <version>8.0.20</version>
  20. </dependency>
  21. </dependencies>
  22. <configuration>
  23. <!--username-->
  24. <username>root</username>
  25. <!--password-->
  26. <password>1234</password>
  27. <!--driver-->
  28. <driverClassName>com.mysql.cj.jdbc.Driver</driverClassName>
  29. <!--jdbc url-->
  30. <jdbcUrl>jdbc:mysql://127.0.0.1:3306/db_shopping?serverTimezone=GMT%2B8</jdbcUrl>
  31. <!--生成文件类型 HTML MD WORD-->
  32. <fileType>WORD</fileType>
  33. <!--打开文件输出目录-->
  34. <openOutputDir>false</openOutputDir>
  35. <!--生成模板-->
  36. <produceType>freemarker</produceType>
  37. <!--文档名称 为空时:将采用[数据库名称-描述-版本号]作为文档名称-->
  38. <fileName></fileName>
  39. <!--描述-->
  40. <description>数据库文档生成</description>
  41. <!--版本-->
  42. <version>${project.version}</version>
  43. <!--标题-->
  44. <title>数据库文档</title>
  45. </configuration>
  46. <executions>
  47. <execution>
  48. <phase>compile</phase>
  49. <goals>
  50. <goal>run</goal>
  51. </goals>
  52. </execution>
  53. </executions>
  54. </plugin>
  55. </plugins>
  56. </build>

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

 

 

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

三:总结

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

    

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/848043
推荐阅读
相关标签
  

闽ICP备14008679号