一鍵生成數(shù)據(jù)庫文檔,堪稱數(shù)據(jù)庫界的Swagger
1
簡介
在企業(yè)級開發(fā)中、我們經(jīng)常會有編寫數(shù)據(jù)庫表結(jié)構(gòu)文檔的時間付出,從業(yè)以來,待過幾家企業(yè),關(guān)于數(shù)據(jù)庫表結(jié)構(gòu)文檔狀態(tài):要么沒有、要么有、但都是手寫、后期運(yùn)維開發(fā),需要手動進(jìn)行維護(hù)到文檔中,很是繁瑣、如果忘記一次維護(hù)、就會給以后工作造成很多困擾、無形中制造了很多坑留給自己和后人,于是萌生了要自己寫一個插件工具的想法
2
特點(diǎn)
?
簡潔、輕量、設(shè)計(jì)良好
多數(shù)據(jù)庫支持
多種格式文檔
靈活擴(kuò)展
支持自定義模板
3
數(shù)據(jù)庫支持
?
[x] MySQL
[x] MariaDB
[x] TIDB
[x] Oracle
[x] SqlServer
[x] PostgreSQL
[x] Cache DB(2016)
[ ] H2 (開發(fā)中)
[ ] DB2? (開發(fā)中)
[ ] HSQL? (開發(fā)中)
[ ] SQLite(開發(fā)中)
[ ] 瀚高(開發(fā)中)
[ ] 達(dá)夢 (開發(fā)中)
[ ] 虛谷? (開發(fā)中)
[ ] 人大金倉(開發(fā)中)
?
4
文檔生成支持
?
[x] html
[x] word
[x] markdown
5
文檔截圖
?
html


word

markdwon


