CodeGenerator.java
11.4 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
//import com.google.common.base.CaseFormat;
//import freemarker.template.TemplateExceptionHandler;
//import org.apache.commons.lang3.StringUtils;
//import org.mybatis.generator.api.MyBatisGenerator;
//import org.mybatis.generator.config.*;
//import org.mybatis.generator.internal.DefaultShellCallback;
//
//import java.io.File;
//import java.io.FileWriter;
//import java.io.IOException;
//import java.text.SimpleDateFormat;
//import java.util.*;
//
///**
// * 代码生成器,根据数据表名称生成对应的Model、Mapper、Service、Controller简化开发。
// */
//public class CodeGenerator {
// //JDBC配置,请修改为你项目的实际配置
// private static final String JDBC_URL = "jdbc:mysql://124.71.186.146:3316/t_test";
// private static final String JDBC_USERNAME = "weishuangshuang";
// private static final String JDBC_PASSWORD = "weishuangshuang2020PipiTest";
// private static final String JDBC_DIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
//
// private static final String PROJECT_PATH = System.getProperty("user.dir");//项目在硬盘上的基础路径
// private static final String TEMPLATE_FILE_PATH = PROJECT_PATH + "/src/test/resources/generator/template";//模板位置
//
// private static final String JAVA_PATH = "/src/main/java"; //java文件路径
// private static final String RESOURCES_PATH = "/src/main/resources";//资源文件路径
//
//// private static final String PACKAGE_PATH_SERVICE = packageConvertPath(SERVICE_PACKAGE);//生成的Service存放路径
//// private static final String PACKAGE_PATH_SERVICE_IMPL = packageConvertPath(SERVICE_IMPL_PACKAGE);//生成的Service实现存放路径
//// private static final String PACKAGE_PATH_CONTROLLER = packageConvertPath(CONTROLLER_PACKAGE);//生成的Controller存放路径
//
// private static final String AUTHOR = "CodeGenerator";//@author
// private static final String DATE = new SimpleDateFormat("yyyy/MM/dd").format(new Date());//@date
//
// public static void main(String[] args) {
// genCode("t_feishu_customer_feedback");
// //genCodeByCustomModelName("输入表名","输入自定义Model名称");
// }
//
// /**
// * 通过数据表名称生成代码,Model 名称通过解析数据表名称获得,下划线转大驼峰的形式。
// * 如输入表名称 "t_user_detail" 将生成 TUserDetail、TUserDetailMapper、TUserDetailService ...
// * @param tableNames 数据表名称...
// */
// public static void genCode(String... tableNames) {
// for (String tableName : tableNames) {
// genCodeByCustomModelName(tableName, null);
// }
// }
//
// /**
// * 通过数据表名称,和自定义的 Model 名称生成代码
// * 如输入表名称 "t_user_detail" 和自定义的 Model 名称 "User" 将生成 User、UserMapper、UserService ...
// * @param tableName 数据表名称
// * @param modelName 自定义的 Model 名称
// */
// public static void genCodeByCustomModelName(String tableName, String modelName) {
// genModelAndMapper(tableName, modelName);
// genService(tableName, modelName);
// genController(tableName, modelName);
// }
//
//
// public static void genModelAndMapper(String tableName, String modelName) {
// Context context = new Context(ModelType.FLAT);
// context.setId("Potato");
// context.setTargetRuntime("MyBatis3Simple");
// context.addProperty(PropertyRegistry.CONTEXT_BEGINNING_DELIMITER, "`");
// context.addProperty(PropertyRegistry.CONTEXT_ENDING_DELIMITER, "`");
//
// JDBCConnectionConfiguration jdbcConnectionConfiguration = new JDBCConnectionConfiguration();
// jdbcConnectionConfiguration.setConnectionURL(JDBC_URL);
// jdbcConnectionConfiguration.setUserId(JDBC_USERNAME);
// jdbcConnectionConfiguration.setPassword(JDBC_PASSWORD);
// jdbcConnectionConfiguration.setDriverClass(JDBC_DIVER_CLASS_NAME);
// context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration);
//
// PluginConfiguration pluginConfiguration = new PluginConfiguration();
// pluginConfiguration.setConfigurationType("tk.mybatis.mapper.generator.MapperPlugin");
// pluginConfiguration.addProperty("mappers", MAPPER_INTERFACE_REFERENCE);
// context.addPluginConfiguration(pluginConfiguration);
//
// JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
// javaModelGeneratorConfiguration.setTargetProject(PROJECT_PATH + JAVA_PATH);
// javaModelGeneratorConfiguration.setTargetPackage(MODEL_PACKAGE);
// context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration);
//
// SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = new SqlMapGeneratorConfiguration();
// sqlMapGeneratorConfiguration.setTargetProject(PROJECT_PATH + RESOURCES_PATH);
// sqlMapGeneratorConfiguration.setTargetPackage("mapper");
// context.setSqlMapGeneratorConfiguration(sqlMapGeneratorConfiguration);
//
// JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration();
// javaClientGeneratorConfiguration.setTargetProject(PROJECT_PATH + JAVA_PATH);
// javaClientGeneratorConfiguration.setTargetPackage(MAPPER_PACKAGE);
// javaClientGeneratorConfiguration.setConfigurationType("XMLMAPPER");
// context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfiguration);
//
// TableConfiguration tableConfiguration = new TableConfiguration(context);
// tableConfiguration.setTableName(tableName);
// if (StringUtils.isNotEmpty(modelName))tableConfiguration.setDomainObjectName(modelName);
// tableConfiguration.setGeneratedKey(new GeneratedKey("id", "Mysql", true, null));
// context.addTableConfiguration(tableConfiguration);
//
// List<String> warnings;
// MyBatisGenerator generator;
// try {
// Configuration config = new Configuration();
// config.addContext(context);
// config.validate();
//
// boolean overwrite = true;
// DefaultShellCallback callback = new DefaultShellCallback(overwrite);
// warnings = new ArrayList<String>();
// generator = new MyBatisGenerator(config, callback, warnings);
// generator.generate(null);
// } catch (Exception e) {
// throw new RuntimeException("生成Model和Mapper失败", e);
// }
//
// if (generator.getGeneratedJavaFiles().isEmpty() || generator.getGeneratedXmlFiles().isEmpty()) {
// throw new RuntimeException("生成Model和Mapper失败:" + warnings);
// }
// if (StringUtils.isEmpty(modelName)) modelName = tableNameConvertUpperCamel(tableName);
// System.out.println(modelName + ".java 生成成功");
// System.out.println(modelName + "Mapper.java 生成成功");
// System.out.println(modelName + "Mapper.xml 生成成功");
// }
//
// public static void genService(String tableName, String modelName) {
// try {
// freemarker.template.Configuration cfg = getConfiguration();
//
// Map<String, Object> data = new HashMap<>();
// data.put("date", DATE);
// data.put("author", AUTHOR);
// String modelNameUpperCamel = StringUtils.isEmpty(modelName) ? tableNameConvertUpperCamel(tableName) : modelName;
// data.put("modelNameUpperCamel", modelNameUpperCamel);
// data.put("modelNameLowerCamel", tableNameConvertLowerCamel(tableName));
// data.put("basePackage", BASE_PACKAGE);
//
// File file = new File(PROJECT_PATH + JAVA_PATH + PACKAGE_PATH_SERVICE + modelNameUpperCamel + "Service.java");
// if (!file.getParentFile().exists()) {
// file.getParentFile().mkdirs();
// }
// cfg.getTemplate("service.ftl").process(data,
// new FileWriter(file));
// System.out.println(modelNameUpperCamel + "Service.java 生成成功");
//
// File file1 = new File(PROJECT_PATH + JAVA_PATH + PACKAGE_PATH_SERVICE_IMPL + modelNameUpperCamel + "ServiceImpl.java");
// if (!file1.getParentFile().exists()) {
// file1.getParentFile().mkdirs();
// }
// cfg.getTemplate("service-impl.ftl").process(data,
// new FileWriter(file1));
// System.out.println(modelNameUpperCamel + "ServiceImpl.java 生成成功");
// } catch (Exception e) {
// throw new RuntimeException("生成Service失败", e);
// }
// }
//
// public static void genController(String tableName, String modelName) {
// try {
// freemarker.template.Configuration cfg = getConfiguration();
//
// Map<String, Object> data = new HashMap<>();
// data.put("date", DATE);
// data.put("author", AUTHOR);
// String modelNameUpperCamel = StringUtils.isEmpty(modelName) ? tableNameConvertUpperCamel(tableName) : modelName;
// data.put("baseRequestMapping", modelNameConvertMappingPath(modelNameUpperCamel));
// data.put("modelNameUpperCamel", modelNameUpperCamel);
// data.put("modelNameLowerCamel", CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, modelNameUpperCamel));
// data.put("basePackage", BASE_PACKAGE);
//
// File file = new File(PROJECT_PATH + JAVA_PATH + PACKAGE_PATH_CONTROLLER + modelNameUpperCamel + "Controller.java");
// if (!file.getParentFile().exists()) {
// file.getParentFile().mkdirs();
// }
// //cfg.getTemplate("controller-restful.ftl").process(data, new FileWriter(file));
// cfg.getTemplate("controller.ftl").process(data, new FileWriter(file));
//
// System.out.println(modelNameUpperCamel + "Controller.java 生成成功");
// } catch (Exception e) {
// throw new RuntimeException("生成Controller失败", e);
// }
//
// }
//
// private static freemarker.template.Configuration getConfiguration() throws IOException {
// freemarker.template.Configuration cfg = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_23);
// cfg.setDirectoryForTemplateLoading(new File(TEMPLATE_FILE_PATH));
// cfg.setDefaultEncoding("UTF-8");
// cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
// return cfg;
// }
//
// private static String tableNameConvertLowerCamel(String tableName) {
// return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, tableName.toLowerCase());
// }
//
// private static String tableNameConvertUpperCamel(String tableName) {
// return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, tableName.toLowerCase());
//
// }
//
// private static String tableNameConvertMappingPath(String tableName) {
// tableName = tableName.toLowerCase();//兼容使用大写的表名
// return "/" + (tableName.contains("_") ? tableName.replaceAll("_", "/") : tableName);
// }
//
// private static String modelNameConvertMappingPath(String modelName) {
// String tableName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, modelName);
// return tableNameConvertMappingPath(tableName);
// }
//
// private static String packageConvertPath(String packageName) {
// return String.format("/%s/", packageName.contains(".") ? packageName.replaceAll("\\.", "/") : packageName);
// }
//
//}