最近部門訂單業務調整,收攏其他業務線的下單入口,做個統一大訂單平臺,需要梳理各業務線的資料表,但每個業務線庫都有近百張和訂單相關的表,挨個表一個一個欄位的弄腦瓜子嗡嗡的,
為了不重復 CV 操作,抱著一絲希望開始在GitHub里找,看看有沒有什么工具可以用,結果就真的發現了寶藏,screw(螺絲釘),居然可以生成資料庫檔案,優秀啊~,
資料庫支持
- MySQL
- MariaDB
- TIDB
- Oracle
- SqlServer
- PostgreSQL
- Cache DB
配置
1、pom檔案
引入screw核心包,HikariCP資料庫連接池,HikariCP號稱性能最出色的資料庫連接池,
2、配置資料源
配置資料源,設定 useInformationSchema 可以獲取tables注釋資訊,
spring.datasource.url=jdbc:mysql://45.93.1.5:3306/fire?useUnicode=true&characterEncoding=UTF-8&useSSL=false spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.xa.properties.useInformationSchema=true
3、screw 核心配置
screw有兩種執行方式,第一種是pom檔案配置,另一種是代碼執行,
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>cn.smallbun.screw</groupId>
<artifactId>screw-maven-plugin</artifactId>
<version>1.0.3</version>
<dependencies>
<!-- HikariCP -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
<!--mysql driver-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
</dependencies>
<configuration>
<!--username-->
<username>root</username>
<!--password-->
<password>123456</password>
<!--driver-->
<driverClassName>com.mysql.cj.jdbc.Driver</driverClassName>
<!--jdbc url-->
<jdbcUrl>jdbc:mysql://41.92.6.5:3306/fire</jdbcUrl>
<!--生成檔案型別-->
<fileType>HTML</fileType>
<!--打開檔案輸出目錄-->
<openOutputDir>false</openOutputDir>
<!--生成模板-->
<produceType>freemarker</produceType>
<!--檔案名稱 為空時:將采用[資料庫名稱-描述-版本號]作為檔案名稱-->
<!--<docName>測驗檔案名稱</docName>-->
<!--描述-->
<description>資料庫檔案生成</description>
<!--版本-->
<version>${project.version}</version>
<!--標題-->
<title>fire資料庫檔案</title>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
配置完以后在 maven project->screw 雙擊執行ok,
代碼生成方式也非常簡單,
@SpringBootTest
public class ScrewApplicationTests {
@Autowired
ApplicationContext applicationContext;
@Test
void contextLoads() {
DataSource dataSourceMysql = applicationContext.getBean(DataSource.class);
// 生成檔案配置
EngineConfig engineConfig = EngineConfig.builder()
// 生成檔案路徑,自己mac本地的地址,這里需要自己更換下路徑
.fileOutputDir("D:/")
// 打開目錄
.openOutputDir(false)
// 檔案型別
.fileType(EngineFileType.HTML)
// 生成模板實作
.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("a", "test_group");
// 忽略表前綴,如忽略a開頭的資料庫表
List<String> ignorePrefix = Arrays.asList("a", "t");
// 忽略表后綴
List<String> ignoreSuffix = Arrays.asList("_test", "czb_");
return ProcessConfig.builder()
//根據名稱指定表生成
.designatedTableName(Arrays.asList("fire_user"))
//根據表前綴生成
.designatedTablePrefix(new ArrayList<>())
//根據表后綴生成
.designatedTableSuffix(new ArrayList<>())
//忽略表名
.ignoreTableName(ignoreTableName)
//忽略表前綴
.ignoreTablePrefix(ignorePrefix)
//忽略表后綴
.ignoreTableSuffix(ignoreSuffix).build();
}
}
4、檔案格式
screw 有 HTML、DOC、MD 三種格式的檔案,
代碼中的修改
.fileType(EngineFileType.HTML)
或者pom檔案
<fileType>MD</fileType>
DOC檔案樣式
HTML檔案樣式
MD檔案樣式
最后
不得不說這個工具是真TM好用,提前完成任務,有點傲嬌有木有!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/43144.html
標籤:Java
上一篇:Java 將Html轉為PDF
