优化代码生成创建表结构功能

master
RuoYi 2021-09-29 18:57:52 +08:00
parent 4b6ed674b3
commit 54d338807e
5 changed files with 66 additions and 52 deletions

View File

@ -7,6 +7,7 @@ import java.util.Map;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions; import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap; import org.springframework.ui.ModelMap;
@ -16,6 +17,10 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.Log; import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.controller.BaseController;
@ -30,11 +35,6 @@ import com.ruoyi.generator.domain.GenTable;
import com.ruoyi.generator.domain.GenTableColumn; import com.ruoyi.generator.domain.GenTableColumn;
import com.ruoyi.generator.service.IGenTableColumnService; import com.ruoyi.generator.service.IGenTableColumnService;
import com.ruoyi.generator.service.IGenTableService; import com.ruoyi.generator.service.IGenTableService;
import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement;
import com.alibaba.fastjson.JSON;
/** /**
* *
@ -114,10 +114,10 @@ public class GenController extends BaseController
/** /**
* *
*/ */
@GetMapping("/createTable") @GetMapping("/createTable")
public String createTable() { public String createTable()
return prefix + "/createTable" ; {
return prefix + "/createTable";
} }
/** /**
@ -132,7 +132,7 @@ public class GenController extends BaseController
String[] tableNames = Convert.toStrArray(tables); String[] tableNames = Convert.toStrArray(tables);
// 查询表信息 // 查询表信息
List<GenTable> tableList = genTableService.selectDbTableListByNames(tableNames); List<GenTable> tableList = genTableService.selectDbTableListByNames(tableNames);
String operName = (String) PermissionUtils.getPrincipalProperty("loginName"); String operName = Convert.toStr(PermissionUtils.getPrincipalProperty("loginName"));
genTableService.importGenTable(tableList, operName); genTableService.importGenTable(tableList, operName);
return AjaxResult.success(); return AjaxResult.success();
} }
@ -189,29 +189,36 @@ public class GenController extends BaseController
return AjaxResult.success(); return AjaxResult.success();
} }
@RequiresRoles("admin")
@Log(title = "创建表", businessType = BusinessType.OTHER) @Log(title = "创建表", businessType = BusinessType.OTHER)
@PostMapping("/createTable") @PostMapping("/createTable")
@ResponseBody @ResponseBody
public AjaxResult create(String sql) { public AjaxResult create(String sql)
{
List<SQLStatement> sqlStatements = SQLUtils.parseStatements(sql, DbType.mysql); List<SQLStatement> sqlStatements = SQLUtils.parseStatements(sql, DbType.mysql);
List<String> tableNames = new ArrayList<>(); List<String> tableNames = new ArrayList<>();
for (SQLStatement sqlStatement : sqlStatements) { for (SQLStatement sqlStatement : sqlStatements)
if (sqlStatement instanceof MySqlCreateTableStatement) { {
MySqlCreateTableStatement sqlStatement1 = (MySqlCreateTableStatement) sqlStatement; if (sqlStatement instanceof MySqlCreateTableStatement)
String tableName = sqlStatement1.getTableName(); {
MySqlCreateTableStatement createTableStatement = (MySqlCreateTableStatement) sqlStatement;
String tableName = createTableStatement.getTableName();
tableName = tableName.replaceAll("`", ""); tableName = tableName.replaceAll("`", "");
int msg = genTableService.createTable(sqlStatement1.toString()); int msg = genTableService.createTable(createTableStatement.toString());
if (msg == 0) { if (msg == 0)
{
tableNames.add(tableName); tableNames.add(tableName);
} }
} else { }
else
{
return AjaxResult.error("请输入建表语句"); return AjaxResult.error("请输入建表语句");
} }
} }
List<GenTable> tableList = genTableService.selectDbTableListByNames( List<GenTable> tableList = genTableService.selectDbTableListByNames((tableNames.toArray(new String[tableNames.size()])));
(tableNames.toArray(new String[tableNames.size()]))); String operName = Convert.toStr(PermissionUtils.getPrincipalProperty("loginName"));
genTableService.importGenTable(tableList, "admin"); genTableService.importGenTable(tableList, operName);
return AjaxResult.success(); return AjaxResult.success();
} }

View File

@ -85,7 +85,7 @@ public interface GenTableMapper
* *
* *
* @param sql * @param sql
* @return * @return
*/ */
public int createTable(String sql); public int createTable(String sql);
} }

View File

@ -150,8 +150,15 @@ public class GenTableServiceImpl implements IGenTableService
genTableColumnMapper.deleteGenTableColumnByIds(Convert.toLongArray(ids)); genTableColumnMapper.deleteGenTableColumnByIds(Convert.toLongArray(ids));
} }
/**
*
*
* @param sql
* @return
*/
@Override @Override
public int createTable(String sql) { public int createTable(String sql)
{
return genTableMapper.createTable(sql); return genTableMapper.createTable(sql);
} }

View File

@ -157,7 +157,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</insert> </insert>
<update id="createTable"> <update id="createTable">
${sql} ${sql}
</update> </update>
<update id="updateGenTable" parameterType="GenTable"> <update id="updateGenTable" parameterType="GenTable">

View File

@ -1,30 +1,30 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org"> <html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head> <head>
<th:block th:include="include :: header('创建表结构')"/> <th:block th:include="include :: header('创建表结构')"/>
</head> </head>
<body> <body>
<div class="main-content"> <div class="main-content">
<label class="col-sm-6 control-label">创建表语句(支持多个建表语句)</label> <label class="col-sm-6 control-label">创建表语句(支持多个建表语句)</label>
<div class="col-sm-11 col"> <div class="col-sm-11 col">
<textarea class="form-control" id="text_create" name="" placeholder="请输入文本" rows="12" type="text"></textarea> <textarea class="form-control" id="text_create" name="" placeholder="请输入文本" rows="12" type="text"></textarea>
</div> </div>
</div> </div>
<th:block th:include="include :: footer"/> <th:block th:include="include :: footer"/>
<script type="text/javascript"> <script type="text/javascript">
var prefix = ctx + "tool/gen"; var prefix = ctx + "tool/gen";
/* 创建表结构 */ /* 创建表结构 */
function submitHandler() { function submitHandler() {
var rows = $("#text_create").val(); var rows = $("#text_create").val();
if (rows.length == 0) { if (rows.length == 0) {
$.modal.alertWarning("请输入建表语句"); $.modal.alertWarning("请输入建表语句");
return; return;
} }
var data = {"sql": rows}; var data = {"sql": rows};
$.operate.save(prefix + "/createTable", data); $.operate.save(prefix + "/createTable", data);
} }
</script> </script>
</body> </body>
</html> </html>