Commit 9e38c7de2e51613e248926b4a29616959853682d

Authored by RuoYi
1 parent 9ca28d6d

代码生成支持同步数据库

ruoyi-generator/src/main/java/com/ruoyi/generator/controller/GenController.java
... ... @@ -165,13 +165,25 @@ public class GenController extends BaseController
165 165 @PreAuthorize("@ss.hasPermi('tool:gen:code')")
166 166 @Log(title = "代码生成", businessType = BusinessType.GENCODE)
167 167 @GetMapping("/genCode/{tableName}")
168   - public AjaxResult genCode(HttpServletResponse response, @PathVariable("tableName") String tableName)
  168 + public AjaxResult genCode(@PathVariable("tableName") String tableName)
169 169 {
170 170 genTableService.generatorCode(tableName);
171 171 return AjaxResult.success();
172 172 }
173 173  
174 174 /**
  175 + * 同步数据库
  176 + */
  177 + @PreAuthorize("@ss.hasPermi('tool:gen:edit')")
  178 + @Log(title = "代码生成", businessType = BusinessType.UPDATE)
  179 + @GetMapping("/synchDb/{tableName}")
  180 + public AjaxResult synchDb(@PathVariable("tableName") String tableName)
  181 + {
  182 + genTableService.synchDb(tableName);
  183 + return AjaxResult.success();
  184 + }
  185 +
  186 + /**
175 187 * 批量生成代码
176 188 */
177 189 @PreAuthorize("@ss.hasPermi('tool:gen:code')")
... ...
ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/GenTableColumnMapper.java
... ... @@ -17,7 +17,7 @@ public interface GenTableColumnMapper
17 17 * @return 列信息
18 18 */
19 19 public List<GenTableColumn> selectDbTableColumnsByName(String tableName);
20   -
  20 +
