Skip to content

Commit 7ceb293

Browse files
author
YunaiV
committed
使用 screw 生成接口文档
1 parent b8e503e commit 7ceb293

File tree

4 files changed

+142
-1
lines changed

4 files changed

+142
-1
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>lab-70-db-doc</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>lab-70-db-doc-screw-01</artifactId>
13+
14+
15+
16+
<dependencies>
17+
<!-- screw 库,简洁好用的数据库表结构文档生成器 -->
18+
<dependency>
19+
<groupId>cn.smallbun.screw</groupId>
20+
<artifactId>screw-core</artifactId>
21+
<version>1.0.5</version>
22+
</dependency>
23+
24+
<!-- 数据库连接 -->
25+
<dependency>
26+
<groupId>com.zaxxer</groupId>
27+
<artifactId>HikariCP</artifactId>
28+
<version>3.4.5</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>mysql</groupId>
32+
<artifactId>mysql-connector-java</artifactId>
33+
<version>8.0.22</version>
34+
</dependency>
35+
</dependencies>
36+
37+
</project>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import cn.smallbun.screw.core.Configuration;
2+
import cn.smallbun.screw.core.engine.EngineConfig;
3+
import cn.smallbun.screw.core.engine.EngineFileType;
4+
import cn.smallbun.screw.core.engine.EngineTemplateType;
5+
import cn.smallbun.screw.core.execute.DocumentationExecute;
6+
import cn.smallbun.screw.core.process.ProcessConfig;
7+
import com.zaxxer.hikari.HikariConfig;
8+
import com.zaxxer.hikari.HikariDataSource;
9+
10+
import javax.sql.DataSource;
11+
import java.util.Arrays;
12+
import java.util.Collections;
13+
14+
public class ScrewMain {
15+
16+
private static final String DB_URL = "jdbc:mysql://400-infra.server.iocoder.cn:3306";
17+
private static final String DB_NAME = "mall_system";
18+
private static final String DB_USERNAME = "root";
19+
private static final String DB_PASSWORD = "3WLiVUBEwTbvAfsh";
20+
21+
private static final String FILE_OUTPUT_DIR = "/Users/yunai/screw_test";
22+
private static final EngineFileType FILE_OUTPUT_TYPE = EngineFileType.HTML; // 可以设置 WORD 或者 Markdown 格式
23+
private static final String DOC_FILE_NAME = "数据库文档";
24+
private static final String DOC_VERSION = "1.0.0";
25+
private static final String DOC_DESCRIPTION = "文档描述";
26+
27+
public static void main(String[] args) {
28+
// 创建 screw 的配置
29+
Configuration config = Configuration.builder()
30+
.version(DOC_VERSION) // 版本
31+
.description(DOC_DESCRIPTION) // 描述
32+
.dataSource(buildDataSource()) // 数据源
33+
.engineConfig(buildEngineConfig()) // 引擎配置
34+
.produceConfig(buildProcessConfig()) // 处理配置
35+
.build();
36+
37+
// 执行 screw,生成数据库文档
38+
new DocumentationExecute(config).execute();
39+
}
40+
41+
/**
42+
* 创建数据源
43+
*/
44+
private static DataSource buildDataSource() {
45+
// 创建 HikariConfig 配置类
46+
HikariConfig hikariConfig = new HikariConfig();
47+
hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
48+
hikariConfig.setJdbcUrl(DB_URL + "/" + DB_NAME);
49+
hikariConfig.setUsername(DB_USERNAME);
50+
hikariConfig.setPassword(DB_PASSWORD);
51+
hikariConfig.addDataSourceProperty("useInformationSchema", "true"); // 设置可以获取 tables remarks 信息
52+
// 创建数据源
53+
return new HikariDataSource(hikariConfig);
54+
}
55+
56+
/**
57+
* 创建 screw 的引擎配置
58+
*/
59+
private static EngineConfig buildEngineConfig() {
60+
return EngineConfig.builder()
61+
.fileOutputDir(FILE_OUTPUT_DIR) // 生成文件路径
62+
.openOutputDir(false) // 打开目录
63+
.fileType(FILE_OUTPUT_TYPE) // 文件类型
64+
.produceType(EngineTemplateType.freemarker) // 文件类型
65+
.fileName(DOC_FILE_NAME) // 自定义文件名称
66+
.build();
67+
}
68+
69+
/**
70+
* 创建 screw 的处理配置,一般可忽略
71+
* 指定生成逻辑、当存在指定表、指定表前缀、指定表后缀时,将生成指定表,其余表不生成、并跳过忽略表配置
72+
*/
73+
private static ProcessConfig buildProcessConfig() {
74+
return ProcessConfig.builder()
75+
.designatedTableName(Collections.<String>emptyList()) // 根据名称指定表生成
76+
.designatedTablePrefix(Collections.<String>emptyList()) //根据表前缀生成
77+
.designatedTableSuffix(Collections.<String>emptyList()) // 根据表后缀生成
78+
.ignoreTableName(Arrays.asList("test_user", "test_group")) // 忽略表名
79+
.ignoreTablePrefix(Collections.singletonList("test_")) // 忽略表前缀
80+
.ignoreTableSuffix(Collections.singletonList("_test")) // 忽略表后缀
81+
.build();
82+
}
83+
84+
}

lab-70-db-doc/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>labs-parent</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>lab-70-db-doc</artifactId>
13+
<packaging>pom</packaging>
14+
15+
<modules>
16+
<module>lab-70-db-doc-screw-01</module>
17+
</modules>
18+
19+
</project>

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<!-- <module>lab-21</module>-->
3232
<!-- <module>lab-22</module>-->
3333
<!-- <module>lab-23</module>-->
34-
<module>lab-24</module>
34+
<!-- <module>lab-24</module>-->
3535
<!-- <module>lab-25</module>-->
3636
<!-- <module>lab-26</module>-->
3737
<!-- <module>lab-27</module>-->
@@ -77,6 +77,7 @@
7777
<!-- <module>lab-67</module>-->
7878
<!-- <module>lab-68-spring-security-oauth</module>-->
7979
<!-- <module>lab-69-proxy</module>-->
80+
<module>lab-70-db-doc</module>
8081

8182
<!-- Spring Cloud 示例 -->
8283
<!-- <module>labx-01-spring-cloud-alibaba-nacos-discovery</module>-->

0 commit comments

Comments
 (0)