使用 SpringBoot + Screw 實(shí)現(xiàn)生成數(shù)據(jù)表數(shù)據(jù)字典功能開發(fā)
在軟件開發(fā)過程中,數(shù)據(jù)字典是一個(gè)非常重要的組成部分,它能夠清晰地描述數(shù)據(jù)庫中表結(jié)構(gòu)和字段的詳細(xì)信息。在本文中,我們將介紹如何使用 Springboot 結(jié)合 screw 來實(shí)現(xiàn)生成數(shù)據(jù)表數(shù)據(jù)字典的功能。
screw 介紹
Screw 是一款功能強(qiáng)大、易于使用的數(shù)據(jù)庫文檔生成工具。它旨在幫助開發(fā)人員快速、準(zhǔn)確地獲取和整理數(shù)據(jù)庫的結(jié)構(gòu)信息,從而提高開發(fā)效率和減少因數(shù)據(jù)庫結(jié)構(gòu)不清晰而導(dǎo)致的錯(cuò)誤。
Screw 具有以下顯著特點(diǎn):
- 多數(shù)據(jù)庫支持:它能夠處理多種主流數(shù)據(jù)庫,如 MySQL、Oracle、SQL Server、PostgreSQL 等,具有廣泛的適用性。
- 豐富的文檔格式:支持生成多種常見的文檔格式,如 HTML、Word、Markdown 等,滿足不同場(chǎng)景下的需求。
- 詳細(xì)的表結(jié)構(gòu)和字段信息:提供包括表名、字段名、數(shù)據(jù)類型、長(zhǎng)度、約束條件、注釋等全面而詳細(xì)的信息,讓開發(fā)人員對(duì)數(shù)據(jù)庫結(jié)構(gòu)一目了然。
- 可定制性:允許用戶根據(jù)特定需求進(jìn)行配置,例如選擇要生成文檔的表、過濾特定字段等。
- 良好的兼容性:能夠與各種開發(fā)框架和環(huán)境集成,包括 Spring Boot 等,方便在項(xiàng)目中直接使用。
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 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>3.0.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>data-dictionary-generator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Data Dictionary Generator</name>
<description>Generate data dictionary using Spring Boot and Screw</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>cn.smallbun.screw</groupId>
<artifactId>screw-core</artifactId>
<version>1.0.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
屬性配置
在application.yml 中進(jìn)行相關(guān)配置,以下是 application.yml 的示例:
screw:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/database_name
username: root
password: root
generate:
enabled: true
output-dir: /yourPath/data_dictionary
file-type: html
前端代碼實(shí)現(xiàn)(示例)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>數(shù)據(jù)字典展示</title>
</head>
<body>
<h2>數(shù)據(jù)字典</h2>
<select id="formatSelect">
<option value="html">HTML</option>
<option value="word">Word</option>
<option value="markdown">Markdown</option>
</select>
<button onclick="fetchDataDictionary()">獲取數(shù)據(jù)字典</button>
<div id="dataDictionary"></div>
<script>
function fetchDataDictionary() {
var formatType = document.getElementById('formatSelect').value;
fetch('/dataDictionary?formatType=' + formatType)
.then(response => response.text())
.then(data => {
document.getElementById('dataDictionary').innerHTML = data;
})
.catch(error => console.error('獲取數(shù)據(jù)字典出錯(cuò):', error));
}
</script>
</body>
</html>
后端
添加相應(yīng)的 Controller 處理請(qǐng)求:
package com.example.controller;
import com.example.service.DataDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.sql.DataSource;
@RestController
public class DataDictionaryController {
@Autowired
private DataDictionaryService dataDictionaryService;
@Autowired
private DataSource dataSource;
@GetMapping("/dataDictionary")
public String getDataDictionary(@RequestParam("formatType") String formatType) {
return dataDictionaryService.generateDataDictionary(formatType, dataSource);
}
}
DataDictionaryService 實(shí)現(xiàn)類:
package com.example.service;
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.stereotype.Service;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
@Service
public class DataDictionaryService {
public String generateDataDictionary(String formatType, DataSource dataSource) {
EngineConfig engineConfig = EngineConfig.builder()
.fileType(getEngineFileType(formatType))
.produceName("data_dictionary")
.openOutputDir(false)
.build();
ProcessConfig processConfig = ProcessConfig.builder()
.includeTables(new String[]{"your_table_names"})
.build();
Configuration configuration = Configuration.builder()
.dataSource(dataSource)
.engineConfig(engineConfig)
.processConfig(processConfig)
.build();
DocumentationExecute.execute(configuration);
// 根據(jù)生成的結(jié)果返回相應(yīng)的字符串
// 具體的返回邏輯根據(jù)生成的文件類型和存儲(chǔ)方式進(jìn)行處理
return "Generated data dictionary";
}
private EngineFileType getEngineFileType(String formatType) {
Map<String, EngineFileType> formatTypeMap = new HashMap<>();
formatTypeMap.put("html", EngineFileType.HTML);
formatTypeMap.put("word", EngineFileType.WORD);
formatTypeMap.put("markdown", EngineFileType.MARKDOWN);
return formatTypeMap.getOrDefault(formatType, EngineFileType.HTML);
}
}
使用總結(jié)
通過使用 Springboot 和 screw 結(jié)合,我們能夠方便快捷地生成數(shù)據(jù)庫的數(shù)據(jù)字典。在實(shí)際開發(fā)中,這有助于提高開發(fā)效率,減少因?qū)?shù)據(jù)庫結(jié)構(gòu)不清晰而導(dǎo)致的錯(cuò)誤。同時(shí),通過前端頁面的展示,使得數(shù)據(jù)字典的查看更加直觀和便捷。