当前位置:   article > 正文

Spring Boot集成screw实现数据库文档生成

Spring Boot集成screw实现数据库文档生成

1.什么是screw?

在企业级开发中、我们经常会有编写数据库表结构文档的时间付出,从业以来,待过几家企业,关于数据库表结构文档状态:要么没有、要么有、但都是手写、后期运维开发,需要手动进行维护到文档中,很是繁琐、如果忘记一次维护、就会给以后工作造成很多困扰、无形中制造了很多坑留给自己和后人

数据库支持

  •  MySQL
  •  MariaDB
  •  TIDB
  •  Oracle
  •  SqlServer
  •  PostgreSQL
  •  Cache DB(2016)
  •  H2 (开发中)
  •  DB2 (开发中)
  •  HSQL (开发中)
  •  SQLite(开发中)

2.环境准备

参考之前springboot对接mysql的教程里面的mysql环境搭建 Spring Boot集成 mysql快速入门demo | Harries Blog™

3.代码工程

实验目的

生成mysql数据库的word文档

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>springboot-demo</artifactId>
  7. <groupId>com.et</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>Screw</artifactId>
  12. <properties>
  13. <maven.compiler.source>8</maven.compiler.source>
  14. <maven.compiler.target>8</maven.compiler.target>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-web</artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-autoconfigure</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-test</artifactId>
  28. <scope>test</scope>
  29. </dependency>
  30. <dependency>
  31. <groupId>cn.smallbun.screw</groupId>
  32. <artifactId>screw-core</artifactId>
  33. <version>1.0.5</version>
  34. </dependency>
  35. <dependency>
  36. <groupId>mysql</groupId>
  37. <artifactId>mysql-connector-java</artifactId>
  38. <scope>runtime</scope>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-starter-data-jdbc</artifactId>
  43. </dependency>
  44. </dependencies>
  45. </project>

生成类

  1. package com.et.screw;
  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 org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.boot.ApplicationArguments;
  10. import org.springframework.boot.ApplicationRunner;
  11. import org.springframework.boot.SpringApplication;
  12. import org.springframework.boot.autoconfigure.SpringBootApplication;
  13. import org.springframework.context.ApplicationContext;
  14. import javax.sql.DataSource;
  15. import java.util.ArrayList;
  16. import java.util.Arrays;
  17. import java.util.List;
  18. @SpringBootApplication
  19. public class Application implements ApplicationRunner {
  20. @Autowired
  21. ApplicationContext applicationContext;
  22. public static void main(String[] args) {
  23. SpringApplication.run(Application.class, args);
  24. }
  25. @Override
  26. public void run(ApplicationArguments args) throws Exception {
  27. DataSource dataSourceMysql = applicationContext.getBean(DataSource.class);
  28. //模板引擎配置 生成文件配置
  29. EngineConfig engineConfig = EngineConfig.builder()
  30. // 生成文件路径
  31. .fileOutputDir("D://tmp/")
  32. // 打开目录
  33. .openOutputDir(false)
  34. // 文件类型
  35. .fileType(EngineFileType.WORD)
  36. // 生成模板实现
  37. .produceType(EngineTemplateType.freemarker).build();
  38. // 生成文档配置(包含以下自定义版本号、描述等配置连接),文档名称拼接:数据库名_描述_版本.扩展名
  39. Configuration config = Configuration.builder()
  40. .title("数据库文档")
  41. // 版本号
  42. .version("1.0.0")
  43. // 描述
  44. .description("数据库设计文档")
  45. // 数据源
  46. .dataSource(dataSourceMysql)
  47. // 模板引擎配置
  48. .engineConfig(engineConfig)
  49. // 加载配置:想要生成的表、想要忽略的表
  50. .produceConfig(getProcessConfig())
  51. .build();
  52. // 执行生成
  53. new DocumentationExecute(config).execute();
  54. }
  55. /**
  56. * 配置想要生成的表+ 配置想要忽略的表
  57. *
  58. * @return 生成表配置
  59. */
  60. public static ProcessConfig getProcessConfig() {
  61. // 忽略表名
  62. List<String> ignoreTableName = Arrays.asList("");
  63. return ProcessConfig.builder()
  64. //根据名称指定表生成
  65. .designatedTableName(new ArrayList<>())
  66. //根据表前缀生成
  67. .designatedTablePrefix(new ArrayList<>())
  68. //根据表后缀生成
  69. .designatedTableSuffix(new ArrayList<>())
  70. //忽略表名
  71. .ignoreTableName(ignoreTableName)
  72. .build();
  73. }
  74. }

application.properties

  1. spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
  2. spring.datasource.url=jdbc:mysql://127.0.0.1:3306/jwordpress?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=Asia/Shanghai
  3. spring.datasource.username=root
  4. spring.datasource.password=123456

以上只是一些关键代码,所有代码请参见下面代码仓库

代码仓库

4.测试

启动Spring boot Application,在D://tmp/查看生成的文件

7218196956289

5.引用

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

闽ICP备14008679号