数据字典API新增

This commit is contained in:
2026-04-24 22:07:59 +08:00
parent 564acbc91e
commit e92aea30bf
2 changed files with 56 additions and 6 deletions

View File

@@ -79,7 +79,7 @@ public class MysqlUtils {
/**
* 获取指定数据库下的所有表信息(包含字段)
*/
private static List<DataTable> getTablesByDatabase(Connection conn, String dbName) throws SQLException {
public static List<DataTable> getTablesByDatabase(Connection conn, String dbName) throws SQLException {
List<DataTable> tableInfos = new ArrayList<>();
String tableSql = "SELECT " +
"TABLE_NAME, TABLE_COMMENT, CREATE_TIME, UPDATE_TIME, " +
@@ -118,7 +118,7 @@ public class MysqlUtils {
/**
* 获取指定表的字段信息
*/
private static List<DataColumn> getFieldsByTable(Connection conn, String dbName, String tableName) throws SQLException {
public static List<DataColumn> getFieldsByTable(Connection conn, String dbName, String tableName) throws SQLException {
List<DataColumn> columns = new ArrayList<>();
String fieldSql = "SELECT " +
"TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,DATA_TYPE, COLUMN_TYPE, COLUMN_COMMENT, " +
@@ -175,14 +175,14 @@ public class MysqlUtils {
return null;
}
public static List<TableTree> getTableTrees(MyDataSource dbConfig) {
public static List<TableTree> getTableTrees(MyDataSource dataSource) {
List<TableTree> tableTrees = new ArrayList<>();
try {
Connection conn = getConnection(dbConfig);
Connection conn = getConnection(dataSource);
Map<String, List<DataTable>> schemaInfo = MysqlUtils.getMysqlSchemaInfo(conn);
for (Map.Entry<String, List<DataTable>> entry : schemaInfo.entrySet()) {
for (DataTable dataTable : entry.getValue()) {
dataTable.setSourceId(dbConfig.getId());
dataTable.setSourceId(dataSource.getId());
List<DataColumn> dataTableFields = getFieldsByTable(conn, entry.getKey(), dataTable.getTableName());
tableTrees.add(new TableTree(dataTable, dataTableFields));
}

View File

@@ -1,11 +1,17 @@
package com.jeesite.modules.apps.web.mysql;
import com.jeesite.modules.apps.Module.Dict.DataColumn;
import com.jeesite.modules.apps.Module.Dict.DataSource;
import com.jeesite.modules.apps.Module.Dict.DataTable;
import com.jeesite.modules.apps.Module.Dict.TableTree;
import com.jeesite.modules.apps.utils.MysqlUtils;
import com.jeesite.modules.apps.utils.SqlUtils;
import com.jeesite.modules.biz.entity.MyDataSource;
import com.jeesite.modules.biz.service.MyDataSourceService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.sql.Connection;
import java.util.ArrayList;
@@ -17,7 +23,8 @@ public class DataSourceController {
@Resource
private MyDataSourceService myDataSourceService;
@RequestMapping(value = {"listAll", ""})
@RequestMapping(value = "listAll")
@ResponseBody
public List<DataSource> listAll(MyDataSource myDataSource) throws Exception {
List<DataSource> sourceList = new ArrayList<>();
MyDataSource source = myDataSourceService.get(myDataSource);
@@ -28,4 +35,47 @@ public class DataSourceController {
}
return sourceList;
}
@RequestMapping(value = "listTable")
@ResponseBody
public List<DataTable> listTable(DataTable dataTable) throws Exception {
MyDataSource source = myDataSourceService.get(dataTable.getSourceId());
Connection conn = MysqlUtils.getConnection(source);
return MysqlUtils.getTablesByDatabase(conn, source.getDbName());
}
@RequestMapping(value = "listColumn")
@ResponseBody
public List<DataColumn> listColumn(DataTable dataTable) throws Exception {
MyDataSource source = myDataSourceService.get(dataTable.getSourceId());
Connection conn = MysqlUtils.getConnection(source);
return MysqlUtils.getFieldsByTable(conn, dataTable.getDataSource(), dataTable.getTableName());
}
@RequestMapping(value = "createTable")
@ResponseBody
public String createTable(DataTable dataTable) throws Exception {
MyDataSource source = myDataSourceService.get(dataTable.getSourceId());
Connection conn = MysqlUtils.getConnection(source);
List<DataColumn> dataColumns = MysqlUtils.getFieldsByTable(conn, dataTable.getDataSource(), dataTable.getTableName());
return SqlUtils.CreateTableSql(dataTable, dataColumns);
}
@RequestMapping(value = "selectTable")
@ResponseBody
public String selectTable(DataTable dataTable) throws Exception {
MyDataSource source = myDataSourceService.get(dataTable.getSourceId());
Connection conn = MysqlUtils.getConnection(source);
List<DataColumn> dataColumns = MysqlUtils.getFieldsByTable(conn, dataTable.getDataSource(), dataTable.getTableName());
return SqlUtils.SelectSqlComments(dataTable, dataColumns);
}
@RequestMapping(value = "listTree")
@ResponseBody
public List<TableTree> listTree(MyDataSource myDataSource) {
MyDataSource source = myDataSourceService.get(myDataSource);
return MysqlUtils.getTableTrees(source);
}
}