1. 程式人生 > 其它 >工具庫一鍵生成資料庫說明文件

工具庫一鍵生成資料庫說明文件

技術標籤:springboot工具庫資料庫

一鍵生成資料庫文件

參考網址:

https://mp.weixin.qq.com/s?__biz=MzI4Njc5NjM1NQ==&mid=2247501250&idx=1&sn=4908cc0df374ff99bf89e27cb5aadd51&chksm=ebd5faeedca273f84a97477d296e48fbb515e65d94a543f2222943fd151e10184d5e526d8a47&mpshare=1&scene=23&srcid=1223r3UA0gszHBV74dRmcA05&sharer_sharetime=1608730912167&sharer_shareid=9d1e76e919cc0b2f3ca23ed1f5ef67a8#rd

https://mp.weixin.qq.com/s/Bo_U5_cl82hfQ6GmRs2vtA

1.簡介

在企業級開發中、我們經常會有編寫資料庫表結構文件的時間付出,從業以來,待過幾家企業,關於資料庫表結構文件狀態:要麼沒有、要麼有、但都是手寫、後期運維開發,需要手動進行維護到文件中,很是繁瑣、如果忘記一次維護、就會給以後工作造成很多困擾、無形中製造了很多坑留給自己和後人,於是需要一個外掛工具screw[1]來維護。

2.特點

  • 簡潔、輕量、設計良好。不需要 powerdesigner 這種重量的建模工具
  • 多資料庫支援 。支援市面常見的資料庫型別 MySQL、Oracle、SqlServer
  • 多種格式文件。支援 MD、HTML、WORD 格式
  • 靈活擴充套件。支援使用者自定義模板和展示樣式

3.使用方式

  • 通過自定義程式碼配置文件生成

  • 通過外掛的形式生成文件

個人推薦使用通過自定義程式碼配置文件生成

說明:通過自定義程式碼配置文件生成原因

使用springboot專案,我們不需要再pom.xml進行配置,減少耦合

4.實踐說明

舉例的資料庫,是開源專案ruoyi的資料表

1.新建springboot專案

2.匯入相關的依賴

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.shaoming</groupId> <artifactId>springboot-sql-dev-file</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-sql-dev-file</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!--需要引入的依賴--> <!-- mysql 驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- springboot的web依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--springBoot資料庫連線 必須配置資料來源等資訊 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- 資料庫生成文件所需要的依賴 freemarker模板依賴 --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.30</version> </dependency> <!-- 資料庫生成模板的核心依賴 --> <dependency> <groupId>cn.smallbun.screw</groupId> <artifactId>screw-core</artifactId> <version>1.0.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

3.配置springboot配置檔案

spring.datasource.url=jdbc:mysql://localhost:3306/ruoyi?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&useInformationSchema=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.xa.properties.useInformationSchema=true

4.寫springboot測試方法進行生成文件

4.1 springboot方式生成資料庫文件

package com.shaoming;

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 com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.util.ResourceUtils;

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

@SpringBootTest
class SpringbootSqlDevFileApplicationTests {

    @Autowired
    ApplicationContext applicationContext;

    @Test
    void contextLoads() {
        DataSource dataSourceMysql = applicationContext.getBean(DataSource.class);


        // 生成檔案配置
        EngineConfig engineConfig = EngineConfig.builder()
                // 生成檔案路徑,自己mac本地的地址,這裡需要自己更換下路徑
                .fileOutputDir(System.getProperty("user.dir") + "/src/main/resources/sqlfile/")
                // 開啟目錄
                .openOutputDir(false)
                // 檔案型別
                .fileType(EngineFileType.MD)
                // 生成模板實現
                .produceType(EngineTemplateType.freemarker).build();

        // 生成文件配置(包含以下自定義版本號、描述等配置連線)
        Configuration config = Configuration.builder()
                .version("1.0.3")
                .description("生成文件資訊描述")
                .dataSource(dataSourceMysql)
                .engineConfig(engineConfig)
                .produceConfig(getProcessConfig())
                .build();

        // 執行生成
        new DocumentationExecute(config).execute();
    }


    /**
     * 配置想要生成的表+ 配置想要忽略的表
     * @return 生成表配置
     */
    public static ProcessConfig getProcessConfig(){
        // 忽略表名
        List<String> ignoreTableName = Arrays.asList("aa","test_group");
        // 忽略表字首,如忽略a開頭的資料庫表
        List<String> ignorePrefix = Arrays.asList("a","t");
        // 忽略表字尾
        List<String> ignoreSuffix = Arrays.asList("_test","czb_");

        return ProcessConfig.builder()
                //根據名稱指定表生成
                .designatedTableName(new ArrayList<>())
                //根據表字首生成
                .designatedTablePrefix(new ArrayList<>())
                //根據表字尾生成
                .designatedTableSuffix(new ArrayList<>())
                //忽略表名
                .ignoreTableName(ignoreTableName)
                //忽略表字首
                .ignoreTablePrefix(ignorePrefix)
                //忽略表字尾
                .ignoreTableSuffix(ignoreSuffix).build();
    }

   
}

