1
+ import com .google .common .base .CaseFormat ;
1
2
import freemarker .template .TemplateExceptionHandler ;
3
+ import org .apache .commons .lang3 .StringUtils ;
2
4
import org .mybatis .generator .api .MyBatisGenerator ;
3
5
import org .mybatis .generator .config .*;
4
6
import org .mybatis .generator .internal .DefaultShellCallback ;
@@ -36,18 +38,34 @@ public class CodeGenerator {
36
38
37
39
public static void main (String [] args ) {
38
40
genCode ("输入表名" );
41
+ //genCode("输入表名","输入自定义Model名称");
39
42
}
40
43
44
+ /**
45
+ * 通过数据表名称生成代码,Model 名称通过解析数据表名称获得,下划线转大驼峰的形式。
46
+ * 如输入表名称 "t_user_detail" 将生成 TUserDetail、TUserDetailMapper、TUserDetailService ...
47
+ * @param tableNames 数据表名称...
48
+ */
41
49
public static void genCode (String ... tableNames ) {
42
50
for (String tableName : tableNames ) {
43
- //根据需求生成,不需要的注掉,模板有问题的话可以自己修改。
44
- genModelAndMapper (tableName );
45
- genService (tableName );
46
- genController (tableName );
51
+ genCode (tableName , null );
47
52
}
48
53
}
49
54
50
- public static void genModelAndMapper (String tableName ) {
55
+ /**
56
+ * 通过数据表名称,和自定义的 Model 名称生成代码
57
+ * 如输入表名称 "t_user_detail" 和自定义的 Model 名称 "User" 将生成 User、UserMapper、UserService ...
58
+ * @param tableName 数据表名称
59
+ * @param modelName 自定义的 Model 名称
60
+ */
61
+ public static void genCode (String tableName , String modelName ) {
62
+ genModelAndMapper (tableName , modelName );
63
+ genService (tableName , modelName );
64
+ genController (tableName , modelName );
65
+ }
66
+
67
+
68
+ public static void genModelAndMapper (String tableName , String modelName ) {
51
69
Context context = new Context (ModelType .FLAT );
52
70
context .setId ("Potato" );
53
71
context .setTargetRuntime ("MyBatis3Simple" );
@@ -84,6 +102,7 @@ public static void genModelAndMapper(String tableName) {
84
102
85
103
TableConfiguration tableConfiguration = new TableConfiguration (context );
86
104
tableConfiguration .setTableName (tableName );
105
+ tableConfiguration .setDomainObjectName (modelName );
87
106
tableConfiguration .setGeneratedKey (new GeneratedKey ("id" , "Mysql" , true , null ));
88
107
context .addTableConfiguration (tableConfiguration );
89
108
@@ -106,21 +125,20 @@ public static void genModelAndMapper(String tableName) {
106
125
if (generator .getGeneratedJavaFiles ().isEmpty () || generator .getGeneratedXmlFiles ().isEmpty ()) {
107
126
throw new RuntimeException ("生成Model和Mapper失败:" + warnings );
108
127
}
109
-
110
- String modelName = tableNameConvertUpperCamel (tableName );
128
+ if (StringUtils .isEmpty (modelName )) modelName = tableNameConvertUpperCamel (tableName );
111
129
System .out .println (modelName + ".java 生成成功" );
112
130
System .out .println (modelName + "Mapper.java 生成成功" );
113
131
System .out .println (modelName + "Mapper.xml 生成成功" );
114
132
}
115
133
116
- public static void genService (String tableName ) {
134
+ public static void genService (String tableName , String modelName ) {
117
135
try {
118
136
freemarker .template .Configuration cfg = getConfiguration ();
119
137
120
138
Map <String , Object > data = new HashMap <>();
121
139
data .put ("date" , DATE );
122
140
data .put ("author" , AUTHOR );
123
- String modelNameUpperCamel = tableNameConvertUpperCamel (tableName );
141
+ String modelNameUpperCamel = StringUtils . isEmpty ( modelName ) ? tableNameConvertUpperCamel (tableName ) : modelName ;
124
142
data .put ("modelNameUpperCamel" , modelNameUpperCamel );
125
143
data .put ("modelNameLowerCamel" , tableNameConvertLowerCamel (tableName ));
126
144
data .put ("basePackage" , BASE_PACKAGE );
@@ -145,17 +163,17 @@ public static void genService(String tableName) {
145
163
}
146
164
}
147
165
148
- public static void genController (String tableName ) {
166
+ public static void genController (String tableName , String modelName ) {
149
167
try {
150
168
freemarker .template .Configuration cfg = getConfiguration ();
151
169
152
170
Map <String , Object > data = new HashMap <>();
153
171
data .put ("date" , DATE );
154
172
data .put ("author" , AUTHOR );
155
- data . put ( "baseRequestMapping" , tableNameConvertMappingPath (tableName )) ;
156
- String modelNameUpperCamel = tableNameConvertUpperCamel ( tableName );
173
+ String modelNameUpperCamel = StringUtils . isEmpty ( modelName ) ? tableNameConvertUpperCamel (tableName ) : modelName ;
174
+ data . put ( "baseRequestMapping" , modelNameConvertMappingPath ( modelNameUpperCamel ) );
157
175
data .put ("modelNameUpperCamel" , modelNameUpperCamel );
158
- data .put ("modelNameLowerCamel" , tableNameConvertLowerCamel ( tableName ));
176
+ data .put ("modelNameLowerCamel" , CaseFormat . UPPER_CAMEL . to ( CaseFormat . LOWER_CAMEL , modelNameUpperCamel ));
159
177
data .put ("basePackage" , BASE_PACKAGE );
160
178
161
179
File file = new File (PROJECT_PATH + JAVA_PATH + PACKAGE_PATH_CONTROLLER + modelNameUpperCamel + "Controller.java" );
@@ -181,30 +199,11 @@ private static freemarker.template.Configuration getConfiguration() throws IOExc
181
199
}
182
200
183
201
private static String tableNameConvertLowerCamel (String tableName ) {
184
- StringBuilder result = new StringBuilder ();
185
- if (tableName != null && tableName .length () > 0 ) {
186
- tableName = tableName .toLowerCase ();//兼容使用大写的表名
187
- boolean flag = false ;
188
- for (int i = 0 ; i < tableName .length (); i ++) {
189
- char ch = tableName .charAt (i );
190
- if ("_" .charAt (0 ) == ch ) {
191
- flag = true ;
192
- } else {
193
- if (flag ) {
194
- result .append (Character .toUpperCase (ch ));
195
- flag = false ;
196
- } else {
197
- result .append (ch );
198
- }
199
- }
200
- }
201
- }
202
- return result .toString ();
202
+ return CaseFormat .LOWER_UNDERSCORE .to (CaseFormat .LOWER_CAMEL , tableName .toLowerCase ());
203
203
}
204
204
205
205
private static String tableNameConvertUpperCamel (String tableName ) {
206
- String camel = tableNameConvertLowerCamel (tableName );
207
- return camel .substring (0 , 1 ).toUpperCase () + camel .substring (1 );
206
+ return CaseFormat .LOWER_UNDERSCORE .to (CaseFormat .UPPER_CAMEL , tableName .toLowerCase ());
208
207
209
208
}
210
209
@@ -213,7 +212,13 @@ private static String tableNameConvertMappingPath(String tableName) {
213
212
return "/" + (tableName .contains ("_" ) ? tableName .replaceAll ("_" , "/" ) : tableName );
214
213
}
215
214
215
+ private static String modelNameConvertMappingPath (String modelName ) {
216
+ String tableName = CaseFormat .UPPER_CAMEL .to (CaseFormat .LOWER_UNDERSCORE , modelName );
217
+ return tableNameConvertMappingPath (tableName );
218
+ }
219
+
216
220
private static String packageConvertPath (String packageName ) {
217
221
return String .format ("/%s/" , packageName .contains ("." ) ? packageName .replaceAll ("\\ ." , "/" ) : packageName );
218
222
}
223
+
219
224
}
0 commit comments