From 0d55ca747c9c7ebc1180c847883486a6f1ec1262 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Thu, 11 Feb 2021 01:05:21 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=90=8C=E6=AD=A5=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E8=A1=A8=E5=92=8C=E5=AD=97=E6=AE=B5=E5=AE=9A=E4=B9=89=202.=20?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=95=B0=E6=8D=AE=E5=BA=93=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/generator/config/GenConfig.java | 44 +---- .../generator/controller/GenController.java | 114 ----------- .../generator/mapper/GenTableMapper.java | 53 ------ .../service/GenTableServiceImpl.java | 179 ------------------ .../mapper/generator/GenTableMapper.xml | 57 ------ ruoyi-ui/src/api/tool/codegen.js | 41 +++- ruoyi-ui/src/api/tool/gen.js | 43 ----- ruoyi-ui/src/views/tool/gen/editTable.vue | 4 +- ruoyi-ui/src/views/tool/gen/importTable.vue | 37 ++-- ruoyi-ui/src/views/tool/gen/index.vue | 18 +- .../codegen/ToolCodegenController.java | 56 +++++- .../convert/codegen/ToolCodegenConvert.java | 3 + .../mysql/coegen/ToolCodegenColumnMapper.java | 7 +- .../mysql/coegen/ToolSchemaTableMapper.java | 4 +- .../service/codegen/ToolCodegenService.java | 35 +++- .../codegen/impl/ToolCodegenServiceImpl.java | 92 ++++++++- 16 files changed, 239 insertions(+), 548 deletions(-) delete mode 100644 ruoyi-generator/src/main/java/com/ruoyi/generator/controller/GenController.java delete mode 100644 ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/GenTableMapper.java delete mode 100644 ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java delete mode 100644 ruoyi-generator/src/main/resources/mapper/generator/GenTableMapper.xml delete mode 100644 ruoyi-ui/src/api/tool/gen.js diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/config/GenConfig.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/config/GenConfig.java index e6351984d..970a5a2b7 100644 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/config/GenConfig.java +++ b/ruoyi-generator/src/main/java/com/ruoyi/generator/config/GenConfig.java @@ -14,6 +14,7 @@ import org.springframework.stereotype.Component; @ConfigurationProperties(prefix = "gen") @PropertySource(value = { "classpath:generator.yml" }) public class GenConfig { + /** 作者 */ public static String author; @@ -26,47 +27,4 @@ public class GenConfig { /** 表前缀(类名不会包含表前缀) */ public static String tablePrefix; - public static String getAuthor() - { - return author; - } - - @Value("${author}") - public void setAuthor(String author) - { - GenConfig.author = author; - } - - public static String getPackageName() - { - return packageName; - } - - @Value("${packageName}") - public void setPackageName(String packageName) - { - GenConfig.packageName = packageName; - } - - public static boolean getAutoRemovePre() - { - return autoRemovePre; - } - - @Value("${autoRemovePre}") - public void setAutoRemovePre(boolean autoRemovePre) - { - GenConfig.autoRemovePre = autoRemovePre; - } - - public static String getTablePrefix() - { - return tablePrefix; - } - - @Value("${tablePrefix}") - public void setTablePrefix(String tablePrefix) - { - GenConfig.tablePrefix = tablePrefix; - } } 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 deleted file mode 100644 index b7be10d62..000000000 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/controller/GenController.java +++ /dev/null @@ -1,114 +0,0 @@ -package com.ruoyi.generator.controller; - -import java.io.IOException; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import javax.servlet.http.HttpServletResponse; - -import org.apache.commons.io.IOUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.access.prepost.PreAuthorize; -import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; -import com.ruoyi.common.annotation.Log; -import com.ruoyi.common.core.controller.BaseController; -import com.ruoyi.common.core.domain.AjaxResult; -import com.ruoyi.common.core.page.TableDataInfo; -import com.ruoyi.common.core.text.Convert; -import com.ruoyi.common.enums.BusinessType; -import com.ruoyi.generator.domain.GenTable; -import com.ruoyi.generator.domain.GenTableColumn; -import com.ruoyi.generator.service.IGenTableColumnService; -import com.ruoyi.generator.service.IGenTableService; - -/** - * 代码生成 操作处理 - * - * @author ruoyi - */ -@RestController -@RequestMapping("/tool/gen") -public class GenController extends BaseController { - - /** - * 查询数据表字段列表 - */ - @PreAuthorize("@ss.hasPermi('tool:gen:list')") - @GetMapping(value = "/column/{talbleId}") - public TableDataInfo columnList(Long tableId) { - TableDataInfo dataInfo = new TableDataInfo(); - List list = genTableColumnService.selectGenTableColumnListByTableId(tableId); - dataInfo.setRows(list); - dataInfo.setTotal(list.size()); - return dataInfo; - } - - /** - * 导入表结构(保存) - */ - @PreAuthorize("@ss.hasPermi('tool:gen:list')") - @Log(title = "代码生成", businessType = BusinessType.IMPORT) - @PostMapping("/importTable") - public AjaxResult importTableSave(String tables) { - String[] tableNames = Convert.toStrArray(tables); - // 查询表信息 - List tableList = genTableService.selectDbTableListByNames(tableNames); - genTableService.importGenTable(tableList); - return AjaxResult.success(); - } - - /** - * 删除代码生成 - */ - @PreAuthorize("@ss.hasPermi('tool:gen:remove')") - @Log(title = "代码生成", businessType = BusinessType.DELETE) - @DeleteMapping("/{tableIds}") - public AjaxResult remove(@PathVariable Long[] tableIds) { - genTableService.deleteGenTableByIds(tableIds); - return AjaxResult.success(); - } - - /** - * 生成代码(自定义路径) - */ - @PreAuthorize("@ss.hasPermi('tool:gen:code')") - @Log(title = "代码生成", businessType = BusinessType.GENCODE) - @GetMapping("/genCode/{tableName}") - public AjaxResult genCode(@PathVariable("tableName") String tableName) { - genTableService.generatorCode(tableName); - return AjaxResult.success(); - } - - /** - * 同步数据库 - */ - @PreAuthorize("@ss.hasPermi('tool:gen:edit')") - @Log(title = "代码生成", businessType = BusinessType.UPDATE) - @GetMapping("/synchDb/{tableName}") - public AjaxResult synchDb(@PathVariable("tableName") String tableName) { - genTableService.synchDb(tableName); - return AjaxResult.success(); - } - - /** - * 生成zip文件 - */ - private void genCode(HttpServletResponse response, byte[] data) throws IOException { - response.reset(); - response.addHeader("Access-Control-Allow-Origin", "*"); - response.addHeader("Access-Control-Expose-Headers", "Content-Disposition"); - response.setHeader("Content-Disposition", "attachment; filename=\"ruoyi.zip\""); - response.addHeader("Content-Length", "" + data.length); - response.setContentType("application/octet-stream; charset=UTF-8"); - IOUtils.write(data, response.getOutputStream()); - } - -} diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/GenTableMapper.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/GenTableMapper.java deleted file mode 100644 index fbd734133..000000000 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/GenTableMapper.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.ruoyi.generator.mapper; - -import java.util.List; - -import com.ruoyi.generator.domain.GenTable; - -/** - * 业务 数据层 - * - * @author ruoyi - */ -public interface GenTableMapper { - /** - * 查询业务列表 - * - * @param genTable 业务信息 - * @return 业务集合 - */ - public List selectGenTableList(GenTable genTable); - - /** - * 查询据库列表 - * - * @param genTable 业务信息 - * @return 数据库表集合 - */ - public List selectDbTableList(GenTable genTable); - - /** - * 查询据库列表 - * - * @param tableNames 表名称组 - * @return 数据库表集合 - */ - public List selectDbTableListByNames(String[] tableNames); - - /** - * 查询表ID业务信息 - * - * @param id 业务ID - * @return 业务信息 - */ - public GenTable selectGenTableById(Long id); - - /** - * 查询表名称业务信息 - * - * @param tableName 表名称 - * @return 业务信息 - */ - public GenTable selectGenTableByName(String tableName); - -} 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 deleted file mode 100644 index daa574de5..000000000 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java +++ /dev/null @@ -1,179 +0,0 @@ -package com.ruoyi.generator.service; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.IOException; -import java.io.StringWriter; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; -import java.util.zip.ZipEntry; -import java.util.zip.ZipOutputStream; - -import org.apache.commons.io.IOUtils; -import org.apache.velocity.Template; -import org.apache.velocity.VelocityContext; -import org.apache.velocity.app.Velocity; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.ruoyi.common.constant.Constants; -import com.ruoyi.common.constant.GenConstants; -import com.ruoyi.common.core.text.CharsetKit; -import com.ruoyi.common.exception.CustomException; -import com.ruoyi.common.utils.SecurityUtils; -import com.ruoyi.common.utils.StringUtils; -import com.ruoyi.common.utils.file.FileUtils; -import com.ruoyi.generator.domain.GenTable; -import com.ruoyi.generator.domain.GenTableColumn; -import com.ruoyi.generator.mapper.GenTableColumnMapper; -import com.ruoyi.generator.mapper.GenTableMapper; -import com.ruoyi.generator.util.GenUtils; -import com.ruoyi.generator.util.VelocityInitializer; -import com.ruoyi.generator.util.VelocityUtils; - -/** - * 业务 服务层实现 - * - * @author ruoyi - */ -@Service -public class GenTableServiceImpl implements IGenTableService { - - @Autowired - private GenTableMapper genTableMapper; - - @Autowired - private GenTableColumnMapper genTableColumnMapper; - - /** - * 查询业务信息 - * - * @param id 业务ID - * @return 业务信息 - */ - @Override - public GenTable selectGenTableById(Long id) { - GenTable genTable = genTableMapper.selectGenTableById(id); - setTableFromOptions(genTable); - return genTable; - } - - /** - * 查询据库列表 - * - * @param genTable 业务信息 - * @return 数据库表集合 - */ - @Override - public List selectDbTableList(GenTable genTable) { - return genTableMapper.selectDbTableList(genTable); - } - - /** - * 查询据库列表 - * - * @param tableNames 表名称组 - * @return 数据库表集合 - */ - @Override - public List selectDbTableListByNames(String[] tableNames) { - return genTableMapper.selectDbTableListByNames(tableNames); - } - - /** - * 删除业务对象 - * - * @param tableIds 需要删除的数据ID - * @return 结果 - */ - @Override - @Transactional - public void deleteGenTableByIds(Long[] tableIds) { - genTableMapper.deleteGenTableByIds(tableIds); - genTableColumnMapper.deleteGenTableColumnByIds(tableIds); - } - - /** - * 导入表结构 - * - * @param tableList 导入表列表 - */ - @Override - @Transactional - public void importGenTable(List tableList) { - String operName = SecurityUtils.getUsername(); - try { - for (GenTable table : tableList) { - String tableName = table.getTableName(); - GenUtils.initTable(table, operName); - int row = genTableMapper.insertGenTable(table); - if (row > 0) { - // 保存列信息 - List genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName); - for (GenTableColumn column : genTableColumns) { - GenUtils.initColumnField(column, table); - genTableColumnMapper.insertGenTableColumn(column); - } - } - } - } catch (Exception e) { - throw new CustomException("导入失败:" + e.getMessage()); - } - } - - - /** - * 同步数据库 - * - * @param tableName 表名称 - */ - @Override - @Transactional - public void synchDb(String tableName) { - GenTable table = genTableMapper.selectGenTableByName(tableName); - List tableColumns = table.getColumns(); - List tableColumnNames = tableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList()); - - List dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName); - List dbTableColumnNames = dbTableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList()); - - dbTableColumns.forEach(column -> { - if (!tableColumnNames.contains(column.getColumnName())) { - GenUtils.initColumnField(column, table); - genTableColumnMapper.insertGenTableColumn(column); - } - }); - - List delColumns = tableColumns.stream().filter(column -> !dbTableColumnNames.contains(column.getColumnName())).collect(Collectors.toList()); - if (StringUtils.isNotEmpty(delColumns)) { - genTableColumnMapper.deleteGenTableColumns(delColumns); - } - } - - /** - * 修改保存参数校验 - * - * @param genTable 业务信息 - */ - @Override - public void validateEdit(GenTable genTable) { - if (GenConstants.TPL_TREE.equals(genTable.getTplCategory())) { - String options = JSON.toJSONString(genTable.getParams()); - JSONObject paramsObj = JSONObject.parseObject(options); - if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_CODE))) { - throw new CustomException("树编码字段不能为空"); - } else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_PARENT_CODE))) { - throw new CustomException("树父编码字段不能为空"); - } else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_NAME))) { - throw new CustomException("树名称字段不能为空"); - } - } - } - -} diff --git a/ruoyi-generator/src/main/resources/mapper/generator/GenTableMapper.xml b/ruoyi-generator/src/main/resources/mapper/generator/GenTableMapper.xml deleted file mode 100644 index 33b8f56a2..000000000 --- a/ruoyi-generator/src/main/resources/mapper/generator/GenTableMapper.xml +++ /dev/null @@ -1,57 +0,0 @@ - - - - - - - - - - - - - - - diff --git a/ruoyi-ui/src/api/tool/codegen.js b/ruoyi-ui/src/api/tool/codegen.js index 3fa8b865a..486664ecc 100644 --- a/ruoyi-ui/src/api/tool/codegen.js +++ b/ruoyi-ui/src/api/tool/codegen.js @@ -1,7 +1,7 @@ import request from '@/utils/request' // 获得表定义分页 -export function getCodeGenTablePage(query) { +export function getCodegenTablePage(query) { return request({ url: '/tool/codegen/table/page', method: 'get', @@ -10,7 +10,7 @@ export function getCodeGenTablePage(query) { } // 获得表和字段的明细 -export function getCodeGenDetail(tableId) { +export function getCodegenDetail(tableId) { return request({ url: '/tool/codegen/detail?tableId=' + tableId, method: 'get', @@ -26,6 +26,14 @@ export function updateCodegen(data) { }) } +// 基于数据库的表结构,同步数据库的表和字段定义 +export function syncCodegen(tableId) { + return request({ + url: '/tool/codegen/sync?tableId=' + tableId, + method: 'put' + }) +} + // 预览生成代码 export function previewCodegen(tableId) { return request({ @@ -42,3 +50,32 @@ export function downloadCodegen(tableId) { responseType: 'blob' }) } + +// 获得表定义分页 +export function getSchemaTableList(query) { + return request({ + url: '/tool/codegen/db/table/list', + method: 'get', + params: query + }) +} + +// 基于数据库的表结构,创建代码生成器的表定义 +export function createCodegenList(tableNames) { + return request({ + url: '/tool/codegen/create-list', + method: 'post', + headers:{ + 'Content-type': 'application/x-www-form-urlencoded' + }, + data: 'tableNames=' + tableNames + }) +} + +// 删除数据库的表和字段定义 +export function deleteCodegen(tableId) { + return request({ + url: '/tool/codegen/delete?tableId=' + tableId, + method: 'delete' + }) +} diff --git a/ruoyi-ui/src/api/tool/gen.js b/ruoyi-ui/src/api/tool/gen.js deleted file mode 100644 index 75dfd8fa0..000000000 --- a/ruoyi-ui/src/api/tool/gen.js +++ /dev/null @@ -1,43 +0,0 @@ -import request from '@/utils/request' - -// 查询db数据库列表 -export function listDbTable(query) { - return request({ - url: '/tool/gen/db/list', - method: 'get', - params: query - }) -} - -// 导入表 -export function importTable(data) { - return request({ - url: '/tool/gen/importTable', - method: 'post', - params: data - }) -} - -// 删除表数据 -export function delTable(tableId) { - return request({ - url: '/tool/gen/' + tableId, - method: 'delete' - }) -} - -// 生成代码(自定义路径) -export function genCode(tableName) { - return request({ - url: '/tool/gen/genCode/' + tableName, - method: 'get' - }) -} - -// 同步数据库 -export function synchDb(tableName) { - return request({ - url: '/tool/gen/synchDb/' + tableName, - method: 'get' - }) -} diff --git a/ruoyi-ui/src/views/tool/gen/editTable.vue b/ruoyi-ui/src/views/tool/gen/editTable.vue index 5e45188a6..3a57e6fa1 100644 --- a/ruoyi-ui/src/views/tool/gen/editTable.vue +++ b/ruoyi-ui/src/views/tool/gen/editTable.vue @@ -126,7 +126,7 @@