修改
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package com.jeesite.modules.app.Job;
|
||||
|
||||
import com.jeesite.modules.app.dao.TableTree;
|
||||
import com.jeesite.modules.app.utils.LoggerUtils;
|
||||
import com.jeesite.modules.app.utils.MysqlUtils;
|
||||
import com.jeesite.modules.biz.entity.BizDbConfig;
|
||||
import com.jeesite.modules.biz.entity.BizTableField;
|
||||
import com.jeesite.modules.biz.entity.BizTableInfo;
|
||||
import com.jeesite.modules.biz.service.BizDbConfigService;
|
||||
import com.jeesite.modules.biz.service.BizTableFieldService;
|
||||
import com.jeesite.modules.biz.service.BizTableInfoService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Controller
|
||||
public class dataJob {
|
||||
|
||||
|
||||
@Resource
|
||||
private BizDbConfigService dbConfigService;
|
||||
@Resource
|
||||
private BizTableInfoService tableInfoService;
|
||||
@Resource
|
||||
private BizTableFieldService tableFieldService;
|
||||
|
||||
|
||||
@Resource(name = "hostMonitorExecutor")
|
||||
private ThreadPoolTaskExecutor hostMonitorExecutor;
|
||||
|
||||
|
||||
private static final LoggerUtils logger = LoggerUtils.getInstance();
|
||||
|
||||
/**
|
||||
* 数据字典更新
|
||||
*/
|
||||
@Scheduled(cron = "10 30 2 * * ?")
|
||||
public void getDataManager() {
|
||||
BizDbConfig dbConfig = new BizDbConfig();
|
||||
dbConfig.setIsEnabled("1");
|
||||
List<BizDbConfig> configList = dbConfigService.findList(dbConfig);
|
||||
List<CompletableFuture<Void>> futures = new ArrayList<>(configList.size());
|
||||
for (BizDbConfig config : configList) {
|
||||
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
List<TableTree> tableTrees = MysqlUtils.getTableTrees(config);
|
||||
for (TableTree tableTree : tableTrees) {
|
||||
BizTableInfo tableInfo = tableTree.getTableInfo();
|
||||
tableInfoService.insert(tableInfo);
|
||||
List<BizTableField> tableFields = tableTree.getTableFields();
|
||||
for (BizTableField tableField : tableFields) {
|
||||
tableFieldService.insert(tableField);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage());
|
||||
}
|
||||
}, hostMonitorExecutor); // 指定使用配置的线程池
|
||||
futures.add(future);
|
||||
}
|
||||
try {
|
||||
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
|
||||
.get(30, TimeUnit.SECONDS);
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,6 @@ import com.jeesite.modules.app.utils.LoggerUtils;
|
||||
import com.jeesite.modules.app.utils.NetworkUtils;
|
||||
import com.jeesite.modules.biz.service.BizServerInfoService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
import org.springframework.stereotype.Controller;
|
||||
@@ -24,7 +23,6 @@ import org.springframework.stereotype.Controller;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Controller
|
||||
public class hostJob {
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
package com.jeesite.modules.app.dao;
|
||||
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 数据表字段信息表
|
||||
* </p>
|
||||
*
|
||||
* @author gaoxq
|
||||
* @since 2025-11-17
|
||||
*/
|
||||
@Data
|
||||
public class DataTableField implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private LocalDateTime createTime;
|
||||
|
||||
/**
|
||||
* 字段唯一标识(主键)
|
||||
*/
|
||||
private String fieldId;
|
||||
|
||||
/**
|
||||
* 关联的数据表ID(外键,关联data_table_info.table_id)
|
||||
*/
|
||||
private String tableId;
|
||||
|
||||
/**
|
||||
* 数据来源
|
||||
*/
|
||||
private String dataSource;
|
||||
|
||||
/**
|
||||
* 数据表名称
|
||||
*/
|
||||
private String dataName;
|
||||
|
||||
/**
|
||||
* 字段序号(表示字段在表中的顺序)
|
||||
*/
|
||||
private Integer fieldOrder;
|
||||
|
||||
/**
|
||||
* 字段类型(如:int、varchar、datetime等)
|
||||
*/
|
||||
private String fieldType;
|
||||
|
||||
/**
|
||||
* 字段名称
|
||||
*/
|
||||
private String fieldName;
|
||||
|
||||
/**
|
||||
* 字段长度(如varchar(50)中的50,数值型可表示精度)
|
||||
*/
|
||||
private Long fieldLength;
|
||||
|
||||
/**
|
||||
* 字段说明
|
||||
*/
|
||||
private String fieldRemark;
|
||||
|
||||
/**
|
||||
* 分区日期
|
||||
*/
|
||||
private String ds;
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
package com.jeesite.modules.app.dao;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 数据表基础信息表
|
||||
* </p>
|
||||
*
|
||||
* @author gaoxq
|
||||
* @since 2025-11-17
|
||||
*/
|
||||
@Data
|
||||
public class DataTableInfo implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private String createTime;
|
||||
|
||||
/**
|
||||
* 数据表唯一标识(主键)
|
||||
*/
|
||||
private String tableId;
|
||||
|
||||
/**
|
||||
* 数据表名称
|
||||
*/
|
||||
private String dataName;
|
||||
|
||||
/**
|
||||
* 数据表描述
|
||||
*/
|
||||
private String tableComment;
|
||||
|
||||
/**
|
||||
* 数据表大小(单位:MB)
|
||||
*/
|
||||
private BigDecimal tableSize;
|
||||
|
||||
/**
|
||||
* 数据来源(如:业务系统、文件导入等)
|
||||
*/
|
||||
private String dataSource;
|
||||
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
private String creator;
|
||||
|
||||
/**
|
||||
* 记录条数
|
||||
*/
|
||||
private Long dataRows;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
private String updateTime;
|
||||
|
||||
/**
|
||||
* 备注信息
|
||||
*/
|
||||
private String remarks;
|
||||
|
||||
/**
|
||||
* 数据标识
|
||||
*/
|
||||
private String dbId;
|
||||
|
||||
/**
|
||||
* 分区日期
|
||||
*/
|
||||
private String ds;
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.jeesite.modules.app.dao;
|
||||
|
||||
|
||||
import com.jeesite.modules.biz.entity.BizTableField;
|
||||
import com.jeesite.modules.biz.entity.BizTableInfo;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
@@ -9,14 +11,14 @@ import java.util.List;
|
||||
@Data
|
||||
public class TableTree implements Serializable {
|
||||
|
||||
DataTableInfo tableInfo;
|
||||
BizTableInfo tableInfo;
|
||||
|
||||
List<DataTableField> tableFields;
|
||||
List<BizTableField> tableFields;
|
||||
|
||||
public TableTree() {
|
||||
}
|
||||
|
||||
public TableTree(DataTableInfo tableInfo, List<DataTableField> tableFields) {
|
||||
public TableTree(BizTableInfo tableInfo, List<BizTableField> tableFields) {
|
||||
this.tableInfo = tableInfo;
|
||||
this.tableFields = tableFields;
|
||||
}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
package com.jeesite.modules.app.utils;
|
||||
|
||||
|
||||
import com.jeesite.modules.app.dao.DataTableField;
|
||||
import com.jeesite.modules.app.dao.DataTableInfo;
|
||||
import com.jeesite.modules.app.dao.TableTree;
|
||||
import com.jeesite.modules.biz.entity.BizDbConfig;
|
||||
import com.jeesite.modules.biz.entity.BizTableField;
|
||||
import com.jeesite.modules.biz.entity.BizTableInfo;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.sql.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
import java.util.Date;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -45,14 +45,14 @@ public class MysqlUtils {
|
||||
* @return 数据库名 -> 表信息列表(包含字段)的映射
|
||||
* @throws Exception 连接或查询异常
|
||||
*/
|
||||
public static Map<String, List<DataTableInfo>> getMysqlSchemaInfo(Connection conn) throws Exception {
|
||||
Map<String, List<DataTableInfo>> result = new HashMap<>();
|
||||
public static Map<String, List<BizTableInfo>> getMysqlSchemaInfo(Connection conn) throws Exception {
|
||||
Map<String, List<BizTableInfo>> result = new HashMap<>();
|
||||
// 1. 获取所有非系统数据库
|
||||
List<String> databases = getNonSystemDatabases(conn);
|
||||
logger.info("获取到非系统数据库数量:", databases.size());
|
||||
// 2. 遍历数据库,获取表和字段信息
|
||||
for (String dbName : databases) {
|
||||
List<DataTableInfo> tableInfos = getTablesByDatabase(conn, dbName);
|
||||
List<BizTableInfo> tableInfos = getTablesByDatabase(conn, dbName);
|
||||
result.put(dbName, tableInfos);
|
||||
}
|
||||
return result;
|
||||
@@ -81,8 +81,8 @@ public class MysqlUtils {
|
||||
/**
|
||||
* 获取指定数据库下的所有表信息(包含字段)
|
||||
*/
|
||||
private static List<DataTableInfo> getTablesByDatabase(Connection conn, String dbName) throws SQLException {
|
||||
List<DataTableInfo> tableInfos = new ArrayList<>();
|
||||
private static List<BizTableInfo> getTablesByDatabase(Connection conn, String dbName) throws SQLException {
|
||||
List<BizTableInfo> tableInfos = new ArrayList<>();
|
||||
String tableSql = "SELECT " +
|
||||
"TABLE_NAME, TABLE_COMMENT, CREATE_TIME, UPDATE_TIME, " +
|
||||
"DATA_LENGTH, INDEX_LENGTH, TABLE_ROWS " +
|
||||
@@ -91,8 +91,8 @@ public class MysqlUtils {
|
||||
tablePs.setString(1, dbName);
|
||||
try (ResultSet tableRs = tablePs.executeQuery()) {
|
||||
while (tableRs.next()) {
|
||||
DataTableInfo tableInfo = buildDataTableInfo(tableRs, dbName);
|
||||
List<DataTableField> fields = getFieldsByTable(conn, dbName, tableInfo.getDataName());
|
||||
BizTableInfo tableInfo = buildDataTableInfo(tableRs, dbName);
|
||||
List<BizTableField> fields = getFieldsByTable(conn, dbName, tableInfo.getDataName());
|
||||
fields.forEach(field -> field.setTableId(tableInfo.getTableId()));
|
||||
tableInfos.add(tableInfo);
|
||||
}
|
||||
@@ -104,8 +104,8 @@ public class MysqlUtils {
|
||||
/**
|
||||
* 构建DataTableInfo实体
|
||||
*/
|
||||
private static DataTableInfo buildDataTableInfo(ResultSet tableRs, String dbName) throws SQLException {
|
||||
DataTableInfo tableInfo = new DataTableInfo();
|
||||
private static BizTableInfo buildDataTableInfo(ResultSet tableRs, String dbName) throws SQLException {
|
||||
BizTableInfo tableInfo = new BizTableInfo();
|
||||
tableInfo.setTableId(vId.getUid());
|
||||
tableInfo.setDataName(tableRs.getString("TABLE_NAME"));
|
||||
tableInfo.setTableComment(tableRs.getString("TABLE_COMMENT"));
|
||||
@@ -127,8 +127,8 @@ public class MysqlUtils {
|
||||
/**
|
||||
* 获取指定表的字段信息
|
||||
*/
|
||||
private static List<DataTableField> getFieldsByTable(Connection conn, String dbName, String tableName) throws SQLException {
|
||||
List<DataTableField> fields = new ArrayList<>();
|
||||
private static List<BizTableField> getFieldsByTable(Connection conn, String dbName, String tableName) throws SQLException {
|
||||
List<BizTableField> fields = new ArrayList<>();
|
||||
String fieldSql = "SELECT " +
|
||||
"TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,DATA_TYPE, COLUMN_TYPE, COLUMN_COMMENT, " +
|
||||
"ORDINAL_POSITION, CHARACTER_MAXIMUM_LENGTH " +
|
||||
@@ -149,8 +149,8 @@ public class MysqlUtils {
|
||||
/**
|
||||
* 构建DataTableField实体
|
||||
*/
|
||||
private static DataTableField buildDataTableField(ResultSet fieldRs) throws SQLException {
|
||||
DataTableField field = new DataTableField();
|
||||
private static BizTableField buildDataTableField(ResultSet fieldRs) throws SQLException {
|
||||
BizTableField field = new BizTableField();
|
||||
field.setFieldId(vId.getUid());
|
||||
field.setDataSource(fieldRs.getString("TABLE_SCHEMA"));
|
||||
field.setDataName(fieldRs.getString("TABLE_NAME"));
|
||||
@@ -163,7 +163,7 @@ public class MysqlUtils {
|
||||
length = extractLengthFromType(fieldRs.getString("COLUMN_TYPE"));
|
||||
}
|
||||
field.setFieldLength(length);
|
||||
field.setCreateTime(LocalDateTime.now());
|
||||
field.setCreateTime(new Date());
|
||||
field.setDs(vDate.dsValue());
|
||||
return field;
|
||||
}
|
||||
@@ -188,11 +188,11 @@ public class MysqlUtils {
|
||||
List<TableTree> tableTrees = new ArrayList<>();
|
||||
try {
|
||||
Connection conn = getConnection(dbConfig.getDbIp(), dbConfig.getDbPort(), dbConfig.getDbUsername(), dbConfig.getDbPassword());
|
||||
Map<String, List<DataTableInfo>> schemaInfo = MysqlUtils.getMysqlSchemaInfo(conn);
|
||||
for (Map.Entry<String, List<DataTableInfo>> entry : schemaInfo.entrySet()) {
|
||||
for (DataTableInfo tableInfo : entry.getValue()) {
|
||||
Map<String, List<BizTableInfo>> schemaInfo = MysqlUtils.getMysqlSchemaInfo(conn);
|
||||
for (Map.Entry<String, List<BizTableInfo>> entry : schemaInfo.entrySet()) {
|
||||
for (BizTableInfo tableInfo : entry.getValue()) {
|
||||
tableInfo.setDbId(dbConfig.getId());
|
||||
List<DataTableField> dataTableFields = getFieldsByTable(conn, entry.getKey(), tableInfo.getDataName());
|
||||
List<BizTableField> dataTableFields = getFieldsByTable(conn, entry.getKey(), tableInfo.getDataName());
|
||||
dataTableFields.stream().forEach(tableField -> tableField.setTableId(tableInfo.getTableId()));
|
||||
tableTrees.add(new TableTree(tableInfo, dataTableFields));
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.jeesite.modules.app.utils;
|
||||
|
||||
|
||||
import com.jeesite.modules.app.dao.DataTableField;
|
||||
import com.jeesite.modules.app.dao.DataTableInfo;
|
||||
import com.jeesite.modules.biz.entity.BizTableField;
|
||||
import com.jeesite.modules.biz.entity.BizTableInfo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -16,13 +16,13 @@ public class SqlUtils {
|
||||
* @param fieldList 字段列表
|
||||
* @return CREATE TABLE语句
|
||||
*/
|
||||
public static String CreateTableSql(DataTableInfo tableInfo, List<DataTableField> fieldList) {
|
||||
public static String CreateTableSql(BizTableInfo tableInfo, List<BizTableField> fieldList) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
// 表定义开始
|
||||
sb.append("CREATE TABLE ").append(tableInfo.getDataName()).append(" (\n");
|
||||
// 拼接字段定义
|
||||
for (int i = 0; i < fieldList.size(); i++) {
|
||||
DataTableField field = fieldList.get(i);
|
||||
BizTableField field = fieldList.get(i);
|
||||
sb.append(" ").append(field.getFieldName()).append(" ");
|
||||
// 处理字段类型和长度
|
||||
if (field.getFieldLength() != null && field.getFieldLength() > 0) {
|
||||
@@ -66,7 +66,7 @@ public class SqlUtils {
|
||||
* @param fieldList 字段列表
|
||||
* @return 带注释的SELECT语句
|
||||
*/
|
||||
public static String SelectSqlComments(DataTableInfo tableInfo, List<DataTableField> fieldList) {
|
||||
public static String SelectSqlComments(BizTableInfo tableInfo, List<BizTableField> fieldList) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
// 表注释
|
||||
sb.append("-- 表名:").append(tableInfo.getDataName()).append("\n");
|
||||
@@ -76,7 +76,7 @@ public class SqlUtils {
|
||||
sb.append("SELECT\n");
|
||||
// 拼接字段(带注释)
|
||||
for (int i = 0; i < fieldList.size(); i++) {
|
||||
DataTableField field = fieldList.get(i);
|
||||
BizTableField field = fieldList.get(i);
|
||||
// 拼接字段名
|
||||
sb.append(" ").append(field.getFieldName());
|
||||
// 非最后一个字段加逗号(先处理逗号)
|
||||
|
||||
Reference in New Issue
Block a user