Browse Source

代码生成支持同步数据库

RuoYi 5 years ago
parent
commit
f4f89f9de6

+ 13 - 1
ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/controller/GenController.java

@@ -164,13 +164,25 @@ public class GenController extends BaseController
164
     @PreAuthorize(hasPermi = "tool:gen:code")
164
     @PreAuthorize(hasPermi = "tool:gen:code")
165
     @Log(title = "代码生成", businessType = BusinessType.GENCODE)
165
     @Log(title = "代码生成", businessType = BusinessType.GENCODE)
166
     @GetMapping("/genCode/{tableName}")
166
     @GetMapping("/genCode/{tableName}")
167
-    public AjaxResult genCode(HttpServletResponse response, @PathVariable("tableName") String tableName)
167
+    public AjaxResult genCode(@PathVariable("tableName") String tableName)
168
     {
168
     {
169
         genTableService.generatorCode(tableName);
169
         genTableService.generatorCode(tableName);
170
         return AjaxResult.success();
170
         return AjaxResult.success();
171
     }
171
     }
172
 
172
 
173
     /**
173
     /**
174
+     * 同步数据库
175
+     */
176
+    @PreAuthorize(hasPermi = "tool:gen:edit")
177
+    @Log(title = "代码生成", businessType = BusinessType.UPDATE)
178
+    @GetMapping("/synchDb/{tableName}")
179
+    public AjaxResult synchDb(@PathVariable("tableName") String tableName)
180
+    {
181
+        genTableService.synchDb(tableName);
182
+        return AjaxResult.success();
183
+    }
184
+
185
+    /**
174
      * 批量生成代码
186
      * 批量生成代码
175
      */
187
      */
176
     @PreAuthorize(hasPermi = "tool:gen:code")
188
     @PreAuthorize(hasPermi = "tool:gen:code")

+ 9 - 1
ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/mapper/GenTableColumnMapper.java

@@ -17,7 +17,7 @@ public interface GenTableColumnMapper
17
      * @return 列信息
17
      * @return 列信息
18
      */
18
      */
19
     public List<GenTableColumn> selectDbTableColumnsByName(String tableName);
19
     public List<GenTableColumn> selectDbTableColumnsByName(String tableName);
20
-    
20
+
21
     /**
21
     /**
22
      * 查询业务字段列表
22
      * 查询业务字段列表
23
      * 
23
      * 
@@ -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

+ 32 - 1
ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/service/GenTableServiceImpl.java

@@ -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;
@@ -223,7 +224,6 @@ public class GenTableServiceImpl implements IGenTableService
223
      * 生成代码(自定义路径)
224
      * 生成代码(自定义路径)
224
      * 
225
      * 
225
      * @param tableName 表名称
226
      * @param tableName 表名称
226
-     * @return 数据
227
      */
227
      */
228
     @Override
228
     @Override
229
     public void generatorCode(String tableName)
229
     public void generatorCode(String tableName)
@@ -262,6 +262,37 @@ public class GenTableServiceImpl implements IGenTableService
262
     }
262
     }
263
 
263
 
264
     /**
264
     /**
265
+     * 同步数据库
266
+     * 
267
+     * @param tableName 表名称
268
+     */
269
+    @Override
270
+    @Transactional
271
+    public void synchDb(String tableName)
272
+    {
273
+        GenTable table = genTableMapper.selectGenTableByName(tableName);
274
+        List<GenTableColumn> tableColumns = table.getColumns();
275
+        List<String> tableColumnNames = tableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
276
+
277
+        List<GenTableColumn> dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
278
+        List<String> dbTableColumnNames = dbTableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
279
+
280
+        dbTableColumns.forEach(column -> {
281
+            if (!tableColumnNames.contains(column.getColumnName()))
282
+            {
283
+                GenUtils.initColumnField(column, table);
284
+                genTableColumnMapper.insertGenTableColumn(column);
285
+            }
286
+        });
287
+
288
+        List<GenTableColumn> delColumns = tableColumns.stream().filter(column -> !dbTableColumnNames.contains(column.getColumnName())).collect(Collectors.toList());
289
+        if (StringUtils.isNotEmpty(delColumns))
290
+        {
291
+            genTableColumnMapper.deleteGenTableColumns(delColumns);
292
+        }
293
+    }
294
+
295
+    /**
265
      * 批量生成代码(下载方式)
296
      * 批量生成代码(下载方式)
266
      * 
297
      * 
267
      * @param tableNames 表数组
298
      * @param tableNames 表数组

+ 7 - 0
ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/service/IGenTableService.java

@@ -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 表数组

+ 7 - 0
ruoyi-modules/ruoyi-gen/src/main/resources/mapper/generator/GenTableColumnMapper.xml

@@ -117,4 +117,11 @@ 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>
126
+
120
 </mapper>
127
 </mapper>

+ 1 - 1
ruoyi-modules/ruoyi-gen/src/main/resources/mapper/generator/GenTableMapper.xml

@@ -180,4 +180,4 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
180
         </foreach>
180
         </foreach>
181
     </delete>
181
     </delete>
182
 
182
 
183
-</mapper> 
183
+</mapper>

+ 1 - 1
ruoyi-modules/ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml

@@ -75,7 +75,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
75
 			AND date_format(u.create_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
75
 			AND date_format(u.create_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
76
 		</if>
76
 		</if>
77
 		<if test="deptId != null and deptId != 0">
77
 		<if test="deptId != null and deptId != 0">
78
-			AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE FIND_IN_SET (#{deptId},ancestors) ))
78
+			AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE find_in_set(#{deptId}, ancestors) ))
79
 		</if>
79
 		</if>
80
 		<!-- 数据范围过滤 -->
80
 		<!-- 数据范围过滤 -->
81
 		${params.dataScope}
81
 		${params.dataScope}

+ 8 - 0
ruoyi-ui/src/api/tool/gen.js

@@ -66,3 +66,11 @@ export function genCode(tableName) {
66
     method: 'get'
66
     method: 'get'
67
   })
67
   })
68
 }
68
 }
69
+
70
+// 同步数据库
71
+export function synchDb(tableName) {
72
+  return request({
73
+    url: '/code/gen/synchDb/' + tableName,
74
+    method: 'get'
75
+  })
76
+}

+ 21 - 1
ruoyi-ui/src/views/tool/gen/index.vue

@@ -132,6 +132,13 @@
132
             @click="handleDelete(scope.row)"
132
             @click="handleDelete(scope.row)"
133
             v-hasPermi="['tool:gen:remove']"
133
             v-hasPermi="['tool:gen:remove']"
134
           >删除</el-button>
134
           >删除</el-button>
135
+		  <el-button
136
+            type="text"
137
+            size="small"
138
+            icon="el-icon-refresh"
139
+            @click="handleSynchDb(scope.row)"
140
+            v-hasPermi="['tool:gen:edit']"
141
+          >同步</el-button>
135
           <el-button
142
           <el-button
136
             type="text"
143
             type="text"
137
             size="small"
144
             size="small"
@@ -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
           downLoadZip("/code/gen/batchGenCode?tables=" + tableNames, "ruoyi");
259
           downLoadZip("/code/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();