SpringBoot集成screw实现数据库文档生成的代码示例

 更新时间:2024年07月25日 08:38:16   作者:HBLOG  
数据库设计文档是项目技术文档的重要组成部分,Screw 是一款开源的数据库文档生成工具,它支持多种数据库类型,并能生成丰富格式的文档,本文将通过一个实际的例子,展示如何使用 Spring Boot 集成 Screw 生成数据库设计文档

1.什么是screw?

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

数据库支持

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

2.环境准备

参考之前springboot对接mysql的教程里面的mysql环境搭建 http://www.liuhaihua.cn/archives/710165.html

3.代码工程

实验目的

生成mysql数据库的word文档

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springboot-demo</artifactId>
        <groupId>com.et</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Screw</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>cn.smallbun.screw</groupId>
            <artifactId>screw-core</artifactId>
            <version>1.0.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
    </dependencies>
</project>

生成类

package com.et.screw;

import cn.smallbun.screw.core.Configuration;
import cn.smallbun.screw.core.engine.EngineConfig;
import cn.smallbun.screw.core.engine.EngineFileType;
import cn.smallbun.screw.core.engine.EngineTemplateType;
import cn.smallbun.screw.core.execute.DocumentationExecute;
import cn.smallbun.screw.core.process.ProcessConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@SpringBootApplication
public class Application implements ApplicationRunner {

    @Autowired
    ApplicationContext applicationContext;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {

        DataSource dataSourceMysql = applicationContext.getBean(DataSource.class);

        //模板引擎配置 生成文件配置
        EngineConfig engineConfig = EngineConfig.builder()
                // 生成文件路径
                .fileOutputDir("D://tmp/")
                // 打开目录
                .openOutputDir(false)
                // 文件类型
                .fileType(EngineFileType.WORD)
                // 生成模板实现
                .produceType(EngineTemplateType.freemarker).build();

        // 生成文档配置(包含以下自定义版本号、描述等配置连接),文档名称拼接:数据库名_描述_版本.扩展名
        Configuration config = Configuration.builder()
                .title("数据库文档")
                // 版本号
                .version("1.0.0")
                // 描述
                .description("数据库设计文档")
                // 数据源
                .dataSource(dataSourceMysql)
                // 模板引擎配置
                .engineConfig(engineConfig)
                // 加载配置:想要生成的表、想要忽略的表
                .produceConfig(getProcessConfig())
                .build();
        // 执行生成
        new DocumentationExecute(config).execute();
    }

    /**
     * 配置想要生成的表+ 配置想要忽略的表
     *
     * @return 生成表配置
     */
    public static ProcessConfig getProcessConfig() {
        // 忽略表名
        List<String> ignoreTableName = Arrays.asList("");

        return ProcessConfig.builder()
                //根据名称指定表生成
                .designatedTableName(new ArrayList<>())
                //根据表前缀生成
                .designatedTablePrefix(new ArrayList<>())
                //根据表后缀生成
                .designatedTableSuffix(new ArrayList<>())
                //忽略表名
                .ignoreTableName(ignoreTableName)
                .build();
    }

}

application.properties

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
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
spring.datasource.username=root
spring.datasource.password=123456

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

代码仓库

4.测试

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

5.引用

https://gitee.com/leshalv/screw

到此这篇关于SpringBoot集成screw实现数据库文档生成的代码示例的文章就介绍到这了,更多相关SpringBoot screw数据库文档生成内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 记录jdk21连接SQLServer因为TLS协议报错问题

    记录jdk21连接SQLServer因为TLS协议报错问题

    在使用Druid连接池连接SQL Server时,可能会遇到因TLS版本不匹配导致的连接失败问题,具体表现为客户端使用TLS1.3或TLS1.2,而SQL Server仅支持TLS1.0,导致无法建立安全连接,解决方法是修改JDK的安全配置,启用TLS1.0
    2024-10-10
  • 使用Java Api操作HDFS过程详解

    使用Java Api操作HDFS过程详解

    这篇文章主要介绍了使用Java Api操作HDFS过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • Java中的MessageFormat.format用法实例

    Java中的MessageFormat.format用法实例

    这篇文章主要介绍了Java中的MessageFormat.format用法实例,本文先是讲解了MessageFormat的语法,然后给出了多个操作实例,需要的朋友可以参考下
    2015-06-06
  • Spring BeanFactory和FactoryBean区别解析

    Spring BeanFactory和FactoryBean区别解析

    这篇文章主要介绍了Spring BeanFactory和FactoryBean区别解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • 关于java中基本数据类型的数值范围

    关于java中基本数据类型的数值范围

    这篇文章主要介绍了关于java中基本数据类型的数值范围,基本类型,或者叫做内置类型,是JAVA中不同于类的特殊类型,它们是我们编程中使用最频繁的类型,需要的朋友可以参考下
    2023-07-07
  • Springboot整合itext实现PDF文件合并

    Springboot整合itext实现PDF文件合并

    这篇文章主要为大家详细介绍了Springboot整合itext实现PDF文件合并以及识别图片转成PDF拼接的相关知识,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-11-11
  • Spring-data-redis操作redis知识总结

    Spring-data-redis操作redis知识总结

    这篇文章主要介绍了Spring-data-redis操作redis知识总结,spring-data-redis是spring-data模块的一部分,专门用来支持在spring管理项目对redis的操作。
    2017-04-04
  • java TreeMap源码解析详解

    java TreeMap源码解析详解

    这篇文章主要介绍了java TreeMap源码解析详解的相关资料,需要的朋友可以参考下
    2017-04-04
  • 解决springboot jpa @Column columnDefinition等属性失效问题

    解决springboot jpa @Column columnDefinition等属性失效问题

    这篇文章主要介绍了解决springboot jpa @Column columnDefinition等属性失效问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • java 查找list中重复数据实例详解

    java 查找list中重复数据实例详解

    这篇文章主要介绍了java 查找list中重复数据实例详解的相关资料,需要的朋友可以参考下
    2017-01-01

最新评论