1616import com .generator .CustomGeneratorFacade ;
1717import org .apache .commons .io .FileUtils ;
1818import org .apache .commons .io .IOUtils ;
19+ import org .apache .commons .lang3 .StringUtils ;
1920import org .springframework .beans .factory .annotation .Value ;
2021import org .springframework .web .bind .annotation .*;
2122import org .springframework .beans .factory .annotation .Autowired ;
@@ -66,7 +67,7 @@ public DataBaseController(GeneratorDao generatorDao) {
6667
6768 /**
6869 * 列表
69- * @author Generator
70+ * @author Wang926454
7071 * @date 2019-04-05 18:00:26
7172 */
7273 @ GetMapping
@@ -81,7 +82,7 @@ public ResponseBean list(@RequestParam(defaultValue = "1") Integer page,
8182 map .put ("tableName" , tableName );
8283 PageHelper .startPage (page , rows );
8384 List <Map <String , Object >> list = generatorDao .queryList (map );
84- if (list == null || list . size () <= 0 ) {
85+ if (list == null ) {
8586 throw new CustomException ("查询失败(Query Failure)" );
8687 }
8788 PageInfo <Map <String , Object >> pageInfo = new PageInfo <Map <String , Object >>(list );
@@ -91,9 +92,29 @@ public ResponseBean list(@RequestParam(defaultValue = "1") Integer page,
9192 return new ResponseBean (HttpStatus .OK .value (), "查询成功" , result );
9293 }
9394
95+ /**
96+ * 获取所有表名
97+ * @author Wang926454
98+ * @date 2019-04-08 16:00:26
99+ */
100+ @ GetMapping ("/tableNames/all" )
101+ public ResponseBean all () {
102+ // 获取所有表名
103+ List <Map <String , Object >> list = generatorDao .queryList (null );
104+ for (Map <String , Object > map : list ) {
105+ if (StringUtils .isNoneBlank (map .get ("tableComment" ).toString ())) {
106+ map .put ("label" , map .get ("tableName" ) + "---" + map .get ("tableComment" ) + "---" + map .get ("engine" ));
107+ } else {
108+ map .put ("label" , map .get ("tableName" ) + "---" + map .get ("engine" ));
109+ }
110+ map .put ("value" , map .get ("tableName" ));
111+ }
112+ return new ResponseBean (HttpStatus .OK .value (), "查询成功" , list );
113+ }
114+
94115 /**
95116 * 表详细字段信息
96- * @author Generator
117+ * @author Wang926454
97118 * @date 2019-04-05 18:00:26
98119 */
99120 @ GetMapping ("/{tableName}" )
@@ -105,31 +126,9 @@ public ResponseBean detail(@PathVariable("tableName") String tableName) {
105126 return new ResponseBean (HttpStatus .OK .value (), "查询成功" , columns );
106127 }
107128
108- /**
109- * 打开Windows的代码输出文件夹
110- * @author Generator
111- * @date 2019-04-05 18:00:26
112- */
113- @ GetMapping ("/open" )
114- public ResponseBean open () {
115- try {
116- // 有:应该是全路径
117- String [] array = outRoot .split (":" );
118- if (array .length - 1 >= 0 ) {
119- Runtime .getRuntime ().exec ("cmd.exe /c start " + outRoot );
120- } else {
121- // 项目在硬盘上的基础路径
122- Runtime .getRuntime ().exec ("cmd.exe /c start " + System .getProperty ("user.dir" ) + outRoot );
123- }
124- } catch (IOException e ) {
125- throw new SystemException ("操作失败" );
126- }
127- return new ResponseBean (HttpStatus .OK .value (), "操作成功" , null );
128- }
129-
130129 /**
131130 * 生成代码到输出路径
132- * @author Generator
131+ * @author Wang926454
133132 * @date 2019-04-05 18:00:26
134133 */
135134 @ PostMapping ("/{tableName}" )
@@ -164,7 +163,7 @@ public ResponseBean genTable(@PathVariable("tableName") String tableName) throws
164163
165164 /**
166165 * 生成代码为Zip文件下载
167- * @author Generator
166+ * @author Wang926454
168167 * @date 2019-04-05 18:00:26
169168 */
170169 @ GetMapping ("/zip/{tableName}" )
@@ -201,35 +200,30 @@ public void genTableToZip(@PathVariable("tableName") String tableName,
201200 }
202201
203202 /**
204- * 通过表名称生成代码
205- * @param tableNames
203+ * 打开Windows系统的代码输出文件夹
204+ * @author Wang926454
205+ * @date 2019-04-05 18:00:26
206206 */
207- public boolean genCode (String [] tableNames , String outRoot ) throws IOException {
208- GeneratorFacade generatorFacade = new CustomGeneratorFacade (outRoot );
209- // 配置信息
210- GeneratorProperties .load (new String []{ "classpath:config/generator.properties" });
211- // 模板位置
212- Generator generator = generatorFacade .getGenerator ();
213- generator .addTemplateRootDir (Constant .PROJECT_PATH + template );
214- // 开始执行
207+ @ GetMapping ("/open" )
208+ public ResponseBean open () {
215209 try {
216- for (String tableName : tableNames ) {
217- // 删除旧文件
218- generatorFacade .deleteByTable (tableName );
219- // 生成新文件
220- generatorFacade .generateByTable (tableName );
210+ // 有:应该是全路径
211+ String [] array = outRoot .split (":" );
212+ if (array .length - 1 >= 0 ) {
213+ Runtime .getRuntime ().exec ("cmd.exe /c start " + outRoot );
214+ } else {
215+ // 项目在硬盘上的基础路径
216+ Runtime .getRuntime ().exec ("cmd.exe /c start " + System .getProperty ("user.dir" ) + outRoot );
221217 }
222- } catch (Exception e ) {
223- System .out .println ("----- 生成失败 请检查数据库是否连接正常及表名是否正确以及权限是否缺失 -----" );
224- e .printStackTrace ();
225- return false ;
218+ } catch (IOException e ) {
219+ throw new SystemException ("操作失败" );
226220 }
227- return true ;
221+ return new ResponseBean ( HttpStatus . OK . value (), "操作成功" , null ) ;
228222 }
229223
230224 /**
231225 * 读取更新配置文件generator.properties
232- * @author Generator
226+ * @author Wang926454
233227 * @date 2019-04-05 18:00:26
234228 */
235229 @ PutMapping ("/config" )
@@ -256,7 +250,14 @@ public ResponseBean config(@RequestBody Map<String, String> map) {
256250 Iterator <Map .Entry <String , String >> entries = map .entrySet ().iterator ();
257251 while (entries .hasNext ()) {
258252 Map .Entry <String , String > entry = entries .next ();
259- System .out .println ("Key = " + entry .getKey () + ", Value = " + entry .getValue ());
253+ if (Constant .TEMPLATE .equals (entry .getKey ())) {
254+ // 查看配置路径下macro.includ文件是否存在
255+ File file = new File (Constant .PROJECT_PATH + entry .getValue () + "/macro.include" );
256+ if (!file .exists ()) {
257+ // 模板代码位置不存在
258+ return new ResponseBean (HttpStatus .BAD_REQUEST .value (), "当前填写的模板代码位置不存在" , null );
259+ }
260+ }
260261 safeProperties .setProperty (entry .getKey (), entry .getValue ());
261262 }
262263 output = new FileOutputStream (url .getPath ());
@@ -279,4 +280,36 @@ public ResponseBean config(@RequestBody Map<String, String> map) {
279280 return new ResponseBean (HttpStatus .OK .value (), "操作成功" , map );
280281 }
281282
283+ /**
284+ * 通过表名生成代码
285+ * @param tableNames 表名数组
286+ * @param outRoot 代码输出文件夹
287+ * @throws IOException
288+ * @return boolean
289+ * @author Wang926454
290+ * @date 2019/4/8 17:19
291+ */
292+ public boolean genCode (String [] tableNames , String outRoot ) throws IOException {
293+ GeneratorFacade generatorFacade = new CustomGeneratorFacade (outRoot );
294+ // 配置信息
295+ GeneratorProperties .load (new String []{ "classpath:config/generator.properties" });
296+ // 模板位置
297+ Generator generator = generatorFacade .getGenerator ();
298+ generator .addTemplateRootDir (Constant .PROJECT_PATH + template );
299+ // 开始执行
300+ try {
301+ for (String tableName : tableNames ) {
302+ // 删除旧文件
303+ generatorFacade .deleteByTable (tableName );
304+ // 生成新文件
305+ generatorFacade .generateByTable (tableName );
306+ }
307+ } catch (Exception e ) {
308+ System .out .println ("----- 生成失败 请检查数据库是否连接正常及表名是否正确以及权限是否缺失 -----" );
309+ e .printStackTrace ();
310+ return false ;
311+ }
312+ return true ;
313+ }
314+
282315}
0 commit comments