太方便!Spring Boot整合Screw:高效生成數(shù)據(jù)庫(kù)文檔
環(huán)境:SpringBoot3.2.5 + Screw1.0.5
1. 簡(jiǎn)介
任何一個(gè)項(xiàng)目對(duì)于維護(hù)一份數(shù)據(jù)庫(kù)表結(jié)構(gòu)文檔是至關(guān)重要的,總結(jié)起來(lái)有以下原因:
- 清晰的數(shù)據(jù)模型:
文檔提供了一個(gè)清晰的數(shù)據(jù)庫(kù)數(shù)據(jù)模型視圖,讓項(xiàng)目團(tuán)隊(duì)成員(如開(kāi)發(fā)者、測(cè)試人員、運(yùn)維人員等)都能理解數(shù)據(jù)如何存儲(chǔ)、字段的含義、數(shù)據(jù)之間的關(guān)系等。 
便于溝通和協(xié)作:
- 文檔是團(tuán)隊(duì)成員之間溝通數(shù)據(jù)庫(kù)結(jié)構(gòu)的重要工具。當(dāng)需要討論數(shù)據(jù)模型或進(jìn)行數(shù)據(jù)庫(kù)變更時(shí),文檔可以作為參考和討論的起點(diǎn)。
 
支持變更管理:
- 當(dāng)數(shù)據(jù)庫(kù)結(jié)構(gòu)需要變更時(shí)(如添加新字段、修改字段類型、刪除字段等),文檔可以記錄這些變更,并解釋變更的原因和影響。這有助于團(tuán)隊(duì)成員理解并跟蹤數(shù)據(jù)庫(kù)結(jié)構(gòu)的變化。
 
便于新成員快速上手:
- 對(duì)于新加入項(xiàng)目的成員,一份詳細(xì)的數(shù)據(jù)庫(kù)表結(jié)構(gòu)文檔可以幫助他們快速了解項(xiàng)目的數(shù)據(jù)模型,減少學(xué)習(xí)成本。
 
提高可維護(hù)性:
- 一份維護(hù)良好的數(shù)據(jù)庫(kù)表結(jié)構(gòu)文檔可以提高數(shù)據(jù)庫(kù)的可維護(hù)性。當(dāng)出現(xiàn)問(wèn)題或需要修復(fù)時(shí),文檔可以幫助開(kāi)發(fā)者快速定位問(wèn)題所在,并找到解決方案。
 
在編寫數(shù)據(jù)庫(kù)表結(jié)構(gòu)時(shí),需要投入相當(dāng)?shù)臅r(shí)間,并且存在遺漏或錯(cuò)誤的風(fēng)險(xiǎn)。而Screw是一款強(qiáng)大的工具,通過(guò)簡(jiǎn)單的配置,它就能夠自動(dòng)生成多種數(shù)據(jù)格式的數(shù)據(jù)庫(kù)表結(jié)構(gòu)文檔,從而節(jié)省時(shí)間并確保文檔的準(zhǔn)確性和完整性。
2. Screw簡(jiǎn)介
2.1 Screw特點(diǎn)
- 簡(jiǎn)潔、輕量、設(shè)計(jì)良好
 - 多數(shù)據(jù)庫(kù)支持
 - 多種格式文檔
 - 靈活擴(kuò)展
 - 支持自定義模板
 
2.2 支持的數(shù)據(jù)庫(kù)
MySQL、MariaDB、TIDB、Oracle、SqlServer、PostgreSQL、Cache DB(2016)
2.3 支持的文檔
html
目錄
圖片
具體表
圖片
word
圖片
markdown
目錄
圖片
具體表
圖片
3. 實(shí)戰(zhàn)案例
3.1 引入Screw依賴
<dependency>
  <groupId>cn.smallbun.screw</groupId>
  <artifactId>screw-core</artifactId>
  <version>1.0.5</version>
