From 8cfb82659bad1fd23ee19b047ebe1821789962e7 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Wed, 10 Feb 2021 17:45:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E9=A2=84=E8=A7=88=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../generator/controller/GenController.java | 34 +-- .../service/GenTableServiceImpl.java | 200 ------------------ .../ruoyi/generator/util/VelocityUtils.java | 9 - ruoyi-ui/src/api/tool/codegen.js | 17 ++ ruoyi-ui/src/api/tool/gen.js | 8 - ruoyi-ui/src/main.js | 13 +- ruoyi-ui/src/utils/ruoyi.js | 15 ++ ruoyi-ui/src/views/tool/gen/index.vue | 161 ++++---------- .../codegen/ToolCodegenController.java | 78 ++++++- .../codegen/vo/ToolCodegenPreviewRespVO.java | 17 ++ .../convert/codegen/ToolCodegenConvert.java | 14 +- .../service/codegen/ToolCodegenService.java | 9 + .../codegen/impl/ToolCodegenEngine.java | 7 +- .../codegen/impl/ToolCodegenServiceImpl.java | 17 ++ .../codegen/java/controller/controller.vm | 8 +- .../resources/codegen/java/convert/convert.vm | 2 +- .../resources/codegen/java/service/service.vm | 2 +- .../codegen/java/service/serviceImpl.vm | 2 +- 18 files changed, 223 insertions(+), 390 deletions(-) create mode 100644 src/main/java/cn/iocoder/dashboard/modules/tool/controller/codegen/vo/ToolCodegenPreviewRespVO.java diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/controller/GenController.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/controller/GenController.java index 4778ed77c..d18e5ac0b 100644 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/controller/GenController.java +++ b/ruoyi-generator/src/main/java/com/ruoyi/generator/controller/GenController.java @@ -38,17 +38,6 @@ import com.ruoyi.generator.service.IGenTableService; @RequestMapping("/tool/gen") public class GenController extends BaseController { - /** - * 查询数据库列表 - */ - @PreAuthorize("@ss.hasPermi('tool:gen:list')") - @GetMapping("/db/list") - public TableDataInfo dataList(GenTable genTable) { - startPage(); - List list = genTableService.selectDbTableList(genTable); - return getDataTable(list); - } - /** * 查询数据表字段列表 */ @@ -87,16 +76,6 @@ public class GenController extends BaseController { return AjaxResult.success(); } - /** - * 预览代码 - */ - @PreAuthorize("@ss.hasPermi('tool:gen:preview')") - @GetMapping("/preview/{tableId}") - public AjaxResult preview(@PathVariable("tableId") Long tableId) throws IOException { - Map dataMap = genTableService.previewCode(tableId); - return AjaxResult.success(dataMap); - } - /** * 生成代码(下载方式) */ @@ -130,18 +109,6 @@ public class GenController extends BaseController { return AjaxResult.success(); } - /** - * 批量生成代码 - */ - @PreAuthorize("@ss.hasPermi('tool:gen:code')") - @Log(title = "代码生成", businessType = BusinessType.GENCODE) - @GetMapping("/batchGenCode") - public void batchGenCode(HttpServletResponse response, String tables) throws IOException { - String[] tableNames = Convert.toStrArray(tables); - byte[] data = genTableService.downloadCode(tableNames); - genCode(response, data); - } - /** * 生成zip文件 */ @@ -154,4 +121,5 @@ public class GenController extends BaseController { response.setContentType("application/octet-stream; charset=UTF-8"); IOUtils.write(data, response.getOutputStream()); } + } diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java index de1a41990..e28a352a2 100644 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java +++ b/ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java @@ -44,7 +44,6 @@ import com.ruoyi.generator.util.VelocityUtils; */ @Service public class GenTableServiceImpl implements IGenTableService { - private static final Logger log = LoggerFactory.getLogger(GenTableServiceImpl.class); @Autowired private GenTableMapper genTableMapper; @@ -65,17 +64,6 @@ public class GenTableServiceImpl implements IGenTableService { return genTable; } - /** - * 查询业务列表 - * - * @param genTable 业务信息 - * @return 业务集合 - */ - @Override - public List selectGenTableList(GenTable genTable) { - return genTableMapper.selectGenTableList(genTable); - } - /** * 查询据库列表 * @@ -98,25 +86,6 @@ public class GenTableServiceImpl implements IGenTableService { return genTableMapper.selectDbTableListByNames(tableNames); } - /** - * 修改业务 - * - * @param genTable 业务信息 - * @return 结果 - */ - @Override - @Transactional - public void updateGenTable(GenTable genTable) { - String options = JSON.toJSONString(genTable.getParams()); - genTable.setOptions(options); - int row = genTableMapper.updateGenTable(genTable); - if (row > 0) { - for (GenTableColumn cenTableColumn : genTable.getColumns()) { - genTableColumnMapper.updateGenTableColumn(cenTableColumn); - } - } - } - /** * 删除业务对象 * @@ -158,36 +127,6 @@ public class GenTableServiceImpl implements IGenTableService { } } - /** - * 预览代码 - * - * @param tableId 表编号 - * @return 预览数据列表 - */ - @Override - public Map previewCode(Long tableId) { - Map dataMap = new LinkedHashMap<>(); - // 查询表信息 - GenTable table = genTableMapper.selectGenTableById(tableId); - // 查询列信息 - List columns = table.getColumns(); - setPkColumn(table, columns); - VelocityInitializer.initVelocity(); - - VelocityContext context = VelocityUtils.prepareContext(table); - - // 获取模板列表 - List templates = VelocityUtils.getTemplateList(table.getTplCategory()); - for (String template : templates) { - // 渲染模板 - StringWriter sw = new StringWriter(); - Template tpl = Velocity.getTemplate(template, Constants.UTF8); - tpl.merge(context, sw); - dataMap.put(template, sw.toString()); - } - return dataMap; - } - /** * 生成代码(下载方式) * @@ -203,40 +142,6 @@ public class GenTableServiceImpl implements IGenTableService { return outputStream.toByteArray(); } - /** - * 生成代码(自定义路径) - * - * @param tableName 表名称 - */ - @Override - public void generatorCode(String tableName) { - // 查询表信息 - GenTable table = genTableMapper.selectGenTableByName(tableName); - // 查询列信息 - List columns = table.getColumns(); - setPkColumn(table, columns); - - VelocityInitializer.initVelocity(); - - VelocityContext context = VelocityUtils.prepareContext(table); - - // 获取模板列表 - List templates = VelocityUtils.getTemplateList(table.getTplCategory()); - for (String template : templates) { - if (!StringUtils.containsAny(template, "sql.vm", "api.js.vm", "index.vue.vm", "index-tree.vue.vm")) { - // 渲染模板 - StringWriter sw = new StringWriter(); - Template tpl = Velocity.getTemplate(template, Constants.UTF8); - tpl.merge(context, sw); - try { - String path = getGenPath(table, template); - FileUtils.writeStringToFile(new File(path), sw.toString(), CharsetKit.UTF_8); - } catch (IOException e) { - throw new CustomException("渲染模板失败,表名:" + table.getTableName()); - } - } - } - } /** * 同步数据库 @@ -266,57 +171,6 @@ public class GenTableServiceImpl implements IGenTableService { } } - /** - * 批量生成代码(下载方式) - * - * @param tableNames 表数组 - * @return 数据 - */ - @Override - public byte[] downloadCode(String[] tableNames) { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - ZipOutputStream zip = new ZipOutputStream(outputStream); - for (String tableName : tableNames) { - generatorCode(tableName, zip); - } - IOUtils.closeQuietly(zip); - return outputStream.toByteArray(); - } - - /** - * 查询表信息并生成代码 - */ - private void generatorCode(String tableName, ZipOutputStream zip) { - // 查询表信息 - GenTable table = genTableMapper.selectGenTableByName(tableName); - // 查询列信息 - List columns = table.getColumns(); - setPkColumn(table, columns); - - VelocityInitializer.initVelocity(); - - VelocityContext context = VelocityUtils.prepareContext(table); - - // 获取模板列表 - List templates = VelocityUtils.getTemplateList(table.getTplCategory()); - for (String template : templates) { - // 渲染模板 - StringWriter sw = new StringWriter(); - Template tpl = Velocity.getTemplate(template, Constants.UTF8); - tpl.merge(context, sw); - try { - // 添加到zip - zip.putNextEntry(new ZipEntry(VelocityUtils.getFileName(template, table))); - IOUtils.write(sw.toString(), zip, Constants.UTF8); - IOUtils.closeQuietly(sw); - zip.flush(); - zip.closeEntry(); - } catch (IOException e) { - log.error("渲染模板失败,表名:" + table.getTableName(), e); - } - } - } - /** * 修改保存参数校验 * @@ -337,58 +191,4 @@ public class GenTableServiceImpl implements IGenTableService { } } - /** - * 设置主键列信息 - * - * @param table 业务表信息 - * @param columns 业务字段列表 - */ - public void setPkColumn(GenTable table, List columns) { - for (GenTableColumn column : columns) { - if (column.isPk()) { - table.setPkColumn(column); - break; - } - } - if (StringUtils.isNull(table.getPkColumn())) { - table.setPkColumn(columns.get(0)); - } - } - - /** - * 设置代码生成其他选项值 - * - * @param genTable 设置后的生成对象 - */ - public void setTableFromOptions(GenTable genTable) { - JSONObject paramsObj = JSONObject.parseObject(genTable.getOptions()); - if (StringUtils.isNotNull(paramsObj)) { - String treeCode = paramsObj.getString(GenConstants.TREE_CODE); - String treeParentCode = paramsObj.getString(GenConstants.TREE_PARENT_CODE); - String treeName = paramsObj.getString(GenConstants.TREE_NAME); - String parentMenuId = paramsObj.getString(GenConstants.PARENT_MENU_ID); - String parentMenuName = paramsObj.getString(GenConstants.PARENT_MENU_NAME); - - genTable.setTreeCode(treeCode); - genTable.setTreeParentCode(treeParentCode); - genTable.setTreeName(treeName); - genTable.setParentMenuId(parentMenuId); - genTable.setParentMenuName(parentMenuName); - } - } - - /** - * 获取代码生成地址 - * - * @param table 业务表信息 - * @param template 模板文件路径 - * @return 生成地址 - */ - public static String getGenPath(GenTable table, String template) { - String genPath = table.getGenPath(); - if (StringUtils.equals(genPath, "/")) { - return System.getProperty("user.dir") + File.separator + "src" + File.separator + VelocityUtils.getFileName(template, table); - } - return genPath + File.separator + VelocityUtils.getFileName(template, table); - } } diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/util/VelocityUtils.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/util/VelocityUtils.java index a87866d8f..010a3fe48 100644 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/util/VelocityUtils.java +++ b/ruoyi-generator/src/main/java/com/ruoyi/generator/util/VelocityUtils.java @@ -18,15 +18,6 @@ import com.ruoyi.generator.domain.GenTableColumn; * @author ruoyi */ public class VelocityUtils { - /** - * 项目空间路径 - */ - private static final String PROJECT_PATH = "main/java"; - - /** - * mybatis空间路径 - */ - private static final String MYBATIS_PATH = "main/resources/mapper"; /** * 默认上级菜单,系统工具 diff --git a/ruoyi-ui/src/api/tool/codegen.js b/ruoyi-ui/src/api/tool/codegen.js index 3b5eaf911..63856d3bf 100644 --- a/ruoyi-ui/src/api/tool/codegen.js +++ b/ruoyi-ui/src/api/tool/codegen.js @@ -25,3 +25,20 @@ export function updateCodegen(data) { data: data }) } + +// 预览生成代码 +export function previewCodegen(tableId) { + return request({ + url: '/tool/codegen/preview?tableId=' + tableId, + method: 'get' + }) +} + +// 下载生成代码 +export function downloadCodegen(tableId) { + return request({ + url: '/tool/codegen/download?tableId=' + tableId, + method: 'get', + responseType: 'blob' + }) +} diff --git a/ruoyi-ui/src/api/tool/gen.js b/ruoyi-ui/src/api/tool/gen.js index fe7d7fa49..75dfd8fa0 100644 --- a/ruoyi-ui/src/api/tool/gen.js +++ b/ruoyi-ui/src/api/tool/gen.js @@ -18,14 +18,6 @@ export function importTable(data) { }) } -// 预览生成代码 -export function previewTable(tableId) { - return request({ - url: '/tool/gen/preview/' + tableId, - method: 'get' - }) -} - // 删除表数据 export function delTable(tableId) { return request({ diff --git a/ruoyi-ui/src/main.js b/ruoyi-ui/src/main.js index 3bd0e5b38..bf7a93d7d 100644 --- a/ruoyi-ui/src/main.js +++ b/ruoyi-ui/src/main.js @@ -16,7 +16,17 @@ import './assets/icons' // icon import './permission' // permission control import { getDicts } from "@/api/system/dict/data"; import { getConfigKey } from "@/api/infra/config"; -import { parseTime, resetForm, addDateRange, selectDictLabel, selectDictLabels, download, handleTree, downloadExcel } from "@/utils/ruoyi"; +import { + parseTime, + resetForm, + addDateRange, + selectDictLabel, + selectDictLabels, + download, + handleTree, + downloadExcel, + downloadZip +} from "@/utils/ruoyi"; import Pagination from "@/components/Pagination"; // 自定义表格工具扩展 import RightToolbar from "@/components/RightToolbar" @@ -38,6 +48,7 @@ Vue.prototype.getDictDataLabel = getDictDataLabel Vue.prototype.DICT_TYPE = DICT_TYPE Vue.prototype.download = download Vue.prototype.downloadExcel = downloadExcel +Vue.prototype.downloadZip = downloadZip Vue.prototype.handleTree = handleTree Vue.prototype.msgSuccess = function (msg) { diff --git a/ruoyi-ui/src/utils/ruoyi.js b/ruoyi-ui/src/utils/ruoyi.js index 5bb492dff..d79e9f5e7 100644 --- a/ruoyi-ui/src/utils/ruoyi.js +++ b/ruoyi-ui/src/utils/ruoyi.js @@ -116,6 +116,21 @@ export function downloadExcel(data, fileName) { window.URL.revokeObjectURL(href); } +// 下载 Zip 方法 +export function downloadZip(data, fileName) { + // 创建 blob + let blob = new Blob([data], {type: 'application/zip'}); + // 创建 href 超链接,点击进行下载 + window.URL = window.URL || window.webkitURL; + let href = URL.createObjectURL(blob); + let downA = document.createElement("a"); + downA.href = href; + downA.download = fileName; + downA.click(); + // 销毁超连接 + window.URL.revokeObjectURL(href); +} + // 字符串格式化(%s ) export function sprintf(str) { var args = arguments, flag = true, i = 1; diff --git a/ruoyi-ui/src/views/tool/gen/index.vue b/ruoyi-ui/src/views/tool/gen/index.vue index 0a2ffbeae..9ac16ed5a 100644 --- a/ruoyi-ui/src/views/tool/gen/index.vue +++ b/ruoyi-ui/src/views/tool/gen/index.vue @@ -39,70 +39,18 @@ - 生成 + 生成 - 导入 - - - 修改 - - - 删除 + 导入 - - - + + +