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 49a959540..e60c27a1d 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 @@ -206,12 +206,25 @@ public class GenController extends BaseController @Log(title = "代码生成", businessType = BusinessType.GENCODE) @GetMapping("/genCode/{tableName}") @ResponseBody - public AjaxResult genCode(HttpServletResponse response, @PathVariable("tableName") String tableName) + public AjaxResult genCode(@PathVariable("tableName") String tableName) { genTableService.generatorCode(tableName); return AjaxResult.success(); } + /** + * 同步数据库 + */ + @RequiresPermissions("tool:gen:edit") + @Log(title = "代码生成", businessType = BusinessType.UPDATE) + @GetMapping("/synchDb/{tableName}") + @ResponseBody + 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 6ebe2306b..a819c741b 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); + /** * 批量删除业务字段 * @@ -49,4 +57,4 @@ public interface GenTableColumnMapper * @return 结果 */ public int deleteGenTableColumnByIds(Long[] ids); -} \ No newline at end of file +} 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 041567b59..5f5f48de0 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 @@ -94,9 +94,15 @@ public interface IGenTableService * 生成代码(自定义路径) * * @param tableName 表名称 - * @return 数据 */ public void generatorCode(String tableName); + + /** + * 同步数据库 + * + * @param tableName 表名称 + */ + public void synchDb(String tableName); /** * 批量生成代码(下载方式) diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/service/impl/GenTableServiceImpl.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/service/impl/GenTableServiceImpl.java index d007035ce..dbd2881a5 100644 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/service/impl/GenTableServiceImpl.java +++ b/ruoyi-generator/src/main/java/com/ruoyi/generator/service/impl/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; @@ -237,7 +238,6 @@ public class GenTableServiceImpl implements IGenTableService * 生成代码(自定义路径) * * @param tableName 表名称 - * @return 数据 */ @Override public void generatorCode(String tableName) @@ -276,6 +276,38 @@ 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/resources/mapper/generator/GenTableColumnMapper.xml b/ruoyi-generator/src/main/resources/mapper/generator/GenTableColumnMapper.xml index acfaebcfb..cab47441a 100644 --- a/ruoyi-generator/src/main/resources/mapper/generator/GenTableColumnMapper.xml +++ b/ruoyi-generator/src/main/resources/mapper/generator/GenTableColumnMapper.xml @@ -116,5 +116,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" #{tableId} - + + + 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/templates/tool/gen/gen.html b/ruoyi-generator/src/main/resources/templates/tool/gen/gen.html index 63efe95a8..80de8125d 100644 --- a/ruoyi-generator/src/main/resources/templates/tool/gen/gen.html +++ b/ruoyi-generator/src/main/resources/templates/tool/gen/gen.html @@ -125,6 +125,7 @@ actions.push('预览 '); actions.push('编辑 '); actions.push('删除 '); + actions.push('同步 '); actions.push('生成代码 '); return actions.join(''); } @@ -173,6 +174,13 @@ } }) } + + // 同步数据库 + function synchDb(tableName){ + $.modal.confirm("确认要强制同步" + tableName + "表结构吗?", function() { + $.operate.get(prefix + "/synchDb/" + tableName); + }) + } // 批量生成代码 function batchGenCode() {