</dependency>3.2 定義Screw可配置項(xiàng)
public class ScrewProperties {
  /**數(shù)據(jù)庫(kù)腳本版本*/
  private String version ;
  /**標(biāo)題*/
  private String title ;
  /**數(shù)據(jù)庫(kù)腳本說(shuō)明*/
  private String desc ;
  /**機(jī)構(gòu)*/
  private String org ;
  /**機(jī)構(gòu)網(wǎng)址*/
  private String orgUrl ;
  /**是否啟用*/
  private boolean enabled = false ;
  private boolean autoGen = false ;
  /**全局配置*/
  private ScrewConfig config = new ScrewConfig() ;
  /**忽略表設(shè)置*/
  private TableConfig tables = new TableConfig() ;
  public static class TableConfig {
    /**指定生成的表*/
    private List<String> designatedTables = new ArrayList<>() ;
    /**指定生成表的前綴*/
    private List<String> designatedTablePrefixs = new ArrayList<>() ;
    /**指定生成表的后綴*/
    private List<String> designatedTableSuffixs = new ArrayList<>() ;
    /**忽略表*/
    private List<String> ignoreTables = new ArrayList<>() ;
    /**忽略表前綴*/
    private List<String> ignoreTablePrefixs = new ArrayList<>() ;
    /**忽略表后綴*/
    private List<String> ignoreTableSuffixs = new ArrayList<>() ;
  }
  public static class ScrewConfig {
    /**文檔輸出目錄*/
    private String fileOutputDir ;
    /**生成完成是否打開(kāi)目錄*/
    private boolean openOutputDir = true ;
    /**文檔類型*/
    private EngineFileType fileType = EngineFileType.HTML;
    /**文檔生成模版類型*/
    private EngineTemplateType produceType = EngineTemplateType.freemarker ;
    /**文檔名稱*/
    private String fileName = "數(shù)據(jù)庫(kù)設(shè)計(jì)文檔" ;
  }
}生成文檔組件
@Component
public class DatabaseDocComponent {  
  private final DataSource dataSource ;
  private final ScrewProperties screwProperties ;
  public DatabaseDocComponent(DataSource dataSource, ScrewProperties screwProperties) {
    this.dataSource = dataSource ;
    this.screwProperties = screwProperties ;
  }
  public void genDocument() {
    //生成配置
    EngineConfig engineConfig = EngineConfig.builder()
      // 生成文件路徑
      .fileOutputDir(screwProperties.getConfig().getFileOutputDir())
      // 打開(kāi)目錄
      .openOutputDir(screwProperties.getConfig().isOpenOutputDir())
      // 文件類型
      .fileType(screwProperties.getConfig().getFileType())
      // 生成模板實(shí)現(xiàn)
      .produceType(screwProperties.getConfig().getProduceType())
      // 自定義文件名稱
        .fileName(screwProperties.getConfig().getFileName()).build();
    ProcessConfig processConfig = ProcessConfig.builder()
      //指定生成邏輯、當(dāng)存在指定表、指定表前綴、指定表后綴時(shí),將生成指定表,其余表不生成、并跳過(guò)忽略表配置  
      //根據(jù)名稱指定表生成
      .designatedTableName(screwProperties.getTables().getDesignatedTables())
      //根據(jù)表前綴生成
      .designatedTablePrefix(screwProperties.getTables().getDesignatedTablePrefixs())
      //根據(jù)表后綴生成  
      .designatedTableSuffix(screwProperties.getTables().getDesignatedTableSuffixs())
          //忽略表名
          .ignoreTableName(screwProperties.getTables().getIgnoreTables())
          //忽略表前綴
          .ignoreTablePrefix(screwProperties.getTables().getIgnoreTablePrefixs())
          //忽略表后綴
          .ignoreTableSuffix(screwProperties.getTables().getIgnoreTableSuffixs()).build();
    //配置
    Configuration config = Configuration.builder()
      //版本
      .version(screwProperties.getVersion())
      .title(screwProperties.getTitle())
      //描述
      .description(screwProperties.getDesc())
      .organization(screwProperties.getOrg())
      .organizationUrl(screwProperties.getOrgUrl())
      //數(shù)據(jù)源
      .dataSource(dataSource)
      //生成配置
      .engineConfig(engineConfig)
      //生成配置
      .produceConfig(processConfig)
      .build() ;
    //執(zhí)行生成
    new DocumentationExecute(config).execute() ;
  }
}3.3 測(cè)試文檔生成
@SpringBootTest
public class ScrewTest {
  @Resource
  private DatabaseDocComponent doc ;
  @Test
  public void testGenDoc() {
    doc.genDocument() ;
  }
}在你指定的位置生成了文檔;
圖片
圖片















 
 
 














 
 
 
 