21 21 /**
22 22 * 查询业务字段列表
23 23 *
... ... @@ -43,6 +43,14 @@ public interface GenTableColumnMapper
43 43 public int updateGenTableColumn(GenTableColumn genTableColumn);
44 44  
45 45 /**
  46 + * 删除业务字段
  47 + *
  48 + * @param genTableColumns 列数据
  49 + * @return 结果
  50 + */
  51 + public int deleteGenTableColumns(List<GenTableColumn> genTableColumns);
  52 +
  53 + /**
46 54 * 批量删除业务字段
47 55 *
48 56 * @param ids 需要删除的数据ID
... ...
ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java
... ... @@ -7,6 +7,7 @@ import java.io.StringWriter;
7 7 import java.util.LinkedHashMap;
8 8 import java.util.List;
9 9 import java.util.Map;
  10 +import java.util.stream.Collectors;
10 11 import java.util.zip.ZipEntry;
11 12 import java.util.zip.ZipOutputStream;
12 13 import org.apache.commons.io.IOUtils;
... ... @@ -224,7 +225,6 @@ public class GenTableServiceImpl implements IGenTableService
224 225 * 生成代码(自定义路径)
225 226 *
226 227 * @param tableName 表名称
227   - * @return 数据
228 228 */
229 229 @Override
230 230 public void generatorCode(String tableName)
... ... @@ -263,6 +263,37 @@ public class GenTableServiceImpl implements IGenTableService
263 263 }
264 264  
265 265 /**
  266 + * 同步数据库
  267 + *
  268 + * @param tableName 表名称
  269 + */
  270 + @Override
  271 + @Transactional
  272 + public void synchDb(String tableName)
  273 + {
  274 + GenTable table = genTableMapper.selectGenTableByName(tableName);
  275 + List<GenTableColumn> tableColumns = table.getColumns();
  276 + List<String> tableColumnNames = tableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
  277 +
  278 + List<GenTableColumn> dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
  279 + List<String> dbTableColumnNames = dbTableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
  280 +
  281 + dbTableColumns.forEach(column -> {
  282 + if (!tableColumnNames.contains(column.getColumnName()))
  283 + {
  284 + GenUtils.initColumnField(column, table);
  285 + genTableColumnMapper.insertGenTableColumn(column);
  286 + }
  287 + });
  288 +
  289 + List<GenTableColumn> delColumns = tableColumns.stream().filter(column -> !dbTableColumnNames.contains(column.getColumnName())).collect(Collectors.toList());
  290 + if (StringUtils.isNotEmpty(delColumns))
  291 + {
  292 + genTableColumnMapper.deleteGenTableColumns(delColumns);
  293 + }
  294 + }
  295 +
  296 + /**
266 297 * 批量生成代码(下载方式)
267 298 *
268 299 * @param tableNames 表数组
... ...
ruoyi-generator/src/main/java/com/ruoyi/generator/service/IGenTableService.java
... ... @@ -91,6 +91,13 @@ public interface IGenTableService
91 91 public void generatorCode(String tableName);
92 92  
93 93 /**
  94 + * 同步数据库
  95 + *
  96 + * @param tableName 表名称
  97 + */
  98 + public void synchDb(String tableName);
  99 +
  100 + /**
94 101 * 批量生成代码(下载方式)
95 102 *
96 103 * @param tableNames 表数组
... ...
ruoyi-generator/src/main/resources/mapper/generator/GenTableColumnMapper.xml
... ... @@ -117,4 +117,10 @@ PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot;
117 117 </foreach>
118 118 </delete>
119 119  
  120 + <delete id="deleteGenTableColumns">
  121 + delete from gen_table_column where column_id in
  122 + <foreach collection="list" item="item" open="(" separator="," close=")">
  123 + #{item.columnId}
  124 + </foreach>
  125 + </delete>
120 126 </mapper>
121 127 \ No newline at end of file
... ...
ruoyi-generator/src/main/resources/mapper/generator/GenTableMapper.xml
... ... @@ -186,4 +186,4 @@ PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot;
186 186 </foreach>
187 187 </delete>
188 188  
189   -</mapper>
190 189 \ No newline at end of file
  190 +</mapper>
191 191 \ No newline at end of file
... ...
ruoyi-ui/src/api/tool/gen.js
... ... @@ -67,3 +67,10 @@ export function genCode(tableName) {
67 67 })
68 68 }
69 69  
  70 +// 同步数据库
  71 +export function synchDb(tableName) {
  72 + return request({
  73 + url: '/tool/gen/synchDb/' + tableName,
  74 + method: 'get'
  75 + })
  76 +}
... ...
ruoyi-ui/src/views/tool/gen/index.vue
... ... @@ -135,6 +135,13 @@
135 135 <el-button
136 136 type="text"
137 137 size="small"
  138 + icon="el-icon-refresh"
  139 + @click="handleSynchDb(scope.row)"
  140 + v-hasPermi="['tool:gen:edit']"
  141 + >同步</el-button>
  142 + <el-button
  143 + type="text"
  144 + size="small"
138 145 icon="el-icon-download"
139 146 @click="handleGenTable(scope.row)"
140 147 v-hasPermi="['tool:gen:code']"
... ... @@ -167,7 +174,7 @@
167 174 </template>
168 175  
169 176 <script>
170   -import { listTable, previewTable, delTable, genCode } from "@/api/tool/gen";
  177 +import { listTable, previewTable, delTable, genCode, synchDb } from "@/api/tool/gen";
171 178 import importTable from "./importTable";
172 179 import { downLoadZip } from "@/utils/zipdownload";
173 180 export default {
... ... @@ -252,6 +259,19 @@ export default {
252 259 downLoadZip("/tool/gen/batchGenCode?tables=" + tableNames, "ruoyi");
253 260 }
254 261 },
  262 + /** 同步数据库操作 */
  263 + handleSynchDb(row) {
  264 + const tableName = row.tableName;
  265 + this.$confirm('确认要强制同步"' + tableName + '"表结构吗?', "警告", {
  266 + confirmButtonText: "确定",
  267 + cancelButtonText: "取消",
  268 + type: "warning"
  269 + }).then(function() {
  270 + return synchDb(tableName);
  271 + }).then(() => {
  272 + this.msgSuccess("同步成功");
  273 + }).catch(function() {});
  274 + },
255 275 /** 打开导入表弹窗 */
256 276 openImportTable() {
257 277 this.$refs.import.show();
... ...