Skip to content

Commit f80070d

Browse files
committed
添加输入表名生成代码功能
1 parent 1c01acf commit f80070d

File tree

7 files changed

+265
-97
lines changed

7 files changed

+265
-97
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,13 @@ SpringBoot + Mybatis + PageHelper + 通用Mapper + Thymeleaf(Amaze UI(布局) +
5353
```
5454
![image text](https://docs.wang64.cn/Project/ViewGenerator/image/20190406003.png)
5555
```
56-
配置更新界面如下
56+
输入表名生成代码界面如下
5757
```
5858
![image text](https://docs.wang64.cn/Project/ViewGenerator/image/20190406004.png)
59+
```
60+
配置更新界面如下
61+
```
62+
![image text](https://docs.wang64.cn/Project/ViewGenerator/image/20190406005.png)
5963

6064
#### 搭建参考
6165

src/main/java/com/example/constant/Constant.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ public interface Constant {
1717
*/
1818
String COLON = ":";
1919

20+
/**
21+
* 模板
22+
*/
23+
String TEMPLATE = "template";
24+
2025
}

src/main/java/com/example/controller/DataBaseController.java

Lines changed: 82 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.generator.CustomGeneratorFacade;
1717
import org.apache.commons.io.FileUtils;
1818
import org.apache.commons.io.IOUtils;
19+
import org.apache.commons.lang3.StringUtils;
1920
import org.springframework.beans.factory.annotation.Value;
2021
import org.springframework.web.bind.annotation.*;
2122
import 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
}

src/main/resources/templates/common/common.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
<link rel="stylesheet" th:href="@{css/component.css}"/>
1616
<link rel="shortcut icon" th:href="@{/favicon.ico}" type="image/x-icon"/>
1717
<style>
18+
/* elementUI的确认弹出框时页面右侧缩小5px的解决方法 */
19+
body {
20+
padding-right:0 !important;
21+
}
22+
23+
/* elementUI的Table表头错位的解决方法 */
1824
body .el-table th.gutter {
1925
display: table-cell!important;
2026
}
@@ -94,7 +100,7 @@ <h5><a href="https://wang64.cn" target="_blank" title="每天看着励志的语
94100
<ul class="am-list admin-sidebar-list">
95101

96102
<li><a href="index.shtml"><i class="el-icon-news"></i>&nbsp;&nbsp;<b>首页</b></a></li>
97-
<li><a href="table.shtml"><i class="el-icon-printer"></i>&nbsp;&nbsp;<b>代码生成</b></a></li>
103+
<li><a href="table.shtml"><i class="el-icon-printer"></i>&nbsp;&nbsp;<b>生成代码</b></a></li>
98104
<li><a href="config.shtml"><i class="el-icon-edit-outline"></i>&nbsp;&nbsp;<b>配置更新</b></a></li>
99105

100106
<!--<li class="admin-parent">

src/main/resources/templates/config.html

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -216,24 +216,34 @@ <h5 class="header-title m-t-0 m-b-30">说明</h5>
216216
this.$refs.generator.validate((valid) => {
217217
// 加载显示
218218
this.submitLoading = true;
219-
console.log(valid)
219+
// console.log(valid)
220220
if (valid) {
221-
this.$notify({
222-
dangerouslyUseHTMLString: true,
223-
title: '请等待',
224-
message: '配置更新会导致应用热重启'
225-
});
226221
this.generator.isRead = false;
227222
axios.put('/database/config', this.generator).then(res => {
228223
var data = res.data;
229-
this.generator = data.data;
230-
setTimeout(() => {
231-
this.submitLoading = false;
232-
this.$message({
233-
message: data.msg,
234-
type: 'success'
224+
if (data.code === 200) {
225+
this.$notify({
226+
dangerouslyUseHTMLString: true,
227+
title: '请等待',
228+
message: '配置更新会导致应用热重启'
235229
});
236-
}, 3033);
230+
this.generator = data.data;
231+
setTimeout(() => {
232+
this.submitLoading = false;
233+
this.$message({
234+
message: data.msg,
235+
type: 'success'
236+
});
237+
}, 3033);
238+
} else {
239+
this.$notify({
240+
dangerouslyUseHTMLString: true,
241+
title: '提示',
242+
message: data.msg
243+
});
244+
this.$message.error(data.msg);
245+
this.submitLoading = false;
246+
}
237247
}).catch(err => {
238248
console.log(err.response);
239249
this.submitLoading = false;

0 commit comments

Comments
 (0)