教你怎么用java一鍵自動生成數(shù)據(jù)庫文檔
前言
這是該工具的github地址:https://github.com/pingfangushi/screw
一、引入pom.xml依賴
<dependencies> <!-- screw 庫,簡潔好用的數(shù)據(jù)庫表結(jié)構(gòu)文檔生成器 --> <dependency> <groupId>cn.smallbun.screw</groupId> <artifactId>screw-core</artifactId> <version>1.0.5</version> </dependency> <!-- 數(shù)據(jù)庫連接 --> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency> </dependencies>
二、創(chuàng)建Java類
/** * @description: 使用 screw 生成文檔 * @author: DT * @date: 2021/5/9 12:19 * @version: v1.0 */ public class TestScrewMain { private static final String DB_URL = "jdbc:mysql://192.168.31.158:3306"; private static final String DB_NAME = "testdt?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"; private static final String DB_USERNAME = "root"; private static final String DB_PASSWORD = "123456"; private static final String FILE_OUTPUT_DIR = "C:\\Users\\DT開發(fā)者\\Desktop\\"; // 可以設(shè)置 Word 或者 Markdown 格式 private static final EngineFileType FILE_OUTPUT_TYPE = EngineFileType.WORD; private static final String DOC_FILE_NAME = "數(shù)據(jù)庫字典文檔"; private static final String DOC_VERSION = "V1.0.0"; private static final String DOC_DESCRIPTION = "文檔描述"; public static void main(String[] args) { // 創(chuàng)建 screw 的配置 Configuration config = Configuration.builder() // 版本 .version(DOC_VERSION) // 描述 .description(DOC_DESCRIPTION) // 數(shù)據(jù)源 .dataSource(buildDataSource()) // 引擎配置 .engineConfig(buildEngineConfig()) // 處理配置 .produceConfig(buildProcessConfig()) .build(); // 執(zhí)行 screw,生成數(shù)據(jù)庫文檔 new DocumentationExecute(config).execute(); } /** * 創(chuàng)建數(shù)據(jù)源 */ private static DataSource buildDataSource() { // 創(chuàng)建 HikariConfig 配置類 HikariConfig hikariConfig = new HikariConfig(); hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver"); hikariConfig.setJdbcUrl(DB_URL + "/" + DB_NAME); hikariConfig.setUsername(DB_USERNAME); hikariConfig.setPassword(DB_PASSWORD); // 設(shè)置可以獲取 tables remarks 信息 hikariConfig.addDataSourceProperty("useInformationSchema", "true"); // 創(chuàng)建數(shù)據(jù)源 return new HikariDataSource(hikariConfig); } /** * 創(chuàng)建 screw 的引擎配置 */ private static EngineConfig buildEngineConfig() { return EngineConfig.builder() // 生成文件路徑 .fileOutputDir(FILE_OUTPUT_DIR) // 打開目錄 .openOutputDir(false) // 文件類型 .fileType(FILE_OUTPUT_TYPE) // 文件類型 .produceType(EngineTemplateType.freemarker) // 自定義文件名稱 .fileName(DOC_FILE_NAME) .build(); } /** * 創(chuàng)建 screw 的處理配置,一般可忽略 * 指定生成邏輯、當(dāng)存在指定表、指定表前綴、指定表后綴時,將生成指定表,其余表不生成、并跳過忽略表配置 */ private static ProcessConfig buildProcessConfig() { return ProcessConfig.builder() // 根據(jù)名稱指定表生成 .designatedTableName(Collections.<String>emptyList()) // 根據(jù)表前綴生成 .designatedTablePrefix(Collections.<String>emptyList()) // 根據(jù)表后綴生成 .designatedTableSuffix(Collections.<String>emptyList()) // 忽略表名 .ignoreTableName(Arrays.asList("test", "mytable","role","t_role","t_user")) // 忽略表前綴 //.ignoreTablePrefix(Collections.singletonList("t_")) // 忽略表后綴 //.ignoreTableSuffix(Collections.singletonList("_test")) .build(); } }
三、使用 Maven 插件的方式
<plugin> <groupId>cn.smallbun.screw</groupId> <artifactId>screw-maven-plugin</artifactId> <version>1.0.5</version> <dependencies> <!-- 數(shù)據(jù)庫連接 --> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.17</version> </dependency> </dependencies> <configuration> <!-- 數(shù)據(jù)庫相關(guān)配置 --> <driverClassName>com.mysql.cj.jdbc.Driver</driverClassName> <jdbcUrl>jdbc:mysql://192.168.31.158:3306/testdt?serverTimezone=Asia/Shanghai</jdbcUrl> <username>root</username> <password>123456</password> <!-- screw 配置 --> <fileType>WORD</fileType> <title>數(shù)據(jù)庫文檔</title> <!--標(biāo)題--> <fileName>測試文檔名稱</fileName> <!--文檔名稱 為空時:將采用[數(shù)據(jù)庫名稱-描述-版本號]作為文檔名稱--> <description>數(shù)據(jù)庫文檔生成</description> <!--描述--> <version>${project.version}</version> <!--版本--> <openOutputDir>false</openOutputDir> <!--打開文件輸出目錄--> <produceType>freemarker</produceType> <!--生成模板--> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin>
執(zhí)行 screw-maven-plugin 插件,會在 doc 目錄下生成文檔。如下圖所示:
四、總結(jié)
screw 是一個簡潔好用的數(shù)據(jù)庫表結(jié)構(gòu)文檔的生成工具 ,支持 MySQL、Oracle、PostgreSQL 等主流的關(guān)系數(shù)據(jù)庫。使用起來還是很方便的,不用再去手動編寫數(shù)據(jù)字典了,非常適用,從此告別CV手寫數(shù)據(jù)庫文檔,真的是痛苦啊!他這個插件還可以生成Java實體類,不過除了Lombok其他的不支持,比如Swagger注釋,基礎(chǔ)實體字段表映射等。下一篇會給大家推薦一些生成實體類較好的腳本或者工具類。
到此這篇關(guān)于教你怎么用java一鍵自動生成數(shù)據(jù)庫文檔的文章就介紹到這了,更多相關(guān)java自動生成數(shù)據(jù)庫文檔內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java兩種動態(tài)代理JDK動態(tài)代理和CGLIB動態(tài)代理詳解
這篇文章主要介紹了Java兩種動態(tài)代理JDK動態(tài)代理和CGLIB動態(tài)代理詳解,代理模式是23種設(shè)計模式的一種,他是指一個對象A通過持有另一個對象B,可以具有B同樣的行為的模式,為了對外開放協(xié)議,B往往實現(xiàn)了一個接口,A也會去實現(xiàn)接口,需要的朋友可以參考下2023-11-11SpringBoot ApplicationContextAware拓展接口使用詳解
當(dāng)一個類實現(xiàn)了這個接口(ApplicationContextAware)之后,這個類就可以方便獲得ApplicationContext中的所有bean。換句話說,就是這個類可以直接獲取spring配置文件中,所有有引用到的bean對象2023-04-04Java 格式化輸出JSON字符串的2種實現(xiàn)操作
這篇文章主要介紹了Java 格式化輸出JSON字符串的2種實現(xiàn)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10SpringBoot基于Mybatis-Plus自動代碼生成
這篇文章主要介紹了SpringBoot基于Mybatis-Plus自動代碼生成,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Java連接數(shù)據(jù)庫JDBC技術(shù)之prepareStatement的詳細(xì)介紹
這篇文章主要介紹了Java連接數(shù)據(jù)庫JDBC技術(shù)之prepareStatement的詳細(xì)介紹,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07