作者 RuoYi

代码生成新增创建表结构功能

@@ -163,5 +163,5 @@ public class Constants @@ -163,5 +163,5 @@ public class Constants
163 * 定时任务违规的字符 163 * 定时任务违规的字符
164 */ 164 */
165 public static final String[] JOB_ERROR_STR = { "java.net.URL", "javax.naming.InitialContext", "org.yaml.snakeyaml", 165 public static final String[] JOB_ERROR_STR = { "java.net.URL", "javax.naming.InitialContext", "org.yaml.snakeyaml",
166 - "org.springframework", "org.apache", "com.ruoyi.common.utils.file", "com.ruoyi.common.config" }; 166 + "org.springframework", "org.apache", "com.ruoyi.common.utils.file", "com.ruoyi.common.config", "com.ruoyi.generator" };
167 } 167 }
@@ -17,24 +17,24 @@ @@ -17,24 +17,24 @@
17 17
18 <dependencies> 18 <dependencies>
19 19
20 - <!--velocity代码生成使用模板 --> 20 + <!-- velocity代码生成使用模板 -->
21 <dependency> 21 <dependency>
22 <groupId>org.apache.velocity</groupId> 22 <groupId>org.apache.velocity</groupId>
23 <artifactId>velocity-engine-core</artifactId> 23 <artifactId>velocity-engine-core</artifactId>
24 </dependency> 24 </dependency>
25 25
26 - <!-- collections工具类 -->  
27 - <dependency>  
28 - <groupId>commons-collections</groupId>  
29 - <artifactId>commons-collections</artifactId>  
30 - </dependency>  
31 -  
32 <!-- 通用工具--> 26 <!-- 通用工具-->
33 <dependency> 27 <dependency>
34 <groupId>com.ruoyi</groupId> 28 <groupId>com.ruoyi</groupId>
35 <artifactId>ruoyi-common</artifactId> 29 <artifactId>ruoyi-common</artifactId>
36 </dependency> 30 </dependency>
37 31
  32 + <!-- 阿里数据库连接池 -->
  33 + <dependency>
  34 + <groupId>com.alibaba</groupId>
  35 + <artifactId>druid-spring-boot-starter</artifactId>
  36 + </dependency>
  37 +
38 </dependencies> 38 </dependencies>
39 39
40 </project> 40 </project>
1 package com.ruoyi.generator.controller; 1 package com.ruoyi.generator.controller;
2 2
3 import java.io.IOException; 3 import java.io.IOException;
  4 +import java.util.ArrayList;
4 import java.util.HashMap; 5 import java.util.HashMap;
5 import java.util.List; 6 import java.util.List;
6 import java.util.Map; 7 import java.util.Map;
@@ -17,12 +18,18 @@ import org.springframework.web.bind.annotation.PutMapping; @@ -17,12 +18,18 @@ import org.springframework.web.bind.annotation.PutMapping;
17 import org.springframework.web.bind.annotation.RequestBody; 18 import org.springframework.web.bind.annotation.RequestBody;
18 import org.springframework.web.bind.annotation.RequestMapping; 19 import org.springframework.web.bind.annotation.RequestMapping;
19 import org.springframework.web.bind.annotation.RestController; 20 import org.springframework.web.bind.annotation.RestController;
  21 +import com.alibaba.druid.DbType;
  22 +import com.alibaba.druid.sql.SQLUtils;
  23 +import com.alibaba.druid.sql.ast.SQLStatement;
  24 +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement;
20 import com.ruoyi.common.annotation.Log; 25 import com.ruoyi.common.annotation.Log;
21 import com.ruoyi.common.core.controller.BaseController; 26 import com.ruoyi.common.core.controller.BaseController;
22 import com.ruoyi.common.core.domain.AjaxResult; 27 import com.ruoyi.common.core.domain.AjaxResult;
23 import com.ruoyi.common.core.page.TableDataInfo; 28 import com.ruoyi.common.core.page.TableDataInfo;
24 import com.ruoyi.common.core.text.Convert; 29 import com.ruoyi.common.core.text.Convert;
25 import com.ruoyi.common.enums.BusinessType; 30 import com.ruoyi.common.enums.BusinessType;
  31 +import com.ruoyi.common.utils.SecurityUtils;
  32 +import com.ruoyi.common.utils.sql.SqlUtil;
