Browse Source

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

pull/334/head
RuoYi 3 years ago
parent
commit
54d338807e
  1. 47
      ruoyi-generator/src/main/java/com/ruoyi/generator/controller/GenController.java
  2. 2
      ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/GenTableMapper.java
  3. 9
      ruoyi-generator/src/main/java/com/ruoyi/generator/service/impl/GenTableServiceImpl.java
  4. 2
      ruoyi-generator/src/main/resources/mapper/generator/GenTableMapper.xml
  5. 58
      ruoyi-generator/src/main/resources/templates/tool/gen/createTable.html

47
ruoyi-generator/src/main/java/com/ruoyi/generator/controller/GenController.java

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

2
ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/GenTableMapper.java

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

9
ruoyi-generator/src/main/java/com/ruoyi/generator/service/impl/GenTableServiceImpl.java

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

2
ruoyi-generator/src/main/resources/mapper/generator/GenTableMapper.xml

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

58
src/main/resources/templates/tool/gen/createTable.html → ruoyi-generator/src/main/resources/templates/tool/gen/createTable.html

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