4.2 maven專案方式生成資料庫文件

package com.shaoming;

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 com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.util.ResourceUtils;

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

@SpringBootTest
class SpringbootSqlDevFileApplicationTests {

    @Autowired
    ApplicationContext applicationContext;

    /**
     * 使用maven工程生成資料庫說明文件
     * 參考網址:
     https://mp.weixin.qq.com/s/Bo_U5_cl82hfQ6GmRs2vtA
     */
    @Test
    public void shouldAnswerWithTrue() {
        //資料來源
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
        hikariConfig.setJdbcUrl("jdbc:mysql://localhost:3306/ruoyi?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&useInformationSchema=true");
        hikariConfig.setUsername("root");
        hikariConfig.setPassword("root");
        //設定可以獲取tables remarks資訊
        hikariConfig.addDataSourceProperty("useInformationSchema", "true");
        hikariConfig.setMinimumIdle(2);
        hikariConfig.setMaximumPoolSize(5);
        DataSource dataSource = new HikariDataSource(hikariConfig);
        //生成配置
        EngineConfig engineConfig = EngineConfig.builder()
                //生成檔案路徑
                .fileOutputDir(System.getProperty("user.dir") + "/src/main/resources/sqlfile/")
                //開啟目錄
                .openOutputDir(true)
                //檔案型別
                .fileType(EngineFileType.HTML)
                //生成模板實現
                .produceType(EngineTemplateType.freemarker).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()
                //忽略表名
                .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();
    }

}

5.文件型別說明

定位到這個方法

  EngineConfig engineConfig = EngineConfig.builder()
                //生成檔案路徑
                .fileOutputDir(System.getProperty("user.dir") + "/src/main/resources/sqlfile/")
                //開啟目錄
                .openOutputDir(true)
                //檔案型別
                .fileType(EngineFileType.HTML)
                //生成模板實現
                .produceType(EngineTemplateType.freemarker).build();

.fileType(EngineFileType.HTML)

這表示生成html格式的說明文件

一共有三種類型的說明文件可以生成

  • html
  • md
  • word

下面是對應的列舉類原始碼(瞭解即可)

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package cn.smallbun.screw.core.engine;

import java.io.Serializable;

public enum EngineFileType implements Serializable {
    HTML(".html", "documentation_html", "HTML檔案"),
    WORD(".doc", "documentation_word", "WORD檔案"),
    MD(".md", "documentation_md", "Markdown檔案");

    private String fileSuffix;
    private String templateNamePrefix;
    private String desc;

    private EngineFileType(String type, String templateFile, String desc) {
        this.fileSuffix = type;
        this.templateNamePrefix = templateFile;
        this.desc = desc;
    }

    public String getFileSuffix() {
        return this.fileSuffix;
    }

    public void setFileSuffix(String fileSuffix) {
        this.fileSuffix = fileSuffix;
    }

    public String getTemplateNamePrefix() {
        return this.templateNamePrefix;
    }

    public void setTemplateNamePrefix(String templateNamePrefix) {
        this.templateNamePrefix = templateNamePrefix;
    }

    public String getDesc() {
        return this.desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}

6.需要注意的問題

  • 生成後文檔亂碼?

    MySQL:URL加入?characterEncoding=UTF-8

  • Caused by: java.lang.NoSuchFieldError: VERSION_2_3_30?

    檢查專案freemarker依賴,這是由於版本過低造成的,升級版本為2.3.30即可。

  • java.lang.AbstractMethodError: oracle.jdbc.driver.T4CConnection.getSchema()Ljava/lang/String;

    這是因為oracle驅動版本過低造成的,刪除或遮蔽目前驅動版本,驅動添加升級為以下版本:

  • MySQL資料庫表和列欄位有說明、生成文件沒有說明?

    URL連結加入useInformationSchema=true即可。

  • java.lang.AbstractMethodError: com.mysql.jdbc.JDBC4Connection.getSchema()Ljava/lang/String;

    這是因為mysql驅動版本過低造成的,升級mysql驅動版本為最新即可。

個人csdn部落格網址:https://blog.csdn.net/shaoming314

jam