|
5 | 5 | */ |
6 | 6 | package com.example.controller; |
7 | 7 |
|
| 8 | +import cn.org.rapid_framework.generator.Generator; |
| 9 | +import cn.org.rapid_framework.generator.GeneratorFacade; |
| 10 | +import cn.org.rapid_framework.generator.GeneratorProperties; |
| 11 | +import com.example.constant.Constant; |
8 | 12 | import com.example.dao.GeneratorDao; |
9 | 13 | import com.example.exception.SystemException; |
10 | 14 | import com.example.util.SafeProperties; |
11 | 15 | import com.example.util.ZipUtil; |
12 | | -import com.uframe.generator.GeneratorFacade; |
| 16 | +import com.generator.CustomGeneratorFacade; |
13 | 17 | import org.apache.commons.io.FileUtils; |
14 | 18 | import org.apache.commons.io.IOUtils; |
15 | 19 | import org.springframework.beans.factory.annotation.Value; |
|
38 | 42 | @RequestMapping("database") |
39 | 43 | public class DataBaseController { |
40 | 44 |
|
41 | | - /** |
42 | | - * 项目在硬盘上的基础路径 |
43 | | - */ |
44 | | - private static final String PROJECT_PATH = System.getProperty("user.dir"); |
45 | | - |
46 | 45 | /** |
47 | 46 | * 代码生成的文件输出路径 |
48 | 47 | */ |
@@ -134,10 +133,16 @@ public ResponseBean open() { |
134 | 133 | * @date 2019-04-05 18:00:26 |
135 | 134 | */ |
136 | 135 | @PostMapping("/{tableName}") |
137 | | - public ResponseBean genTable(@PathVariable("tableName") String tableName) { |
| 136 | + public ResponseBean genTable(@PathVariable("tableName") String tableName) throws IOException { |
138 | 137 | // 配置表名 |
139 | 138 | String[] tableNames = tableName.split("-"); |
140 | | - if (genCode(tableNames, null)) { |
| 139 | + if (outRoot == null) { |
| 140 | + throw new IllegalArgumentException("outRootDir must be not null"); |
| 141 | + } else if (outRoot.indexOf(Constant.COLON) < 0) { |
| 142 | + // 项目在硬盘上的基础路径 |
| 143 | + outRoot = System.getProperty("user.dir") + outRoot; |
| 144 | + } |
| 145 | + if (genCode(tableNames, outRoot)) { |
141 | 146 | System.out.println("----- 生成成功 -----"); |
142 | 147 | // 获得系统属性集 |
143 | 148 | Properties props = System.getProperties(); |
@@ -199,24 +204,20 @@ public void genTableToZip(@PathVariable("tableName") String tableName, |
199 | 204 | * 通过表名称生成代码 |
200 | 205 | * @param tableNames |
201 | 206 | */ |
202 | | - public boolean genCode(String[] tableNames, String outRootDir) { |
203 | | - // GeneratorFacade |
204 | | - GeneratorFacade generatorFacade = null; |
205 | | - if (outRootDir == null) { |
206 | | - generatorFacade = new GeneratorFacade(); |
207 | | - } else { |
208 | | - generatorFacade = new GeneratorFacade(outRootDir); |
209 | | - } |
| 207 | + public boolean genCode(String[] tableNames, String outRootDir) throws IOException { |
| 208 | + GeneratorFacade generatorFacade = new CustomGeneratorFacade(outRootDir); |
| 209 | + // 配置信息 |
| 210 | + GeneratorProperties.load(new String[]{ "classpath:config/generator.properties" }); |
| 211 | + // 模板位置 |
| 212 | + Generator generator = generatorFacade.getGenerator(); |
| 213 | + generator.addTemplateRootDir(Constant.PROJECT_PATH + template); |
210 | 214 | // 开始执行 |
211 | 215 | try { |
212 | 216 | for (String tableName : tableNames) { |
213 | | - // 有设置输出路径说明为临时路径,不需要旧删除文件 |
214 | | - if (outRootDir == null) { |
215 | | - // 删除旧文件 |
216 | | - generatorFacade.deleteByTable(tableName, PROJECT_PATH + template); |
217 | | - } |
| 217 | + // 删除旧文件 |
| 218 | + generatorFacade.deleteByTable(tableName); |
218 | 219 | // 生成新文件 |
219 | | - generatorFacade.generateByTable(tableName, PROJECT_PATH + template); |
| 220 | + generatorFacade.generateByTable(tableName); |
220 | 221 | } |
221 | 222 | } catch (Exception e) { |
222 | 223 | System.out.println("----- 生成失败 请检查数据库是否连接正常及表名是否正确以及权限是否缺失 -----"); |
|
0 commit comments