From 25fe49383b93f27ea29dc67dfada9e8f3f89b4e7 Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Tue, 5 Aug 2025 15:19:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E6=8C=87=E5=AE=9A=E8=A1=A8?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E5=88=B0=E7=9B=AE=E6=A0=87=E7=AE=A1=E6=8E=A7?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E7=9A=84=E8=A1=A8=E4=B8=AD=20=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E6=8C=87=E5=AE=9A=E5=BA=93=E4=B8=AD=E6=89=80=E6=9C=89?= =?UTF-8?q?=E8=A1=A8=E7=BB=93=E6=9E=84=E5=88=B0=E7=9B=AE=E6=A0=87=E7=AE=A1?= =?UTF-8?q?=E6=8E=A7=E9=A1=B9=E7=9B=AE=E7=9A=84=E8=A1=A8=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 12 + .../iet/ustb/sf/IetKpiServiceApplication.java | 2 + .../ustb/sf/controller/SyncController.java | 54 ++++ src/main/java/iet/ustb/sf/domain/Columns.java | 238 ++++++++++++++++++ .../java/iet/ustb/sf/domain/TableColumn.java | 167 ++++++++++++ src/main/java/iet/ustb/sf/domain/Tables.java | 215 ++++++++++++++++ .../java/iet/ustb/sf/exception/ErrorCode.java | 3 +- .../iet/ustb/sf/mapper/ColumnsMapper.java | 18 ++ .../iet/ustb/sf/mapper/TableColumnMapper.java | 49 ++++ .../java/iet/ustb/sf/mapper/TablesMapper.java | 18 ++ .../iet/ustb/sf/service/ColumnsService.java | 13 + .../ustb/sf/service/TableColumnService.java | 16 ++ .../sf/service/impl/ColumnsServiceImpl.java | 22 ++ .../service/impl/TableColumnServiceImpl.java | 113 +++++++++ .../java/iet/ustb/sf/util/CheckUtils.java | 42 ++++ src/main/resources/application.yml | 11 +- .../iet/ustb/sf/mapper/ColumnsMapper.xml | 41 +++ .../iet/ustb/sf/mapper/TableColumnMapper.xml | 29 +++ .../iet/ustb/sf/mapper/TablesMapper.xml | 37 +++ 19 files changed, 1098 insertions(+), 2 deletions(-) create mode 100644 src/main/java/iet/ustb/sf/controller/SyncController.java create mode 100644 src/main/java/iet/ustb/sf/domain/Columns.java create mode 100644 src/main/java/iet/ustb/sf/domain/TableColumn.java create mode 100644 src/main/java/iet/ustb/sf/domain/Tables.java create mode 100644 src/main/java/iet/ustb/sf/mapper/ColumnsMapper.java create mode 100644 src/main/java/iet/ustb/sf/mapper/TableColumnMapper.java create mode 100644 src/main/java/iet/ustb/sf/mapper/TablesMapper.java create mode 100644 src/main/java/iet/ustb/sf/service/ColumnsService.java create mode 100644 src/main/java/iet/ustb/sf/service/TableColumnService.java create mode 100644 src/main/java/iet/ustb/sf/service/impl/ColumnsServiceImpl.java create mode 100644 src/main/java/iet/ustb/sf/service/impl/TableColumnServiceImpl.java create mode 100644 src/main/java/iet/ustb/sf/util/CheckUtils.java create mode 100644 src/main/resources/iet/ustb/sf/mapper/ColumnsMapper.xml create mode 100644 src/main/resources/iet/ustb/sf/mapper/TableColumnMapper.xml create mode 100644 src/main/resources/iet/ustb/sf/mapper/TablesMapper.xml diff --git a/pom.xml b/pom.xml index 94c452c..948323d 100644 --- a/pom.xml +++ b/pom.xml @@ -57,6 +57,18 @@ knife4j-openapi3-jakarta-spring-boot-starter 4.4.0 + + + com.baomidou + mybatis-plus-spring-boot3-starter + 3.5.12 + + + + com.alibaba + fastjson + 1.2.29 + diff --git a/src/main/java/iet/ustb/sf/IetKpiServiceApplication.java b/src/main/java/iet/ustb/sf/IetKpiServiceApplication.java index 18c0735..1624321 100644 --- a/src/main/java/iet/ustb/sf/IetKpiServiceApplication.java +++ b/src/main/java/iet/ustb/sf/IetKpiServiceApplication.java @@ -1,9 +1,11 @@ package iet.ustb.sf; +import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication +@MapperScan("iet.ustb.sf.mapper") public class IetKpiServiceApplication { public static void main(String[] args) { diff --git a/src/main/java/iet/ustb/sf/controller/SyncController.java b/src/main/java/iet/ustb/sf/controller/SyncController.java new file mode 100644 index 0000000..6d1a4ca --- /dev/null +++ b/src/main/java/iet/ustb/sf/controller/SyncController.java @@ -0,0 +1,54 @@ +package iet.ustb.sf.controller; + +import com.alibaba.fastjson.JSONObject; +import iet.ustb.sf.common.R; +import iet.ustb.sf.service.TableColumnService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.annotation.Resource; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * web中未使用的有用接口 + * SyncController + * + * @author huangge1199 + * @since 2025/8/5 12:37:22 + */ +@RestController +@RequestMapping("/sync") +@Tag(name = "web中未使用的有用接口") +public class SyncController { + + @Resource + private TableColumnService tableColumnService; + + /** + * 同步指定表结构 + * @param params 表名 + * @return 结果 + */ + @Operation(summary = "同步指定表结构") + @PostMapping("/syncTableData") + public R syncTableData(@RequestBody JSONObject params) { + String tableName = params.getString("tableName"); + String result = tableColumnService.syncTableData(tableName); + return R.ok(result); + } + + /** + * 同步指定库的所有表结构 + * @param params 库名 + * @return 结果 + */ + @Operation(summary = "同步指定库的所有表结构") + @PostMapping("/syncTableDataByDb") + public R syncTableDataByDb(@RequestBody JSONObject params) { + String dbName = params.getString("dbName"); + String result = tableColumnService.syncTableDataByDb(dbName); + return R.ok(result); + } +} diff --git a/src/main/java/iet/ustb/sf/domain/Columns.java b/src/main/java/iet/ustb/sf/domain/Columns.java new file mode 100644 index 0000000..1af754e --- /dev/null +++ b/src/main/java/iet/ustb/sf/domain/Columns.java @@ -0,0 +1,238 @@ +package iet.ustb.sf.domain; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +/** + * + * @TableName columns + */ +@TableName(value ="information_schema.columns") +@Data +public class Columns { + /** + * + */ + private String tableCatalog; + + /** + * + */ + private String tableSchema; + + /** + * + */ + private String tableName; + + /** + * + */ + private String columnName; + + /** + * + */ + private Long ordinalPosition; + + /** + * + */ + private String columnDefault; + + /** + * + */ + private String isNullable; + + /** + * + */ + private String dataType; + + /** + * + */ + private Long characterMaximumLength; + + /** + * + */ + private Long characterOctetLength; + + /** + * + */ + private Long numericPrecision; + + /** + * + */ + private Long numericScale; + + /** + * + */ + private Long datetimePrecision; + + /** + * + */ + private String characterSetName; + + /** + * + */ + private String collationName; + + /** + * + */ + private String columnType; + + /** + * + */ + private String columnKey; + + /** + * + */ + private String extra; + + /** + * + */ + private String privileges; + + /** + * + */ + private String columnComment; + + /** + * + */ + private Long columnSize; + + /** + * + */ + private Long decimalDigits; + + /** + * + */ + private String generationExpression; + + /** + * + */ + private Long srsId; + + @Override + public boolean equals(Object that) { + if (this == that) { + return true; + } + if (that == null) { + return false; + } + if (getClass() != that.getClass()) { + return false; + } + Columns other = (Columns) that; + return (this.getTableCatalog() == null ? other.getTableCatalog() == null : this.getTableCatalog().equals(other.getTableCatalog())) + && (this.getTableSchema() == null ? other.getTableSchema() == null : this.getTableSchema().equals(other.getTableSchema())) + && (this.getTableName() == null ? other.getTableName() == null : this.getTableName().equals(other.getTableName())) + && (this.getColumnName() == null ? other.getColumnName() == null : this.getColumnName().equals(other.getColumnName())) + && (this.getOrdinalPosition() == null ? other.getOrdinalPosition() == null : this.getOrdinalPosition().equals(other.getOrdinalPosition())) + && (this.getColumnDefault() == null ? other.getColumnDefault() == null : this.getColumnDefault().equals(other.getColumnDefault())) + && (this.getIsNullable() == null ? other.getIsNullable() == null : this.getIsNullable().equals(other.getIsNullable())) + && (this.getDataType() == null ? other.getDataType() == null : this.getDataType().equals(other.getDataType())) + && (this.getCharacterMaximumLength() == null ? other.getCharacterMaximumLength() == null : this.getCharacterMaximumLength().equals(other.getCharacterMaximumLength())) + && (this.getCharacterOctetLength() == null ? other.getCharacterOctetLength() == null : this.getCharacterOctetLength().equals(other.getCharacterOctetLength())) + && (this.getNumericPrecision() == null ? other.getNumericPrecision() == null : this.getNumericPrecision().equals(other.getNumericPrecision())) + && (this.getNumericScale() == null ? other.getNumericScale() == null : this.getNumericScale().equals(other.getNumericScale())) + && (this.getDatetimePrecision() == null ? other.getDatetimePrecision() == null : this.getDatetimePrecision().equals(other.getDatetimePrecision())) + && (this.getCharacterSetName() == null ? other.getCharacterSetName() == null : this.getCharacterSetName().equals(other.getCharacterSetName())) + && (this.getCollationName() == null ? other.getCollationName() == null : this.getCollationName().equals(other.getCollationName())) + && (this.getColumnType() == null ? other.getColumnType() == null : this.getColumnType().equals(other.getColumnType())) + && (this.getColumnKey() == null ? other.getColumnKey() == null : this.getColumnKey().equals(other.getColumnKey())) + && (this.getExtra() == null ? other.getExtra() == null : this.getExtra().equals(other.getExtra())) + && (this.getPrivileges() == null ? other.getPrivileges() == null : this.getPrivileges().equals(other.getPrivileges())) + && (this.getColumnComment() == null ? other.getColumnComment() == null : this.getColumnComment().equals(other.getColumnComment())) + && (this.getColumnSize() == null ? other.getColumnSize() == null : this.getColumnSize().equals(other.getColumnSize())) + && (this.getDecimalDigits() == null ? other.getDecimalDigits() == null : this.getDecimalDigits().equals(other.getDecimalDigits())) + && (this.getGenerationExpression() == null ? other.getGenerationExpression() == null : this.getGenerationExpression().equals(other.getGenerationExpression())) + && (this.getSrsId() == null ? other.getSrsId() == null : this.getSrsId().equals(other.getSrsId())); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((getTableCatalog() == null) ? 0 : getTableCatalog().hashCode()); + result = prime * result + ((getTableSchema() == null) ? 0 : getTableSchema().hashCode()); + result = prime * result + ((getTableName() == null) ? 0 : getTableName().hashCode()); + result = prime * result + ((getColumnName() == null) ? 0 : getColumnName().hashCode()); + result = prime * result + ((getOrdinalPosition() == null) ? 0 : getOrdinalPosition().hashCode()); + result = prime * result + ((getColumnDefault() == null) ? 0 : getColumnDefault().hashCode()); + result = prime * result + ((getIsNullable() == null) ? 0 : getIsNullable().hashCode()); + result = prime * result + ((getDataType() == null) ? 0 : getDataType().hashCode()); + result = prime * result + ((getCharacterMaximumLength() == null) ? 0 : getCharacterMaximumLength().hashCode()); + result = prime * result + ((getCharacterOctetLength() == null) ? 0 : getCharacterOctetLength().hashCode()); + result = prime * result + ((getNumericPrecision() == null) ? 0 : getNumericPrecision().hashCode()); + result = prime * result + ((getNumericScale() == null) ? 0 : getNumericScale().hashCode()); + result = prime * result + ((getDatetimePrecision() == null) ? 0 : getDatetimePrecision().hashCode()); + result = prime * result + ((getCharacterSetName() == null) ? 0 : getCharacterSetName().hashCode()); + result = prime * result + ((getCollationName() == null) ? 0 : getCollationName().hashCode()); + result = prime * result + ((getColumnType() == null) ? 0 : getColumnType().hashCode()); + result = prime * result + ((getColumnKey() == null) ? 0 : getColumnKey().hashCode()); + result = prime * result + ((getExtra() == null) ? 0 : getExtra().hashCode()); + result = prime * result + ((getPrivileges() == null) ? 0 : getPrivileges().hashCode()); + result = prime * result + ((getColumnComment() == null) ? 0 : getColumnComment().hashCode()); + result = prime * result + ((getColumnSize() == null) ? 0 : getColumnSize().hashCode()); + result = prime * result + ((getDecimalDigits() == null) ? 0 : getDecimalDigits().hashCode()); + result = prime * result + ((getGenerationExpression() == null) ? 0 : getGenerationExpression().hashCode()); + result = prime * result + ((getSrsId() == null) ? 0 : getSrsId().hashCode()); + return result; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(getClass().getSimpleName()); + sb.append(" ["); + sb.append("Hash = ").append(hashCode()); + sb.append(", tableCatalog=").append(tableCatalog); + sb.append(", tableSchema=").append(tableSchema); + sb.append(", tableName=").append(tableName); + sb.append(", columnName=").append(columnName); + sb.append(", ordinalPosition=").append(ordinalPosition); + sb.append(", columnDefault=").append(columnDefault); + sb.append(", isNullable=").append(isNullable); + sb.append(", dataType=").append(dataType); + sb.append(", characterMaximumLength=").append(characterMaximumLength); + sb.append(", characterOctetLength=").append(characterOctetLength); + sb.append(", numericPrecision=").append(numericPrecision); + sb.append(", numericScale=").append(numericScale); + sb.append(", datetimePrecision=").append(datetimePrecision); + sb.append(", characterSetName=").append(characterSetName); + sb.append(", collationName=").append(collationName); + sb.append(", columnType=").append(columnType); + sb.append(", columnKey=").append(columnKey); + sb.append(", extra=").append(extra); + sb.append(", privileges=").append(privileges); + sb.append(", columnComment=").append(columnComment); + sb.append(", columnSize=").append(columnSize); + sb.append(", decimalDigits=").append(decimalDigits); + sb.append(", generationExpression=").append(generationExpression); + sb.append(", srsId=").append(srsId); + sb.append("]"); + return sb.toString(); + } +} \ No newline at end of file diff --git a/src/main/java/iet/ustb/sf/domain/TableColumn.java b/src/main/java/iet/ustb/sf/domain/TableColumn.java new file mode 100644 index 0000000..6f71489 --- /dev/null +++ b/src/main/java/iet/ustb/sf/domain/TableColumn.java @@ -0,0 +1,167 @@ +package iet.ustb.sf.domain; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import java.util.Date; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * + * @TableName mbgk_table_column + */ +@TableName(value ="mbgk_table_column") +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class TableColumn { + /** + * 主键ID + */ + private String id; + + /** + * 表名称 + */ + private String tableName; + + /** + * 中文表名称 + */ + private String tableNameCn; + + /** + * 列名称 + */ + private String columnName; + + /** + * 中文列名称 + */ + private String columnNameCn; + + /** + * 列类型 + */ + private String colnumnType; + + /** + * 模式 + */ + private String schema; + + /** + * 数据源 + */ + private String dataSource; + + /** + * 删除标识,1:true,0:false + */ + private Integer isDelete; + + /** + * 主键标识,1:true,0:false + */ + private Integer isPrimary; + + /** + * 操作,1:可计算,2:可筛选,可以有多个中间用逗号英文分割 + */ + private String op; + + /** + * 创建时间 + */ + private Date createTime; + + /** + * 更新时间 + */ + private Date updateTime; + + /** + * 排序 + */ + @TableField("sort_") + private Integer sort; + + @Override + public boolean equals(Object that) { + if (this == that) { + return true; + } + if (that == null) { + return false; + } + if (getClass() != that.getClass()) { + return false; + } + TableColumn other = (TableColumn) that; + return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId())) + && (this.getTableName() == null ? other.getTableName() == null : this.getTableName().equals(other.getTableName())) + && (this.getTableNameCn() == null ? other.getTableNameCn() == null : this.getTableNameCn().equals(other.getTableNameCn())) + && (this.getColumnName() == null ? other.getColumnName() == null : this.getColumnName().equals(other.getColumnName())) + && (this.getColumnNameCn() == null ? other.getColumnNameCn() == null : this.getColumnNameCn().equals(other.getColumnNameCn())) + && (this.getColnumnType() == null ? other.getColnumnType() == null : this.getColnumnType().equals(other.getColnumnType())) + && (this.getSchema() == null ? other.getSchema() == null : this.getSchema().equals(other.getSchema())) + && (this.getDataSource() == null ? other.getDataSource() == null : this.getDataSource().equals(other.getDataSource())) + && (this.getIsDelete() == null ? other.getIsDelete() == null : this.getIsDelete().equals(other.getIsDelete())) + && (this.getIsPrimary() == null ? other.getIsPrimary() == null : this.getIsPrimary().equals(other.getIsPrimary())) + && (this.getOp() == null ? other.getOp() == null : this.getOp().equals(other.getOp())) + && (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime())) + && (this.getUpdateTime() == null ? other.getUpdateTime() == null : this.getUpdateTime().equals(other.getUpdateTime())) + && (this.getSort() == null ? other.getSort() == null : this.getSort().equals(other.getSort())); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((getId() == null) ? 0 : getId().hashCode()); + result = prime * result + ((getTableName() == null) ? 0 : getTableName().hashCode()); + result = prime * result + ((getTableNameCn() == null) ? 0 : getTableNameCn().hashCode()); + result = prime * result + ((getColumnName() == null) ? 0 : getColumnName().hashCode()); + result = prime * result + ((getColumnNameCn() == null) ? 0 : getColumnNameCn().hashCode()); + result = prime * result + ((getColnumnType() == null) ? 0 : getColnumnType().hashCode()); + result = prime * result + ((getSchema() == null) ? 0 : getSchema().hashCode()); + result = prime * result + ((getDataSource() == null) ? 0 : getDataSource().hashCode()); + result = prime * result + ((getIsDelete() == null) ? 0 : getIsDelete().hashCode()); + result = prime * result + ((getIsPrimary() == null) ? 0 : getIsPrimary().hashCode()); + result = prime * result + ((getOp() == null) ? 0 : getOp().hashCode()); + result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode()); + result = prime * result + ((getUpdateTime() == null) ? 0 : getUpdateTime().hashCode()); + result = prime * result + ((getSort() == null) ? 0 : getSort().hashCode()); + return result; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(getClass().getSimpleName()); + sb.append(" ["); + sb.append("Hash = ").append(hashCode()); + sb.append(", id=").append(id); + sb.append(", tableName=").append(tableName); + sb.append(", tableNameCn=").append(tableNameCn); + sb.append(", columnName=").append(columnName); + sb.append(", columnNameCn=").append(columnNameCn); + sb.append(", colnumnType=").append(colnumnType); + sb.append(", schema=").append(schema); + sb.append(", dataSource=").append(dataSource); + sb.append(", isDelete=").append(isDelete); + sb.append(", isPrimary=").append(isPrimary); + sb.append(", op=").append(op); + sb.append(", createTime=").append(createTime); + sb.append(", updateTime=").append(updateTime); + sb.append(", sort=").append(sort); + sb.append("]"); + return sb.toString(); + } +} \ No newline at end of file diff --git a/src/main/java/iet/ustb/sf/domain/Tables.java b/src/main/java/iet/ustb/sf/domain/Tables.java new file mode 100644 index 0000000..3a707a4 --- /dev/null +++ b/src/main/java/iet/ustb/sf/domain/Tables.java @@ -0,0 +1,215 @@ +package iet.ustb.sf.domain; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import java.util.Date; +import lombok.Data; + +/** + * + * @TableName tables + */ +@TableName(value ="information_schema.tables") +@Data +public class Tables { + /** + * + */ + private String tableCatalog; + + /** + * + */ + private String tableSchema; + + /** + * + */ + private String tableName; + + /** + * + */ + private String tableType; + + /** + * + */ + private String engine; + + /** + * + */ + private Long version; + + /** + * + */ + private String rowFormat; + + /** + * + */ + private Long tableRows; + + /** + * + */ + private Long avgRowLength; + + /** + * + */ + private Long dataLength; + + /** + * + */ + private Long maxDataLength; + + /** + * + */ + private Long indexLength; + + /** + * + */ + private Long dataFree; + + /** + * + */ + private Long autoIncrement; + + /** + * + */ + private Date createTime; + + /** + * + */ + private Date updateTime; + + /** + * + */ + private Date checkTime; + + /** + * + */ + private String tableCollation; + + /** + * + */ + private Long checksum; + + /** + * + */ + private String createOptions; + + /** + * + */ + private String tableComment; + + @Override + public boolean equals(Object that) { + if (this == that) { + return true; + } + if (that == null) { + return false; + } + if (getClass() != that.getClass()) { + return false; + } + Tables other = (Tables) that; + return (this.getTableCatalog() == null ? other.getTableCatalog() == null : this.getTableCatalog().equals(other.getTableCatalog())) + && (this.getTableSchema() == null ? other.getTableSchema() == null : this.getTableSchema().equals(other.getTableSchema())) + && (this.getTableName() == null ? other.getTableName() == null : this.getTableName().equals(other.getTableName())) + && (this.getTableType() == null ? other.getTableType() == null : this.getTableType().equals(other.getTableType())) + && (this.getEngine() == null ? other.getEngine() == null : this.getEngine().equals(other.getEngine())) + && (this.getVersion() == null ? other.getVersion() == null : this.getVersion().equals(other.getVersion())) + && (this.getRowFormat() == null ? other.getRowFormat() == null : this.getRowFormat().equals(other.getRowFormat())) + && (this.getTableRows() == null ? other.getTableRows() == null : this.getTableRows().equals(other.getTableRows())) + && (this.getAvgRowLength() == null ? other.getAvgRowLength() == null : this.getAvgRowLength().equals(other.getAvgRowLength())) + && (this.getDataLength() == null ? other.getDataLength() == null : this.getDataLength().equals(other.getDataLength())) + && (this.getMaxDataLength() == null ? other.getMaxDataLength() == null : this.getMaxDataLength().equals(other.getMaxDataLength())) + && (this.getIndexLength() == null ? other.getIndexLength() == null : this.getIndexLength().equals(other.getIndexLength())) + && (this.getDataFree() == null ? other.getDataFree() == null : this.getDataFree().equals(other.getDataFree())) + && (this.getAutoIncrement() == null ? other.getAutoIncrement() == null : this.getAutoIncrement().equals(other.getAutoIncrement())) + && (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime())) + && (this.getUpdateTime() == null ? other.getUpdateTime() == null : this.getUpdateTime().equals(other.getUpdateTime())) + && (this.getCheckTime() == null ? other.getCheckTime() == null : this.getCheckTime().equals(other.getCheckTime())) + && (this.getTableCollation() == null ? other.getTableCollation() == null : this.getTableCollation().equals(other.getTableCollation())) + && (this.getChecksum() == null ? other.getChecksum() == null : this.getChecksum().equals(other.getChecksum())) + && (this.getCreateOptions() == null ? other.getCreateOptions() == null : this.getCreateOptions().equals(other.getCreateOptions())) + && (this.getTableComment() == null ? other.getTableComment() == null : this.getTableComment().equals(other.getTableComment())); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((getTableCatalog() == null) ? 0 : getTableCatalog().hashCode()); + result = prime * result + ((getTableSchema() == null) ? 0 : getTableSchema().hashCode()); + result = prime * result + ((getTableName() == null) ? 0 : getTableName().hashCode()); + result = prime * result + ((getTableType() == null) ? 0 : getTableType().hashCode()); + result = prime * result + ((getEngine() == null) ? 0 : getEngine().hashCode()); + result = prime * result + ((getVersion() == null) ? 0 : getVersion().hashCode()); + result = prime * result + ((getRowFormat() == null) ? 0 : getRowFormat().hashCode()); + result = prime * result + ((getTableRows() == null) ? 0 : getTableRows().hashCode()); + result = prime * result + ((getAvgRowLength() == null) ? 0 : getAvgRowLength().hashCode()); + result = prime * result + ((getDataLength() == null) ? 0 : getDataLength().hashCode()); + result = prime * result + ((getMaxDataLength() == null) ? 0 : getMaxDataLength().hashCode()); + result = prime * result + ((getIndexLength() == null) ? 0 : getIndexLength().hashCode()); + result = prime * result + ((getDataFree() == null) ? 0 : getDataFree().hashCode()); + result = prime * result + ((getAutoIncrement() == null) ? 0 : getAutoIncrement().hashCode()); + result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode()); + result = prime * result + ((getUpdateTime() == null) ? 0 : getUpdateTime().hashCode()); + result = prime * result + ((getCheckTime() == null) ? 0 : getCheckTime().hashCode()); + result = prime * result + ((getTableCollation() == null) ? 0 : getTableCollation().hashCode()); + result = prime * result + ((getChecksum() == null) ? 0 : getChecksum().hashCode()); + result = prime * result + ((getCreateOptions() == null) ? 0 : getCreateOptions().hashCode()); + result = prime * result + ((getTableComment() == null) ? 0 : getTableComment().hashCode()); + return result; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(getClass().getSimpleName()); + sb.append(" ["); + sb.append("Hash = ").append(hashCode()); + sb.append(", tableCatalog=").append(tableCatalog); + sb.append(", tableSchema=").append(tableSchema); + sb.append(", tableName=").append(tableName); + sb.append(", tableType=").append(tableType); + sb.append(", engine=").append(engine); + sb.append(", version=").append(version); + sb.append(", rowFormat=").append(rowFormat); + sb.append(", tableRows=").append(tableRows); + sb.append(", avgRowLength=").append(avgRowLength); + sb.append(", dataLength=").append(dataLength); + sb.append(", maxDataLength=").append(maxDataLength); + sb.append(", indexLength=").append(indexLength); + sb.append(", dataFree=").append(dataFree); + sb.append(", autoIncrement=").append(autoIncrement); + sb.append(", createTime=").append(createTime); + sb.append(", updateTime=").append(updateTime); + sb.append(", checkTime=").append(checkTime); + sb.append(", tableCollation=").append(tableCollation); + sb.append(", checksum=").append(checksum); + sb.append(", createOptions=").append(createOptions); + sb.append(", tableComment=").append(tableComment); + sb.append("]"); + return sb.toString(); + } +} \ No newline at end of file diff --git a/src/main/java/iet/ustb/sf/exception/ErrorCode.java b/src/main/java/iet/ustb/sf/exception/ErrorCode.java index 5b3c10f..9c52a36 100644 --- a/src/main/java/iet/ustb/sf/exception/ErrorCode.java +++ b/src/main/java/iet/ustb/sf/exception/ErrorCode.java @@ -18,7 +18,8 @@ public enum ErrorCode { NOT_FOUND_ERROR(40400, "请求数据不存在"), FORBIDDEN_ERROR(40300, "禁止访问"), SYSTEM_ERROR(50000, "系统内部异常"), - OPERATION_ERROR(50001, "操作失败"); + OPERATION_ERROR(50001, "操作失败"), + NULL_ERROR(60000, "空值"); /** * 状态码 diff --git a/src/main/java/iet/ustb/sf/mapper/ColumnsMapper.java b/src/main/java/iet/ustb/sf/mapper/ColumnsMapper.java new file mode 100644 index 0000000..fd3c51f --- /dev/null +++ b/src/main/java/iet/ustb/sf/mapper/ColumnsMapper.java @@ -0,0 +1,18 @@ +package iet.ustb.sf.mapper; + +import iet.ustb.sf.domain.Columns; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; + +/** +* @author hyy +* @description 针对表【columns】的数据库操作Mapper +* @createDate 2025-08-05 13:53:45 +* @Entity iet.ustb.sf.domain.Columns +*/ +public interface ColumnsMapper extends BaseMapper { + +} + + + + diff --git a/src/main/java/iet/ustb/sf/mapper/TableColumnMapper.java b/src/main/java/iet/ustb/sf/mapper/TableColumnMapper.java new file mode 100644 index 0000000..c34e165 --- /dev/null +++ b/src/main/java/iet/ustb/sf/mapper/TableColumnMapper.java @@ -0,0 +1,49 @@ +package iet.ustb.sf.mapper; + +import iet.ustb.sf.domain.TableColumn; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Select; + +import java.util.List; +import java.util.Map; + +/** +* @author hyy +* @description 针对表【mbgk_table_column】的数据库操作Mapper +* @createDate 2025-08-05 12:33:36 +* @Entity iet.ustb.sf.domain.TableColumn +*/ +public interface TableColumnMapper extends BaseMapper { + + @Select("SELECT DISTINCT c.table_name AS \"tableName\", " + + " c.column_name AS \"columnName\", " + + " c.data_type AS \"colnumnType\", " + + " cc.comments AS \"columnNameCn\", " + + " t.comments AS \"tableNameCn\", " + + " t.owner AS \"schema\", " + + " CASE " + + " WHEN kcu.column_name IS NOT NULL THEN '1' " + + " ELSE '0' " + + " END AS \"isPrimary\" " + + "FROM all_tab_columns c " + + " JOIN all_tab_comments t " + + " ON c.table_name = t.table_name " + + " AND c.owner = t.owner " + + " LEFT JOIN all_col_comments cc " + + " ON c.table_name = cc.table_name " + + " AND c.column_name = cc.column_name " + + " AND c.owner = cc.owner " + + " LEFT JOIN all_cons_columns kcu " + + " ON c.table_name = kcu.table_name " + + " AND c.column_name = kcu.column_name " + + " AND c.owner = kcu.owner " + + " LEFT JOIN all_constraints ku " + + " ON kcu.constraint_name = ku.constraint_name " + + " AND ku.constraint_type = 'P' " + + "WHERE c.table_name = ?1") + List> getOriColumnByTableName(String tableName); +} + + + + diff --git a/src/main/java/iet/ustb/sf/mapper/TablesMapper.java b/src/main/java/iet/ustb/sf/mapper/TablesMapper.java new file mode 100644 index 0000000..6ff0239 --- /dev/null +++ b/src/main/java/iet/ustb/sf/mapper/TablesMapper.java @@ -0,0 +1,18 @@ +package iet.ustb.sf.mapper; + +import iet.ustb.sf.domain.Tables; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; + +/** +* @author hyy +* @description 针对表【tables】的数据库操作Mapper +* @createDate 2025-08-05 13:08:39 +* @Entity iet.ustb.sf.domain.Tables +*/ +public interface TablesMapper extends BaseMapper { + +} + + + + diff --git a/src/main/java/iet/ustb/sf/service/ColumnsService.java b/src/main/java/iet/ustb/sf/service/ColumnsService.java new file mode 100644 index 0000000..512b168 --- /dev/null +++ b/src/main/java/iet/ustb/sf/service/ColumnsService.java @@ -0,0 +1,13 @@ +package iet.ustb.sf.service; + +import iet.ustb.sf.domain.Columns; +import com.baomidou.mybatisplus.extension.service.IService; + +/** +* @author hyy +* @description 针对表【columns】的数据库操作Service +* @createDate 2025-08-05 13:53:45 +*/ +public interface ColumnsService extends IService { + +} diff --git a/src/main/java/iet/ustb/sf/service/TableColumnService.java b/src/main/java/iet/ustb/sf/service/TableColumnService.java new file mode 100644 index 0000000..44aebfe --- /dev/null +++ b/src/main/java/iet/ustb/sf/service/TableColumnService.java @@ -0,0 +1,16 @@ +package iet.ustb.sf.service; + +import iet.ustb.sf.domain.TableColumn; +import com.baomidou.mybatisplus.extension.service.IService; + +/** + * @author hyy + * @description 针对表【mbgk_table_column】的数据库操作Service + * @createDate 2025-08-05 12:33:36 + */ +public interface TableColumnService extends IService { + + String syncTableData(String tableName); + + String syncTableDataByDb(String dbName); +} diff --git a/src/main/java/iet/ustb/sf/service/impl/ColumnsServiceImpl.java b/src/main/java/iet/ustb/sf/service/impl/ColumnsServiceImpl.java new file mode 100644 index 0000000..21081f9 --- /dev/null +++ b/src/main/java/iet/ustb/sf/service/impl/ColumnsServiceImpl.java @@ -0,0 +1,22 @@ +package iet.ustb.sf.service.impl; + +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import iet.ustb.sf.domain.Columns; +import iet.ustb.sf.service.ColumnsService; +import iet.ustb.sf.mapper.ColumnsMapper; +import org.springframework.stereotype.Service; + +/** +* @author hyy +* @description 针对表【columns】的数据库操作Service实现 +* @createDate 2025-08-05 13:53:45 +*/ +@Service +public class ColumnsServiceImpl extends ServiceImpl + implements ColumnsService{ + +} + + + + diff --git a/src/main/java/iet/ustb/sf/service/impl/TableColumnServiceImpl.java b/src/main/java/iet/ustb/sf/service/impl/TableColumnServiceImpl.java new file mode 100644 index 0000000..2e14f6f --- /dev/null +++ b/src/main/java/iet/ustb/sf/service/impl/TableColumnServiceImpl.java @@ -0,0 +1,113 @@ +package iet.ustb.sf.service.impl; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.baomidou.mybatisplus.extension.toolkit.SqlRunner; +import iet.ustb.sf.domain.Columns; +import iet.ustb.sf.domain.TableColumn; +import iet.ustb.sf.domain.Tables; +import iet.ustb.sf.exception.ThrowUtils; +import iet.ustb.sf.mapper.ColumnsMapper; +import iet.ustb.sf.mapper.TablesMapper; +import iet.ustb.sf.service.TableColumnService; +import iet.ustb.sf.mapper.TableColumnMapper; +import iet.ustb.sf.util.CheckUtils; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.*; +import java.util.stream.Collectors; + +/** + * @author hyy + * @description 针对表【mbgk_table_column】的数据库操作Service实现 + * @createDate 2025-08-05 12:33:36 + */ +@Service +public class TableColumnServiceImpl extends ServiceImpl + implements TableColumnService { + + @Resource + private TableColumnMapper tableColumnMapper; + + @Resource + private TablesMapper tablesMapper; + + @Resource + private ColumnsMapper columnsMapper; + + @Override + @Transactional(rollbackFor = Exception.class) + public String syncTableData(String tableName) { + CheckUtils.checkTableName(tableName); + List list = tableColumnMapper.selectList(new QueryWrapper().eq("table_name", tableName)); + if (list == null) { + list = new ArrayList<>(); + } + List columnList = list.stream().map(TableColumn::getColumnName).toList(); + + List oriList = columnsMapper.selectList(new QueryWrapper().in("table_name", tableName)); + if (oriList == null) { + oriList = new ArrayList<>(); + } + List oriColumns = oriList.stream().map(Columns::getColumnName).toList(); + + Tables tables = tablesMapper.selectOne(new QueryWrapper().eq("table_name", tableName)); + + List saveList = new ArrayList<>(); + Date now = new Date(); + int addCount = 0; + for (Columns columns : oriList) { + if (columnList.contains(columns.getColumnName())) { + continue; + } + TableColumn ori = TableColumn.builder() + .id(UUID.randomUUID().toString()) + .tableName(tableName) + .tableNameCn(tables.getTableComment()) + .columnName(columns.getColumnName()) + .columnNameCn(columns.getColumnComment()) + .colnumnType(columns.getDataType()) + .schema(columns.getTableSchema()) + .isDelete(0) + .isPrimary("UNI".equals(columns.getColumnKey()) ? 1 : 0) + .createTime(now) + .updateTime(now) + .sort(list.size()) + .build(); + saveList.add(ori); + list.add(ori); + addCount++; + } + int delCount = 0; + for (TableColumn tableColumn : list) { + if (!oriColumns.contains(tableColumn.getColumnName())) { + tableColumn.setIsDelete(1); + tableColumn.setUpdateTime(now); + saveList.add(tableColumn); + delCount++; + } + } + tableColumnMapper.insert(saveList); + return "添加" + addCount + "个字段,删除" + delCount + "个字段"; + } + + @Override + @Transactional(rollbackFor = Exception.class) + public String syncTableDataByDb(String dbName) { + List tablesList = tablesMapper.selectList(new QueryWrapper().eq("table_schema", dbName)); + ThrowUtils.throwIf(tablesList.size() <= 0, new RuntimeException("数据库不存在!")); + StringBuilder result = new StringBuilder(); + for (Tables tables : tablesList) { + String tableName = tables.getTableName(); + result.append("\n" + "数据库表:").append(tableName); + result.append(syncTableData(tableName)); + } + return result.toString(); + } +} + + + + diff --git a/src/main/java/iet/ustb/sf/util/CheckUtils.java b/src/main/java/iet/ustb/sf/util/CheckUtils.java new file mode 100644 index 0000000..af38771 --- /dev/null +++ b/src/main/java/iet/ustb/sf/util/CheckUtils.java @@ -0,0 +1,42 @@ +package iet.ustb.sf.util; + +import com.baomidou.mybatisplus.extension.toolkit.SqlRunner; +import iet.ustb.sf.exception.ErrorCode; +import iet.ustb.sf.exception.ThrowUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; + +/** + * CheckUtils + * + * @author huangge1199 + * @since 2025/8/5 12:43:14 + */ +@Component +public class CheckUtils { + + /** + * 字符串空值验证 + * + * @param str 字符串 + * @param name 中文名 + */ + public static void checkEmpty(String str, String name) { + ThrowUtils.throwIf(StringUtils.isEmpty(str), ErrorCode.NULL_ERROR, name + "不能为空!"); + } + + /** + * 表名验证 + * + * @param tableName 表名 + */ + public static void checkTableName(String tableName) { + checkEmpty(tableName, "表名"); + String sql = "SELECT COUNT(1) FROM information_schema.tables WHERE table_schema = 'mbgk' AND table_name = '" + tableName + "'"; + long cnt = SqlRunner.db().selectCount(sql); + ThrowUtils.throwIf(cnt <= 0, new RuntimeException("表" + tableName + "不存在!")); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index ded8e0d..cb5d146 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -17,4 +17,13 @@ springdoc: knife4j: enable: true setting: - language: zh_cn \ No newline at end of file + language: zh_cn +mybatis-plus: + global-config: + enable-sql-runner: true + configuration: + # ?????????? + map-underscore-to-camel-case: true + # ????SQL + log-impl: org.apache.ibatis.logging.stdout.StdOutImpl + diff --git a/src/main/resources/iet/ustb/sf/mapper/ColumnsMapper.xml b/src/main/resources/iet/ustb/sf/mapper/ColumnsMapper.xml new file mode 100644 index 0000000..b27f7f7 --- /dev/null +++ b/src/main/resources/iet/ustb/sf/mapper/ColumnsMapper.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + TABLE_CATALOG,TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,ORDINAL_POSITION,COLUMN_DEFAULT, + IS_NULLABLE,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,CHARACTER_OCTET_LENGTH,NUMERIC_PRECISION, + NUMERIC_SCALE,DATETIME_PRECISION,CHARACTER_SET_NAME,COLLATION_NAME,COLUMN_TYPE, + COLUMN_KEY,EXTRA,PRIVILEGES,COLUMN_COMMENT,COLUMN_SIZE, + DECIMAL_DIGITS,GENERATION_EXPRESSION,SRS_ID + + diff --git a/src/main/resources/iet/ustb/sf/mapper/TableColumnMapper.xml b/src/main/resources/iet/ustb/sf/mapper/TableColumnMapper.xml new file mode 100644 index 0000000..0742b2d --- /dev/null +++ b/src/main/resources/iet/ustb/sf/mapper/TableColumnMapper.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + id,table_name,table_name_cn,column_name,column_name_cn,colnumn_type, + schema,data_source,is_delete,is_primary,op, + create_time,update_time,sort_ + + diff --git a/src/main/resources/iet/ustb/sf/mapper/TablesMapper.xml b/src/main/resources/iet/ustb/sf/mapper/TablesMapper.xml new file mode 100644 index 0000000..20aee22 --- /dev/null +++ b/src/main/resources/iet/ustb/sf/mapper/TablesMapper.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + TABLE_CATALOG,TABLE_SCHEMA,TABLE_NAME,TABLE_TYPE,ENGINE,VERSION, + ROW_FORMAT,TABLE_ROWS,AVG_ROW_LENGTH,DATA_LENGTH,MAX_DATA_LENGTH, + INDEX_LENGTH,DATA_FREE,AUTO_INCREMENT,CREATE_TIME,UPDATE_TIME, + CHECK_TIME,TABLE_COLLATION,CHECKSUM,CREATE_OPTIONS,TABLE_COMMENT + +