优化代码生成创建表结构功能
parent
4b6ed674b3
commit
54d338807e
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -85,7 +85,7 @@ public interface GenTableMapper
|
||||||
* 创建表
|
* 创建表
|
||||||
*
|
*
|
||||||
* @param sql
|
* @param sql
|
||||||
* @return
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
public int createTable(String sql);
|
public int createTable(String sql);
|
||||||
}
|
}
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue