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

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,9 +114,9 @@ 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);
} }