duhuizhe
2024-04-19 64c4fd3c7067c7626dc960a70b4bc2c3662bc653
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.yqzx.generator.service;
 
import cn.hutool.core.util.StrUtil;
import com.yqzx.generator.config.DataSourceConfig;
import com.yqzx.generator.action.model.DbColumnType;
import com.yqzx.generator.action.model.Table;
import com.yqzx.generator.action.model.TableInfo;
import com.yqzx.generator.action.model.Template;
import lombok.extern.slf4j.Slf4j;
 
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
 
/**
 * @description: 表工具类
 * @author: chaoyapeng
 * @time: 2020/8/18 16:13
 */
@Slf4j
public class GeneratorService {
 
    private DataSourceConfig dataSourceConfig;
 
    public GeneratorService setDataSourceConfig(DataSourceConfig dataSourceConfig) {
        this.dataSourceConfig = dataSourceConfig;
        return this;
    }
 
    public GeneratorService setDataSourceConfig(String driverName, String username, String password, String url) {
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDriverName(driverName);
        dataSourceConfig.setUsername(username);
        dataSourceConfig.setPassword(password);
        dataSourceConfig.setUrl(url);
        this.dataSourceConfig = dataSourceConfig;
        return this;
    }
 
    /**
     * 查询数据库表信息
     *
     * @param schema
     * @return
     */
    public List<Table> getTable(String schema) {
        {
            // 获取数据库连接,查询表结构信息
            Connection conn = dataSourceConfig.getConn();
            Statement sm = null;
            List<Table> tables = new ArrayList<>();
            try {
                sm = conn.createStatement();
                StringBuffer sql = new StringBuffer();
                sql.append("select table_name as tablename,table_comment as tablecomment from information_schema.`tables` where table_schema = '");
                sql.append(schema);
                sql.append("'");
                /*String sql = "select table_name as tablename,table_comment as tablecomment from information_schema.`tables` where table_schema = '"
                        + schema + "'";*/
                ResultSet rs = sm.executeQuery(sql.toString());
                while (rs.next()) {
                    Table table = new Table();
                    table.setName(rs.getString(1));
                    table.setComment(rs.getString(2));
                    table.setLabel(table.getName() + "-" + table.getComment());
                    tables.add(table);
                }
            } catch (SQLException e) {
                log.error(e.getMessage());
            }
            if (sm != null) {
                try {
                    sm.close();
                } catch (SQLException e) {
                    log.error(e.getMessage());
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    log.error(e.getMessage());
                }
            }
            return tables;
        }
    }
 
    /**
     * 查询表结构信息
     *
     * @param tableName
     * @return
     */
    public List<TableInfo> getTableInfo(String tableName) {
        Connection conn = dataSourceConfig.getConn();
        Statement sm = null;
        List<TableInfo> tableInfos = new ArrayList<>();
        try {
            sm = conn.createStatement();
            StringBuffer sql = new StringBuffer();
            sql.append("select column_name, data_type, column_comment from information_schema.columns where table_schema = (select database()) and table_name = '");
            sql.append(tableName);
            sql.append("'");
            /*String sql = "select column_name, data_type, column_comment from information_schema.columns where table_schema = (select database()) and table_name = '"
                    + tableName + "'";*/
            ResultSet rs = sm.executeQuery(sql.toString());
            while (rs.next()) {
                TableInfo tableInfo = new TableInfo();
                tableInfo.setName(rs.getString(1));
                DbColumnType column = DbColumnType.get(rs.getString(2));
                tableInfo.setType(column.getType());
                tableInfo.setJdbcType(column.getJdbcType());
                tableInfo.setComment(rs.getString(3));
                tableInfo.setPropertyName(StrUtil.toCamelCase(tableInfo.getName()));
                tableInfos.add(tableInfo);
            }
            if (tableInfos.size() > 0) {
                TableInfo t = tableInfos.get(tableInfos.size() - 1);
                tableInfos.remove(tableInfos.size() - 1);
                t.setLast(true);
                tableInfos.add(t);
            }
        } catch (SQLException e) {
            log.error(e.getMessage());
        }
        if (sm != null) {
            try {
                sm.close();
            } catch (SQLException e) {
                log.error(e.getMessage());
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                log.error(e.getMessage());
            }
        }
        return tableInfos;
    }
 
    /**
     * 获取模板内容
     *
     * @return
     */
    public List<Template> getTemplate() {
        List<Template> templates = new ArrayList<>();
        templates.add(create("controllerSwitch", "controller-模板"));
        templates.add(create("serviceSwitch", "service-模板(包含Impl)"));
        templates.add(create("mapperSwitch", "mapper-模板(包含xml和继承xml)"));
        templates.add(create("modelSwitch", "model-模板"));
        templates.add(create("querySwitch", "query-模板(列表查询参数,默认全部model内容)"));
        return templates;
    }
 
    private static Template create(String name, String comment) {
        Template template = new Template();
        template.setName(name);
        template.setComment(comment);
        return template;
    }
 
}