From 9e38c7de2e51613e248926b4a29616959853682d Mon Sep 17 00:00:00 2001 From: RuoYi Date: Fri, 18 Sep 2020 10:24:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=90=8C=E6=AD=A5=E6=95=B0=E6=8D=AE=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../generator/controller/GenController.java | 14 +++++++- .../mapper/GenTableColumnMapper.java | 10 +++++- .../service/GenTableServiceImpl.java | 33 ++++++++++++++++++- .../generator/service/IGenTableService.java | 7 ++++ .../mapper/generator/GenTableColumnMapper.xml | 6 ++++ .../mapper/generator/GenTableMapper.xml | 2 +- ruoyi-ui/src/api/tool/gen.js | 7 ++++ ruoyi-ui/src/views/tool/gen/index.vue | 22 ++++++++++++- 8 files changed, 96 insertions(+), 5 deletions(-) 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 df72fcb99..9de4d4472 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 @@ -165,12 +165,24 @@ public class GenController extends BaseController @PreAuthorize("@ss.hasPermi('tool:gen:code')") @Log(title = "代码生成", businessType = BusinessType.GENCODE) @GetMapping("/genCode/{tableName}") - public AjaxResult genCode(HttpServletResponse response, @PathVariable("tableName") String 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(); + } + /** * 批量生成代码 */ diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/GenTableColumnMapper.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/GenTableColumnMapper.java index 1366453c8..0dc342895 100644 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/GenTableColumnMapper.java +++ b/ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/GenTableColumnMapper.java @@ -17,7 +17,7 @@ public interface GenTableColumnMapper * @return 列信息 */ public List selectDbTableColumnsByName(String tableName); - + /** * 查询业务字段列表 * @@ -42,6 +42,14 @@ public interface GenTableColumnMapper */ public int updateGenTableColumn(GenTableColumn genTableColumn); + /** + * 删除业务字段 + * + * @param genTableColumns 列数据 + * @return 结果 + */ + public int deleteGenTableColumns(List genTableColumns); + /** * 批量删除业务字段 * 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 1286a0053..4f663021d 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 @@ -7,6 +7,7 @@ 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; @@ -224,7 +225,6 @@ public class GenTableServiceImpl implements IGenTableService * 生成代码(自定义路径) * * @param tableName 表名称 - * @return 数据 */ @Override public void generatorCode(String tableName) @@ -262,6 +262,37 @@ public class GenTableServiceImpl implements IGenTableService } } + /** + * 同步数据库 + * + * @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); + } + } + /** * 批量生成代码(下载方式) * diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/service/IGenTableService.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/service/IGenTableService.java index 049cb5fd4..cb2f04373 100644 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/service/IGenTableService.java +++ b/ruoyi-generator/src/main/java/com/ruoyi/generator/service/IGenTableService.java @@ -90,6 +90,13 @@ public interface IGenTableService */ public void generatorCode(String tableName); + /** + * 同步数据库 + * + * @param tableName 表名称 + */ + public void synchDb(String tableName); + /** * 批量生成代码(下载方式) * diff --git a/ruoyi-generator/src/main/resources/mapper/generator/GenTableColumnMapper.xml b/ruoyi-generator/src/main/resources/mapper/generator/GenTableColumnMapper.xml index 513275653..90b71a97f 100644 --- a/ruoyi-generator/src/main/resources/mapper/generator/GenTableColumnMapper.xml +++ b/ruoyi-generator/src/main/resources/mapper/generator/GenTableColumnMapper.xml @@ -117,4 +117,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + + delete from gen_table_column where column_id in + + #{item.columnId} + + \ No newline at end of file diff --git a/ruoyi-generator/src/main/resources/mapper/generator/GenTableMapper.xml b/ruoyi-generator/src/main/resources/mapper/generator/GenTableMapper.xml index 6ff51ff8c..75c8caedd 100644 --- a/ruoyi-generator/src/main/resources/mapper/generator/GenTableMapper.xml +++ b/ruoyi-generator/src/main/resources/mapper/generator/GenTableMapper.xml @@ -186,4 +186,4 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - \ No newline at end of file + \ No newline at end of file diff --git a/ruoyi-ui/src/api/tool/gen.js b/ruoyi-ui/src/api/tool/gen.js index 6c87d4f75..afaf2a959 100644 --- a/ruoyi-ui/src/api/tool/gen.js +++ b/ruoyi-ui/src/api/tool/gen.js @@ -67,3 +67,10 @@ export function genCode(tableName) { }) } +// 同步数据库 +export function synchDb(tableName) { + return request({ + url: '/tool/gen/synchDb/' + tableName, + method: 'get' + }) +} diff --git a/ruoyi-ui/src/views/tool/gen/index.vue b/ruoyi-ui/src/views/tool/gen/index.vue index 7f37f62e0..beebfe2de 100644 --- a/ruoyi-ui/src/views/tool/gen/index.vue +++ b/ruoyi-ui/src/views/tool/gen/index.vue @@ -132,6 +132,13 @@ @click="handleDelete(scope.row)" v-hasPermi="['tool:gen:remove']" >删除 + 同步