forked from DillonPu/JavaCodeAutoGenerate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeGenerate.java
More file actions
231 lines (189 loc) · 8.12 KB
/
Copy pathCodeGenerate.java
File metadata and controls
231 lines (189 loc) · 8.12 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package com.al;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.*;
import java.util.*;
public class CodeGenerate {
private static Logger log = LoggerFactory.getLogger(CodeGenerate.class);
// 固定
private static String projectPath = System.getProperty("user.dir"); // 获取当前工程目录
private static PackageConfig pc = new PackageConfig();
// 自定义值
private static String author = "阿来小同学";
private static String ip = "localhost";
private static String port = "3306";
private static String dbname = "al"; // 数据库名
private static String url = "jdbc:mysql://localhost:3306/al?useUnicode=true&useSSL=false&characterEncoding=utf8";
private static String driver = "com.mysql.jdbc.Driver";
private static String username = "root";
private static String password = "root";
private static String[] dbTableNames = {}; // 无值:为数据库中所有表生成代码;有值 如:{"table1","table2"},只生成数组中的表
private static String javaPackageName = "com.test"; // 顶级包名
private static String javaModuleName = "codeServer"; // 模块名
private static String mapperPackageName = ""; // mapper文件所在包名
private static final String templatePath = "/templates/mapper.xml.ftl"; // 如果模板引擎是 freemarker
// private static final templatePath = "/templates/mapper.xml.vm"; //如果模板引擎是 velocity
static {
url = "jdbc:mysql://"
+ip+":"
+port+"/"
+dbname+"?useUnicode=true&useSSL=false&characterEncoding=utf8";
}
/**
* 读取控制台内容
* @param tip 提示内容
* @return
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
/**
* 获取数据库表及其字段
* @return
* @throws ClassNotFoundException
* @throws SQLException
*/
public static Map<String, Object> getDatabaseNameToTableNameAndColumnName() throws ClassNotFoundException, SQLException {
Map<String, Object> tableNameMap = new HashMap<String, Object>();
//加载驱动
Class.forName(driver);
//获得数据库连接
Connection connection = DriverManager.getConnection(url, username, password);
//获得元数据
DatabaseMetaData metaData = connection.getMetaData();
//获得表信息
ResultSet tables = metaData.getTables(null, null, null, new String[]{"TABLE"});
while (tables.next()) {
Map<String, String> columnNameMap = new HashMap<String, String>(); //保存字段名
//获得表名
String table_name = tables.getString("TABLE_NAME");
//通过表名获得所有字段名
ResultSet columns = metaData.getColumns(null, null, table_name, "%");
//获得所有字段名
while (columns.next()) {
//获得字段名
String column_name = columns.getString("COLUMN_NAME");
//获得字段类型
String type_name = columns.getString("TYPE_NAME");
columnNameMap.put(column_name, type_name);
}
tableNameMap.put(table_name, columnNameMap);
}
return tableNameMap;
}
/**
* 获取数据库所有表名
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
public static String[] getTableNames() throws SQLException, ClassNotFoundException {
Map<String, Object> map = getDatabaseNameToTableNameAndColumnName();
Set<String> tableNames = map.keySet();
log.info("数据库表名:{}",tableNames);
String[] names = new String[tableNames.size()];
names = tableNames.toArray(names);
return names;
}
/**
* 配置完毕,运行即可,生成mybatisx 代码
* @param args
* @throws SQLException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws SQLException, ClassNotFoundException {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor(author);
gc.setOpen(false);
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl(url);
dsc.setDriverName(driver);
dsc.setUsername(username);
dsc.setPassword(password);
mpg.setDataSource(dsc);
// 包配置
pc.setParent(javaPackageName);
pc.setModuleName(javaModuleName);
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<FileOutConfig>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mapper/" + mapperPackageName
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
/*
cfg.setFileCreate(new IFileCreate() {
@Override
public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
// 判断自定义文件夹是否需要创建
checkDir("调用默认方法创建的目录");
return false;
}
});
*/
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
// 配置自定义输出模板
//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
// templateConfig.setEntity("templates/entity2.java");
// templateConfig.setService();
// templateConfig.setController();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
// 公共父类
// 写于父类中的公共字段
strategy.setSuperEntityColumns("id");
String[] dbTableName = dbTableNames.length>0 ? dbTableNames : getTableNames();
strategy.setInclude(dbTableName);
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix(pc.getModuleName() + "_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}