26 import com.ruoyi.generator.domain.GenTable; 33 import com.ruoyi.generator.domain.GenTable;
27 import com.ruoyi.generator.domain.GenTableColumn; 34 import com.ruoyi.generator.domain.GenTableColumn;
28 import com.ruoyi.generator.service.IGenTableColumnService; 35 import com.ruoyi.generator.service.IGenTableColumnService;
@@ -109,11 +116,48 @@ public class GenController extends BaseController @@ -109,11 +116,48 @@ public class GenController extends BaseController
109 String[] tableNames = Convert.toStrArray(tables); 116 String[] tableNames = Convert.toStrArray(tables);
110 // 查询表信息 117 // 查询表信息
111 List<GenTable> tableList = genTableService.selectDbTableListByNames(tableNames); 118 List<GenTable> tableList = genTableService.selectDbTableListByNames(tableNames);
112 - genTableService.importGenTable(tableList); 119 + genTableService.importGenTable(tableList, SecurityUtils.getUsername());
113 return success(); 120 return success();
114 } 121 }
115 122
116 /** 123 /**
  124 + * 创建表结构(保存)
  125 + */
  126 + @PreAuthorize("@ss.hasRole('admin')")
  127 + @Log(title = "创建表", businessType = BusinessType.OTHER)
  128 + @PostMapping("/createTable")
  129 + public AjaxResult createTableSave(String sql)
  130 + {
  131 + try
  132 + {
  133 + SqlUtil.filterKeyword(sql);
  134 + List<SQLStatement> sqlStatements = SQLUtils.parseStatements(sql, DbType.mysql);
  135 + List<String> tableNames = new ArrayList<>();
  136 + for (SQLStatement sqlStatement : sqlStatements)
  137 + {
  138 + if (sqlStatement instanceof MySqlCreateTableStatement)
  139 + {
  140 + MySqlCreateTableStatement createTableStatement = (MySqlCreateTableStatement) sqlStatement;
  141 + if (genTableService.createTable(createTableStatement.toString()))
  142 + {
  143 + String tableName = createTableStatement.getTableName().replaceAll("`", "");
  144 + tableNames.add(tableName);
  145 + }
  146 + }
  147 + }
  148 + List<GenTable> tableList = genTableService.selectDbTableListByNames(tableNames.toArray(new String[tableNames.size()]));
  149 + String operName = SecurityUtils.getUsername();
  150 + genTableService.importGenTable(tableList, operName);
  151 + return AjaxResult.success();
  152 + }
  153 + catch (Exception e)
  154 + {
  155 + logger.error(e.getMessage(), e);
  156 + return AjaxResult.error("创建表结构异常");
  157 + }
  158 + }
  159 +
  160 + /**
117 * 修改保存代码生成业务 161 * 修改保存代码生成业务
118 */ 162 */
119 @PreAuthorize("@ss.hasPermi('tool:gen:edit')") 163 @PreAuthorize("@ss.hasPermi('tool:gen:edit')")
@@ -80,4 +80,12 @@ public interface GenTableMapper @@ -80,4 +80,12 @@ public interface GenTableMapper
80 * @return 结果 80 * @return 结果
81 */ 81 */
82 public int deleteGenTableByIds(Long[] ids); 82 public int deleteGenTableByIds(Long[] ids);
  83 +
  84 + /**
  85 + * 创建表
  86 + *
  87 + * @param sql 表结构
  88 + * @return 结果
  89 + */
  90 + public int createTable(String sql);
