增加主机信息功能
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
package com.jeesite.modules.apps.Module.Dict;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
public class DataSource implements Serializable {
|
||||
|
||||
private String sourceId; // 主键ID
|
||||
private String sourceName; // 连接名称
|
||||
private String dbName; // 数据库名称
|
||||
|
||||
public DataSource() {
|
||||
}
|
||||
|
||||
public DataSource(String sourceId, String sourceName, String dbName) {
|
||||
this.sourceId = sourceId;
|
||||
this.sourceName = sourceName;
|
||||
this.dbName = dbName;
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,6 @@ import java.math.BigDecimal;
|
||||
public class DataTable implements Serializable {
|
||||
|
||||
private String createTime; // 创建时间
|
||||
private String tableId; // 表ID
|
||||
private String sourceId; // 数据源ID
|
||||
private String tableName; // 数据表名
|
||||
private String tableComment; // 表注释
|
||||
|
||||
@@ -10,7 +10,6 @@ import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.sql.*;
|
||||
import java.util.*;
|
||||
import java.util.Date;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -28,14 +27,14 @@ public class MysqlUtils {
|
||||
/**
|
||||
* 封装:获取MySQL数据库连接
|
||||
*/
|
||||
private static Connection getConnection(String ip, Integer port, String username, String password) throws Exception {
|
||||
public static Connection getConnection(MyDataSource dbConfig) throws Exception {
|
||||
String driver = "com.mysql.cj.jdbc.Driver";
|
||||
String jdbcUrl = String.format(
|
||||
"jdbc:mysql://%s:%d/information_schema?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true",
|
||||
ip, port
|
||||
dbConfig.getDbHost(), dbConfig.getDbPort()
|
||||
);
|
||||
Class.forName(driver); // 加载驱动
|
||||
return DriverManager.getConnection(jdbcUrl, username, password);
|
||||
return DriverManager.getConnection(jdbcUrl, dbConfig.getUsername(), dbConfig.getPassword());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,7 +59,7 @@ public class MysqlUtils {
|
||||
/**
|
||||
* 获取所有非系统数据库
|
||||
*/
|
||||
private static List<String> getNonSystemDatabases(Connection conn) throws SQLException {
|
||||
public static List<String> getNonSystemDatabases(Connection conn) throws SQLException {
|
||||
List<String> databases = new ArrayList<>();
|
||||
String sql = "SELECT SCHEMA_NAME FROM SCHEMATA WHERE SCHEMA_NAME NOT IN ("
|
||||
+ String.join(",", Collections.nCopies(SYSTEM_DATABASES.size(), "?")) + ")";
|
||||
@@ -91,8 +90,6 @@ public class MysqlUtils {
|
||||
try (ResultSet tableRs = tablePs.executeQuery()) {
|
||||
while (tableRs.next()) {
|
||||
DataTable tableInfo = buildDataTableInfo(tableRs, dbName);
|
||||
List<DataColumn> fields = getFieldsByTable(conn, dbName, tableInfo.getTableName());
|
||||
fields.forEach(field -> field.setTableId(tableInfo.getTableId()));
|
||||
tableInfos.add(tableInfo);
|
||||
}
|
||||
}
|
||||
@@ -105,21 +102,16 @@ public class MysqlUtils {
|
||||
*/
|
||||
private static DataTable buildDataTableInfo(ResultSet tableRs, String dbName) throws SQLException {
|
||||
DataTable dataTable = new DataTable();
|
||||
String runTime = DateUtils.getCurrentDateTime();
|
||||
String createDate = tableRs.getString("CREATE_TIME");
|
||||
String updateDate = tableRs.getString("UPDATE_TIME");
|
||||
long dataLength = tableRs.getLong("DATA_LENGTH");
|
||||
long indexLength = tableRs.getLong("INDEX_LENGTH");
|
||||
BigDecimal tableSize = BigDecimal.valueOf((dataLength + indexLength) / 1024.0 / 1024.0)
|
||||
.setScale(2, RoundingMode.HALF_UP);
|
||||
|
||||
dataTable.setTableId(KeyUtil.ObjKey(32, 0));
|
||||
dataTable.setTableRows(tableRs.getLong("TABLE_ROWS"));
|
||||
dataTable.setTableName(tableRs.getString("TABLE_NAME"));
|
||||
dataTable.setTableComment(tableRs.getString("TABLE_COMMENT"));
|
||||
dataTable.setTableSize(tableSize);
|
||||
dataTable.setDataSource(dbName);
|
||||
dataTable.setCreateTime(createDate != null ? createDate : runTime);
|
||||
return dataTable;
|
||||
}
|
||||
|
||||
@@ -186,7 +178,7 @@ public class MysqlUtils {
|
||||
public static List<TableTree> getTableTrees(MyDataSource dbConfig) {
|
||||
List<TableTree> tableTrees = new ArrayList<>();
|
||||
try {
|
||||
Connection conn = getConnection(dbConfig.getDbHost(), dbConfig.getDbPort(), dbConfig.getUsername(), dbConfig.getPassword());
|
||||
Connection conn = getConnection(dbConfig);
|
||||
Map<String, List<DataTable>> schemaInfo = MysqlUtils.getMysqlSchemaInfo(conn);
|
||||
for (Map.Entry<String, List<DataTable>> entry : schemaInfo.entrySet()) {
|
||||
for (DataTable dataTable : entry.getValue()) {
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.jeesite.modules.apps.web.mysql;
|
||||
|
||||
import com.jeesite.modules.apps.Module.Dict.DataSource;
|
||||
import com.jeesite.modules.apps.utils.MysqlUtils;
|
||||
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 java.sql.Connection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RequestMapping(value = "${adminPath}/mysql/myDataSource")
|
||||
public class DataSourceController {
|
||||
|
||||
@Resource
|
||||
private MyDataSourceService myDataSourceService;
|
||||
|
||||
@RequestMapping(value = {"listAll", ""})
|
||||
public List<DataSource> listAll(MyDataSource myDataSource) throws Exception {
|
||||
List<DataSource> sourceList = new ArrayList<>();
|
||||
MyDataSource source = myDataSourceService.get(myDataSource);
|
||||
Connection conn = MysqlUtils.getConnection(source);
|
||||
List<String> dataList = MysqlUtils.getNonSystemDatabases(conn);
|
||||
for (String data : dataList) {
|
||||
sourceList.add(new DataSource(source.getSourceId(), source.getSourceName(), data));
|
||||
}
|
||||
return sourceList;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user