作者 RuoYi

代码生成支持同步数据库

@@ -165,13 +165,25 @@ public class GenController extends BaseController @@ -165,13 +165,25 @@ public class GenController extends BaseController
165 @PreAuthorize("@ss.hasPermi('tool:gen:code')") 165 @PreAuthorize("@ss.hasPermi('tool:gen:code')")
166 @Log(title = "代码生成", businessType = BusinessType.GENCODE) 166 @Log(title = "代码生成", businessType = BusinessType.GENCODE)
167 @GetMapping("/genCode/{tableName}") 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 genTableService.generatorCode(tableName); 170 genTableService.generatorCode(tableName);
171 return AjaxResult.success(); 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 @PreAuthorize("@ss.hasPermi('tool:gen:code')") 189 @PreAuthorize("@ss.hasPermi('tool:gen:code')")
@@ -43,6 +43,14 @@ public interface GenTableColumnMapper @@ -43,6 +43,14 @@ public interface GenTableColumnMapper
43 public int updateGenTableColumn(GenTableColumn genTableColumn); 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 * @param ids 需要删除的数据ID 56 * @param ids 需要删除的数据ID
@@ -7,6 +7,7 @@ import java.io.StringWriter; @@ -7,6 +7,7 @@ import java.io.StringWriter;
7 import java.util.LinkedHashMap; 7 import java.util.LinkedHashMap;
8 import java.util.List; 8 import java.util.List;
9 import java.util.Map; 9 import java.util.Map;
  10 +import java.util.stream.Collectors;
10 import java.util.zip.ZipEntry; 11 import java.util.zip.ZipEntry;
11 import java.util.zip.ZipOutputStream; 12 import java.util.zip.ZipOutputStream;
12 import org.apache.commons.io.IOUtils; 13 import org.apache.commons.io.IOUtils;
@@ -224,7 +225,6 @@ public class GenTableServiceImpl implements IGenTableService @@ -224,7 +225,6 @@ public class GenTableServiceImpl implements IGenTableService
224 * 生成代码(自定义路径) 225 * 生成代码(自定义路径)
225 * 226 *
226 * @param tableName 表名称 227 * @param tableName 表名称
227 - * @return 数据  
228 */ 228 */
229 @Override 229 @Override
230 public void generatorCode(String tableName) 230 public void generatorCode(String tableName)
@@ -263,6 +263,37 @@ public class GenTableServiceImpl implements IGenTableService @@ -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 * @param tableNames 表数组 299 * @param tableNames 表数组
@@ -91,6 +91,13 @@ public interface IGenTableService @@ -91,6 +91,13 @@ public interface IGenTableService
91 public void generatorCode(String tableName); 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 * @param tableNames 表数组 103 * @param tableNames 表数组
@@ -117,4 +117,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" @@ -117,4 +117,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
117 </foreach> 117 </foreach>
118 </delete> 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 </mapper> 126 </mapper>
@@ -67,3 +67,10 @@ export function genCode(tableName) { @@ -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 +}
@@ -135,6 +135,13 @@ @@ -135,6 +135,13 @@
135 <el-button 135 <el-button
136 type="text" 136 type="text"
137 size="small" 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 icon="el-icon-download" 145 icon="el-icon-download"
139 @click="handleGenTable(scope.row)" 146 @click="handleGenTable(scope.row)"
140 v-hasPermi="['tool:gen:code']" 147 v-hasPermi="['tool:gen:code']"
@@ -167,7 +174,7 @@ @@ -167,7 +174,7 @@
167 </template> 174 </template>
168 175
169 <script> 176 <script>
170 -import { listTable, previewTable, delTable, genCode } from "@/api/tool/gen"; 177 +import { listTable, previewTable, delTable, genCode, synchDb } from "@/api/tool/gen";
171 import importTable from "./importTable"; 178 import importTable from "./importTable";
172 import { downLoadZip } from "@/utils/zipdownload"; 179 import { downLoadZip } from "@/utils/zipdownload";
173 export default { 180 export default {
@@ -252,6 +259,19 @@ export default { @@ -252,6 +259,19 @@ export default {
252 downLoadZip("/tool/gen/batchGenCode?tables=" + tableNames, "ruoyi"); 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 openImportTable() { 276 openImportTable() {
257 this.$refs.import.show(); 277 this.$refs.import.show();