# screw-数据库表结构文档工具

screw (opens new window)

# 依赖

		<dependency>
			<groupId>org.freemarker</groupId>
			<artifactId>freemarker</artifactId>
			<version>2.3.30</version>
		</dependency>

		<dependency>
			<groupId>cn.smallbun.screw</groupId>
			<artifactId>screw-core</artifactId>
			<version>1.0.5</version>
		</dependency>

		<!-- HikariCP -->
		<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.20</version>
		</dependency>

		<dependency>
			<groupId>com.oracle.ojdbc</groupId>
			<artifactId>ojdbc8</artifactId>
			<version>19.3.0.0</version>
		</dependency>
		<dependency>
			<groupId>cn.easyproject</groupId>
			<artifactId>orai18n</artifactId>
			<version>12.1.0.2.0</version>
		</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

# 代码

package com.example.demo.Utils;

import cn.smallbun.screw.core.Configuration;
import cn.smallbun.screw.core.engine.EngineConfig;
import cn.smallbun.screw.core.engine.EngineFileType;
import cn.smallbun.screw.core.engine.EngineTemplateType;
import cn.smallbun.screw.core.execute.DocumentationExecute;
import cn.smallbun.screw.core.process.ProcessConfig;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;
import java.util.ArrayList;

/**
 * @Author: mayuanyuan
 * @Date: 2020/09/12/23:48
 * @Description:
 */
public class DbSchemaHelper {

    private static boolean isMysql = true;

    String fileOutputDir = "D:\\Users\\Desktop";

    boolean isOpen = false;

    boolean html = true;

    boolean word = false;

    boolean md = false;

    public static void main(String[] args) {
        if(isMysql){
            new DbSchemaHelper().mysql();
        }else{
            new DbSchemaHelper().oracle();
        }
    }

    void mysql(){
        //数据源
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
        hikariConfig.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/t?serverTimezone=UTC&characterEncoding=UTF-8");
        hikariConfig.setUsername("root");
        hikariConfig.setPassword("root");
        markHtml(hikariConfig);
    }

    void oracle(){
        //数据源
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        hikariConfig.setJdbcUrl("jdbc:oracle:thin:@//127.0.0.1:1521/orcl");
        hikariConfig.setUsername("root");
        hikariConfig.setPassword("root");
        markHtml(hikariConfig);
    }

    void markHtml(HikariConfig hikariConfig){
        //设置可以获取tables remarks信息
        hikariConfig.addDataSourceProperty("useInformationSchema", "true");
        hikariConfig.setMinimumIdle(2);
        hikariConfig.setMaximumPoolSize(5);
        DataSource dataSource = new HikariDataSource(hikariConfig);
        EngineFileType type = null;
        if(html){
            type = EngineFileType.HTML;
        }
        if(word){
            type = EngineFileType.WORD;
        }
        if(md){
            type = EngineFileType.MD;
        }

        // 生成文件配置
        EngineConfig engineConfig = EngineConfig.builder()
                // 生成文件路径,自己mac本地的地址,这里需要自己更换下路径
                .fileOutputDir(fileOutputDir)
                // 打开目录
                .openOutputDir(isOpen)
                // 文件类型
                .fileType(type)
                // 生成模板实现
                .produceType(EngineTemplateType.freemarker)
                //.fileName("自定义文件名称")
                .build();

        //配置
        Configuration config = Configuration.builder()
                //版本
                .version("1.0.0")
                //描述
                .description("数据库设计文档生成")
                //数据源
                .dataSource(dataSource)
                //生成配置
                .engineConfig(engineConfig)
                //忽略表
                //.produceConfig(ignoreTable())
                .build();

        // 执行生成
        new DocumentationExecute(config).execute();
    }

    public ProcessConfig ignoreTable(){
        //忽略表
        ArrayList<String> ignoreTableName = new ArrayList<>();
        ignoreTableName.add("test_user");
        //忽略表前缀
        ArrayList<String> ignorePrefix = new ArrayList<>();
        ignorePrefix.add("test_");
        //忽略表后缀
        ArrayList<String> ignoreSuffix = new ArrayList<>();
        ignoreSuffix.add("_test");
        ProcessConfig processConfig = ProcessConfig.builder()
                //指定生成逻辑、当存在指定表、指定表前缀、指定表后缀时,将生成指定表,其余表不生成、并跳过忽略表配置
                //根据名称指定表生成
                .designatedTableName(new ArrayList<>())
                //根据表前缀生成
                .designatedTablePrefix(new ArrayList<>())
                //根据表后缀生成
                .designatedTableSuffix(new ArrayList<>())
                //忽略表名
                .ignoreTableName(ignoreTableName)
                //忽略表前缀
                .ignoreTablePrefix(ignorePrefix)
                //忽略表后缀
                .ignoreTableSuffix(ignoreSuffix).build();
        return processConfig;
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137