6
使用方式
普通方式
引入依賴
<dependency>??
????<groupId>cn.smallbun.screwgroupId>??
????<artifactId>screw-coreartifactId>??
????<version>${lastVersion}version>??
?dependency>編寫代碼
/**
?* 文檔生成
?*/??
void?documentGeneration() {
???//數(shù)據(jù)源
???HikariConfig hikariConfig = new?HikariConfig();
???hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
???hikariConfig.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/database");
???hikariConfig.setUsername("root");
???hikariConfig.setPassword("password");
???//設(shè)置可以獲取tables remarks信息
???hikariConfig.addDataSourceProperty("useInformationSchema", "true");
???hikariConfig.setMinimumIdle(2);
???hikariConfig.setMaximumPoolSize(5);
???DataSource dataSource = new?HikariDataSource(hikariConfig);
???//生成配置
???EngineConfig engineConfig = EngineConfig.builder()
?????????//生成文件路徑
?????????.fileOutputDir(fileOutputDir)
?????????//打開目錄
?????????.openOutputDir(true)
?????????//文件類型
?????????.fileType(EngineFileType.HTML)
?????????//生成模板實(shí)現(xiàn)
?????????.produceType(EngineTemplateType.freemarker)
?????????//自定義文件名稱
?????????.fileName("自定義文件名稱").build();
??
???//忽略表
???ArrayList ignoreTableName = new?ArrayList<>();
???ignoreTableName.add("test_user");
???ignoreTableName.add("test_group");
???//忽略表前綴
???ArrayList ignorePrefix = new?ArrayList<>();
???ignorePrefix.add("test_");
???//忽略表后綴
???ArrayList ignoreSuffix = new?ArrayList<>();
???ignoreSuffix.add("_test");
???ProcessConfig processConfig = ProcessConfig.builder()
?????????//指定生成邏輯、當(dāng)存在指定表、指定表前綴、指定表后綴時,將生成指定表,其余表不生成、并跳過忽略表配置
???//根據(jù)名稱指定表生成
???.designatedTableName(new?ArrayList<>())
???//根據(jù)表前綴生成
???.designatedTablePrefix(new?ArrayList<>())
???//根據(jù)表后綴生成
???.designatedTableSuffix(new?ArrayList<>())
?????????//忽略表名
?????????.ignoreTableName(ignoreTableName)
?????????//忽略表前綴
?????????.ignoreTablePrefix(ignorePrefix)
?????????//忽略表后綴
?????????.ignoreTableSuffix(ignoreSuffix).build();
???//配置
???Configuration config = Configuration.builder()
?????????//版本
?????????.version("1.0.0")
?????????//描述
?????????.description("數(shù)據(jù)庫設(shè)計(jì)文檔生成")
?????????//數(shù)據(jù)源
?????????.dataSource(dataSource)
?????????//生成配置
?????????.engineConfig(engineConfig)
?????????//生成配置
?????????.produceConfig(processConfig)
?????????.build();
???//執(zhí)行生成
???new?DocumentationExecute(config).execute();
} Maven 插件
<build>??
????<plugins>??
????????<plugin>??
????????????<groupId>cn.smallbun.screwgroupId>??
????????????<artifactId>screw-maven-pluginartifactId>??
????????????<version>${lastVersion}version>??
????????????<dependencies>??
??????????????????
????????????????<dependency>??
????????????????????<groupId>com.zaxxergroupId>??
????????????????????<artifactId>HikariCPartifactId>??
????????????????????<version>3.4.5version>??
????????????????dependency>??
??????????????????
????????????????<dependency>??
????????????????????<groupId>mysqlgroupId>??
????????????????????<artifactId>mysql-connector-javaartifactId>??
????????????????????<version>8.0.20version>??
????????????????dependency>??
????????????dependencies>??
????????????<configuration>??
??????????????????
????????????????<username>rootusername>??
??????????????????
????????????????<password>passwordpassword>??
??????????????????
????????????????<driverClassName>com.mysql.cj.jdbc.DriverdriverClassName>??
??????????????????
????????????????<jdbcUrl>jdbc:mysql://127.0.0.1:3306/xxxxjdbcUrl>??
??????????????????
????????????????<fileType>HTMLfileType>??
??????????????????
????????????????<openOutputDir>falseopenOutputDir>??
??????????????????
????????????????<produceType>freemarkerproduceType>??
??????????????????
????????????????<fileName>測試文檔名稱fileName>??
??????????????????
????????????????<description>數(shù)據(jù)庫文檔生成description>??
??????????????????
????????????????<version>${project.version}version>??
??????????????????
????????????????<title>數(shù)據(jù)庫文檔title>??
????????????configuration>??
????????????<executions>??
????????????????<execution>??
????????????????????<phase>compilephase>??
????????????????????<goals>??
????????????????????????<goal>rungoal>??
????????????????????goals>??
????????????????execution>??
????????????executions>??
????????plugin>??
????plugins>??
build>7
擴(kuò)展模塊
pojo生成功能
功能簡介
pojo生成功能是基于screw延伸出的擴(kuò)展模塊,目前處于初步開發(fā)的狀態(tài)。在日常的開發(fā)中,經(jīng)過需求分析、建模之后,往往會先在數(shù)據(jù)庫中建表,其次在進(jìn)行代碼的開發(fā)。
那么pojo生成功能在這個階段就可以幫助大家節(jié)省一些重復(fù)勞動了。使用pojo生成功能可以直接根據(jù)數(shù)據(jù)庫生成對應(yīng)的java pojo對象。這樣后續(xù)的修改,開發(fā)都會很方便。
數(shù)據(jù)庫支持
[x] MySQL
使用方式
引入依賴
<dependency>??
????<groupId>cn.smallbun.screwgroupId>??
????<artifactId>screw-extensionartifactId>??
????<version>${lastVersion}version>??
dependency>編寫代碼
/**
?* pojo生成
?*/??
void?pojoGeneration()?{
????//數(shù)據(jù)源
????HikariConfig hikariConfig = new?HikariConfig();
????hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
????hikariConfig.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/screw");
????hikariConfig.setUsername("screw");
????hikariConfig.setPassword("screw");
????//設(shè)置可以獲取tables remarks信息
????hikariConfig.addDataSourceProperty("useInformationSchema", "true");
????hikariConfig.setMinimumIdle(2);
????hikariConfig.setMaximumPoolSize(5);
????DataSource dataSource = new?HikariDataSource(hikariConfig);
??
????ProcessConfig processConfig = ProcessConfig.builder()
????????//指定生成邏輯、當(dāng)存在指定表、指定表前綴、指定表后綴時,將生成指定表,其余表不生成、并跳過忽略表配置
????????//根據(jù)名稱指定表生成
????????.designatedTableName(new?ArrayList<>())
????????//根據(jù)表前綴生成
????????.designatedTablePrefix(new?ArrayList<>())
????????//根據(jù)表后綴生成
????????.designatedTableSuffix(new?ArrayList<>()).build();
??
????//設(shè)置生成pojo相關(guān)配置
????PojoConfiguration config = new?PojoConfiguration();
????//設(shè)置文件存放路徑
????config.setPath("/cn/smallbun/screw/");
????//設(shè)置包名
????config.setPackageName("cn.smallbun.screw");
????//設(shè)置是否使用lombok
????config.setUseLombok(false);
????//設(shè)置數(shù)據(jù)源
????config.setDataSource(dataSource);
????//設(shè)置命名策略
????config.setNameStrategy(new?HumpNameStrategy());
????//設(shè)置表過濾邏輯
????config.setProcessConfig(processConfig);
????//執(zhí)行生成
????new?PojoExecute(config).execute();
}8
常見問題
生成后文檔亂碼?
MySQL:URL加入?characterEncoding=UTF-8。
Caused by: java.lang.NoSuchFieldError: VERSION_2_3_30?
檢查項(xiàng)目freemarker依賴,這是由于版本過低造成的,升級版本為2.3.30即可。
java.lang.AbstractMethodError: oracle.jdbc.driver.T4CConnection.getSchema()Ljava/lang/String;
這是因?yàn)閛racle驅(qū)動版本過低造成的,刪除或屏蔽目前驅(qū)動版本,驅(qū)動添加升級為以下版本:
<dependency>??
???<groupId>com.oracle.ojdbcgroupId>??
???<artifactId>ojdbc8artifactId>??
???<version>19.3.0.0version>??
dependency>??
<dependency>??
???<groupId>cn.easyprojectgroupId>??
???<artifactId>orai18nartifactId>??
???<version>12.1.0.2.0version>??
dependency>MySQL數(shù)據(jù)庫表和列字段有說明、生成文檔沒有說明?
URL鏈接加入useInformationSchema=true即可。
java.lang.AbstractMethodError: com.mysql.jdbc.JDBC4Connection.getSchema()Ljava/lang/String;
這是因?yàn)閙ysql驅(qū)動版本過低造成的,升級mysql驅(qū)動版本為最新即可。
| 項(xiàng)目地址 | https://gitee.com/leshalv/screw |