83 } 91 }
@@ -27,7 +27,6 @@ import com.ruoyi.common.constant.Constants; @@ -27,7 +27,6 @@ import com.ruoyi.common.constant.Constants;
27 import com.ruoyi.common.constant.GenConstants; 27 import com.ruoyi.common.constant.GenConstants;
28 import com.ruoyi.common.core.text.CharsetKit; 28 import com.ruoyi.common.core.text.CharsetKit;
29 import com.ruoyi.common.exception.ServiceException; 29 import com.ruoyi.common.exception.ServiceException;
30 -import com.ruoyi.common.utils.SecurityUtils;  
31 import com.ruoyi.common.utils.StringUtils; 30 import com.ruoyi.common.utils.StringUtils;
32 import com.ruoyi.generator.domain.GenTable; 31 import com.ruoyi.generator.domain.GenTable;
33 import com.ruoyi.generator.domain.GenTableColumn; 32 import com.ruoyi.generator.domain.GenTableColumn;
@@ -151,15 +150,26 @@ public class GenTableServiceImpl implements IGenTableService @@ -151,15 +150,26 @@ public class GenTableServiceImpl implements IGenTableService
151 } 150 }
152 151
153 /** 152 /**
  153 + * 创建表
  154 + *
  155 + * @param sql 创建表语句
  156 + * @return 结果
  157 + */
  158 + @Override
  159 + public boolean createTable(String sql)
  160 + {
  161 + return genTableMapper.createTable(sql) == 0;
  162 + }
  163 +
  164 + /**
154 * 导入表结构 165 * 导入表结构
155 * 166 *
156 * @param tableList 导入表列表 167 * @param tableList 导入表列表
157 */ 168 */
158 @Override 169 @Override
159 @Transactional 170 @Transactional
160 - public void importGenTable(List<GenTable> tableList) 171 + public void importGenTable(List<GenTable> tableList, String operName)
161 { 172 {
162 - String operName = SecurityUtils.getUsername();  
163 try 173 try
164 { 174 {
165 for (GenTable table : tableList) 175 for (GenTable table : tableList)
@@ -67,11 +67,20 @@ public interface IGenTableService @@ -67,11 +67,20 @@ public interface IGenTableService
67 public void deleteGenTableByIds(Long[] tableIds); 67 public void deleteGenTableByIds(Long[] tableIds);
68 68
69 /** 69 /**
  70 + * 创建表
  71 + *
  72 + * @param sql 创建表语句
  73 + * @return 结果
  74 + */
  75 + public boolean createTable(String sql);
  76 +
  77 + /**
70 * 导入表结构 78 * 导入表结构
71 - * 79 + *
72 * @param tableList 导入表列表 80 * @param tableList 导入表列表
  81 + * @param operName 操作人员
73 */ 82 */
74 - public void importGenTable(List<GenTable> tableList); 83 + public void importGenTable(List<GenTable> tableList, String operName);
75 84
76 /** 85 /**
77 * 预览代码 86 * 预览代码
@@ -171,6 +171,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" @@ -171,6 +171,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
171 ) 171 )
172 </insert> 172 </insert>
173 173
  174 + <update id="createTable">
  175 + ${sql}
  176 + </update>
  177 +
174 <update id="updateGenTable" parameterType="GenTable"> 178 <update id="updateGenTable" parameterType="GenTable">
175 update gen_table 179 update gen_table
176 <set> 180 <set>
@@ -43,6 +43,15 @@ export function importTable(data) { @@ -43,6 +43,15 @@ export function importTable(data) {
43 }) 43 })
44 } 44 }
45 45
  46 +// 创建表
  47 +export function createTable(data) {
  48 + return request({
  49 + url: '/tool/gen/createTable',
  50 + method: 'post',
  51 + params: data
  52 + })
  53 +}
  54 +
46 // 预览生成代码 55 // 预览生成代码
47 export function previewTable(tableId) { 56 export function previewTable(tableId) {
48 return request({ 57 return request({
  1 +<template>
  2 + <!-- 创建表 -->
  3 + <el-dialog title="创建表" :visible.sync="visible" width="800px" top="5vh" append-to-body>
  4 + <span>创建表语句(支持多个建表语句):</span>
  5 + <el-input type="textarea" :rows="10" placeholder="请输入文本" v-model="content"></el-input>
  6 + <div slot="footer" class="dialog-footer">
  7 + <el-button type="primary" @click="handleCreateTable">确 定</el-button>
  8 + <el-button @click="visible = false">取 消</el-button>
  9 + </div>
  10 + </el-dialog>
  11 +</template>
  12 +
  13 +<script>
  14 +import { createTable } from "@/api/tool/gen";
  15 +export default {
  16 + data() {
  17 + return {
  18 + // 遮罩层
  19 + visible: false,
  20 + // 文本内容
  21 + content: ""
  22 + };
  23 + },
  24 + methods: {
  25 + // 显示弹框
  26 + show() {
  27 + this.visible = true;
  28 + },
  29 + /** 创建按钮操作 */
  30 + handleCreateTable() {
  31 + if (this.content === "") {
  32 + this.$modal.msgError("请输入建表语句");
  33 + return;
  34 + }
  35 + createTable({ sql: this.content }).then(res => {
  36 + this.$modal.msgSuccess(res.msg);
  37 + if (res.code === 200) {
  38 + this.visible = false;
  39 + this.$emit("ok");
  40 + }
  41 + });
  42 + }
  43 + }
  44 +};
  45 +</script>
@@ -41,12 +41,23 @@ @@ -41,12 +41,23 @@
41 plain 41 plain
42 icon="el-icon-download" 42 icon="el-icon-download"
43 size="mini" 43 size="mini"
  44 + :disabled="multiple"
44 @click="handleGenTable" 45 @click="handleGenTable"
45 v-hasPermi="['tool:gen:code']" 46 v-hasPermi="['tool:gen:code']"
46 >生成</el-button> 47 >生成</el-button>
47 </el-col> 48 </el-col>
48 <el-col :span="1.5"> 49 <el-col :span="1.5">
49 <el-button 50 <el-button
  51 + type="primary"
  52 + plain
  53 + icon="el-icon-plus"
  54 + size="mini"
  55 + @click="openCreateTable"
  56 + v-hasRole="['admin']"
  57 + >创建</el-button>
  58 + </el-col>
  59 + <el-col :span="1.5">
  60 + <el-button
50 type="info" 61 type="info"
51 plain 62 plain
52 icon="el-icon-upload" 63 icon="el-icon-upload"
@@ -172,12 +183,14 @@ @@ -172,12 +183,14 @@
172 </el-tabs> 183 </el-tabs>
173 </el-dialog> 184 </el-dialog>
174 <import-table ref="import" @ok="handleQuery" /> 185 <import-table ref="import" @ok="handleQuery" />
  186 + <create-table ref="create" @ok="handleQuery" />
175 </div> 187 </div>
176 </template> 188 </template>
177 189
178 <script> 190 <script>
179 import { listTable, previewTable, delTable, genCode, synchDb } from "@/api/tool/gen"; 191 import { listTable, previewTable, delTable, genCode, synchDb } from "@/api/tool/gen";
180 import importTable from "./importTable"; 192 import importTable from "./importTable";
  193 +import createTable from "./createTable";
181 import hljs from "highlight.js/lib/highlight"; 194 import hljs from "highlight.js/lib/highlight";
182 import "highlight.js/styles/github-gist.css"; 195 import "highlight.js/styles/github-gist.css";
183 hljs.registerLanguage("java", require("highlight.js/lib/languages/java")); 196 hljs.registerLanguage("java", require("highlight.js/lib/languages/java"));
@@ -189,7 +202,7 @@ hljs.registerLanguage("sql", require("highlight.js/lib/languages/sql")); @@ -189,7 +202,7 @@ hljs.registerLanguage("sql", require("highlight.js/lib/languages/sql"));
189 202
190 export default { 203 export default {
191 name: "Gen", 204 name: "Gen",
192 - components: { importTable }, 205 + components: { importTable, createTable },
193 data() { 206 data() {
194 return { 207 return {
195 // 遮罩层 208 // 遮罩层
@@ -283,6 +296,10 @@ export default { @@ -283,6 +296,10 @@ export default {
283 openImportTable() { 296 openImportTable() {
284 this.$refs.import.show(); 297 this.$refs.import.show();
285 }, 298 },
  299 + /** 打开创建表弹窗 */
  300 + openCreateTable() {
  301 + this.$refs.create.show();
  302 + },
286 /** 重置按钮操作 */ 303 /** 重置按钮操作 */
287 resetQuery() { 304 resetQuery() {
288 this.dateRange = []; 305 this.dateRange = [];