Compare commits

...

2 Commits

Author SHA1 Message Date
031026 8ddd6a8609 Merge remote-tracking branch 'origin/master' 2024-04-26 10:11:10 +08:00
031026 92579b93a3 fix:修复同步bug
feat:资产树形
2024-04-26 10:10:50 +08:00
8 changed files with 210 additions and 80 deletions

View File

@ -19,6 +19,11 @@ import java.util.List;
@NoArgsConstructor
@AllArgsConstructor
public class AssetStructure{
/**
* id
*/
private Integer id;
/** 接入源名称 */
private String accessSourceName;
@ -40,10 +45,9 @@ public class AssetStructure{
/**是否核心*/
private String coreOrNot;
/**表注释*/
private String tableComment;
/**类型*/
/**
*
*/
private Integer type;
}

View File

@ -1,5 +1,9 @@
package com.muyu.data.source.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.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
@ -15,9 +19,11 @@ import lombok.NoArgsConstructor;
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "database_table_information")
public class DatabaseTableInformation {
/**数据库信息id*/
@TableId(value = "id",type = IdType.AUTO)
private Long id;
/**数据库表名*/
@ -40,4 +46,16 @@ public class DatabaseTableInformation {
/**类型*/
private Integer type;
/**
* ID
*/
private Integer structureId;
public DatabaseTableInformation databaseTableInformation (String tableName, String tableComment, int tableRows) {
return DatabaseTableInformation.builder()
.name(tableName)
.as(tableComment)
.dataTotal(tableRows)
.build();
}
}

View File

@ -0,0 +1,46 @@
package com.muyu.data.source.domain.req;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
/**
*
*
* @ClassName DatabaseConnect
* @Author AnNan.Wang
* @Date 2024/4/25 21:14
*/
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class DatabaseConnect {
/**
*
*/
private String username;
/**
*
*/
private String password;
/**
*
*/
private String host;
/**
*
*/
private String type;
/**
*
*/
private String databaseName;
/**
*
*/
private String port;
}

View File

@ -6,16 +6,10 @@ import javax.servlet.http.HttpServletResponse;
import com.muyu.data.source.domain.*;
import com.muyu.data.source.domain.model.DataBaseModel;
import com.muyu.data.source.domain.model.DatabaseTableModel;
import com.muyu.data.source.domain.req.DatabaseConnect;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
@ -133,20 +127,22 @@ public class DataSourceController extends BaseController {
return dataSourceService.synchronization(dataSource);
}
@GetMapping("/findDataBaseTable")
public Result<List<DatabaseTableModel>> findDataBaseTable(){
return dataSourceService.findDataBaseTable();
}
@GetMapping("/findAssetStructure")
public Result<List<AssetStructure>> findAssetStructure(){
return dataSourceService.findAssetStructure();
//查询结构
@PostMapping("/findDataBaseTable")
public Result<List<DatabaseTableModel>> findDataBaseTable(@RequestParam("databaseTableName") String databaseTableName){
return dataSourceService.findDataBaseTable(databaseTableName);
}
//查询接入源信息
@GetMapping("/findStructure")
public Result<Structure> findStructure(){
return dataSourceService.findStructure();
}
//点击查询表
@PostMapping("/structureList")
public Result<Structure> structureList(@RequestParam("id") Integer id){
return dataSourceService.structureList(id);
}
}

View File

@ -24,17 +24,20 @@ public interface DataSourceMapper extends BaseMapper<DataSource> {
void addAssets1(AssetStructure assetStructure);
List<AssetStructure> assetStructureList();
DatabaseTableModel findByName(@Param("databaseName") String databaseName, @Param("databaseTableName") String databaseTableName);
List<DatabaseTableInformation> findByBaseName(@Param("databaseName") String databaseName);
void addDatabaseTableInformation(@Param("databaseTableInformationList") List<DatabaseTableInformation> databaseTableInformationList);
void addDatabaseTableInformation(@Param("databaseTableInformationList") List<DatabaseTableInformation> databaseTableInformationList, @Param("id") Integer id);
void addDatabaseTableInfo(@Param("databaseTableInformations") ArrayList<DatabaseTableInformation> databaseTableInformations);
List<AssetStructure> findAssetStructure();
List<DatabaseTableInformation> findInformation();
List<DatabaseTableInformation> findInformation(Integer id);
List<AssetStructure> assetStructureList();
}

View File

@ -30,9 +30,10 @@ public interface DataSourceService extends IService<DataSource> {
Result synchronization(DataSource dataSource);
Result<List<DatabaseTableModel>> findDataBaseTable();
Result<List<DatabaseTableModel>> findDataBaseTable(String databaseTableName);
Result<List<AssetStructure>> findAssetStructure();
Result<Structure> findStructure();
Result<Structure> structureList(Integer id);
}

View File

@ -2,12 +2,10 @@ package com.muyu.data.source.service.impl;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.ObjUtils;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.data.source.domain.*;
import com.muyu.data.source.domain.model.DatabaseTableModel;
import com.muyu.data.source.domain.req.DataSourceSaveReq;
@ -30,7 +28,6 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSource> implements DataSourceService {
@Autowired
private DataSourceMapper dataSourceMapper;
/**
*
*
@ -119,77 +116,140 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
@Override
public Result synchronization(DataSource dataSource) {
AssetStructure assetStructure = new AssetStructure();
ArrayList<String> tableNames = new ArrayList<>();
List<DatabaseTableInformation> databaseTableInformationList=dataSourceMapper.findByBaseName(dataSource.getDatabaseName());
dataSourceMapper.addDatabaseTableInformation(databaseTableInformationList);
List<AssetStructure> list = new ArrayList<>();
String user=dataSource.getDatabaseUserName();
String password=dataSource.getDatabaseUserPassword();
String jdbcDriver="com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://" + dataSource.getHostAddress() + ":" + dataSource.getHostPort() + "/" + dataSource.getDatabaseName();
String query="show tables";
String user=dataSource.getDatabaseUserName();
String password=dataSource.getDatabaseUserPassword();
String jdbcDriver="com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://" + dataSource.getHostAddress() + ":" + dataSource.getHostPort() + "/" + dataSource.getDatabaseName();
String query="show tables ";
try(Connection con=DriverManager.getConnection(url,user,password);
Statement stmt=con.createStatement();
ResultSet rs= stmt.executeQuery(query)) {
ResultSetMetaData rsmd = rs.getMetaData();
// 5. 处理查询结果
while (rs.next()) {
String tableName = rs.getString(1);
tableNames.add(tableName);
}
ArrayList<DatabaseTableInformation> arrayList = new ArrayList<>();
assetStructure.setAccessSourceName(dataSource.getAccessSourceName());
assetStructure.setDatabaseName(dataSource.getDatabaseName());
assetStructure.setDataSourceSystemName(dataSource.getDataSourceSystemName());
try (Connection con = DriverManager.getConnection(url, user, password)) {
// 创建 PreparedStatement并设置参数
PreparedStatement prepared = con.prepareStatement("SELECT " +
" TABLE_NAME AS `name`, " +
" TABLE_COMMENT AS `as`, " +
" TABLE_ROWS AS `dataTotal` " +
" FROM INFORMATION_SCHEMA.TABLES " +
" WHERE TABLE_SCHEMA = ?");
prepared.setString(1, dataSource.getDatabaseName()); // 设置数据库名称参数
assetStructure.setDatabaseTableName(rsmd.getTableName(1));
assetStructure.setDataVolume(rsmd.getColumnDisplaySize(1));
assetStructure.setDataQuantity(rsmd.getColumnDisplaySize(1));
dataSourceMapper.addAssets1(assetStructure);
// 执行查询
ResultSet resultSet = prepared.executeQuery();
return Result.success("同步成功");
ResultSetMetaData rsmd = resultSet.getMetaData();
// 5. 处理查询结果
while (resultSet.next()) {
//表名
String tableName = resultSet.getString("name");
//表备注
String tableComment = resultSet.getString("as");
//表条数
int tableRows = resultSet.getInt("dataTotal");
//查询出表结构
PreparedStatement preparedStatement = con.prepareStatement(" SELECT " +
" TABLE_NAME AS 'tableName', " +
" COLUMN_NAME AS 'name', " +
" COLUMN_COMMENT AS 'comment', " +
" CASE WHEN COLUMN_KEY = 'PRI' THEN '是' ELSE '否' END AS 'isPrimaryKey', " +
" CASE " +
" WHEN DATA_TYPE = 'int' THEN 'Integer' " +
" WHEN DATA_TYPE = 'varchar' THEN 'String' " +
" WHEN DATA_TYPE = 'decimal' THEN 'BigDecimal' " +
" WHEN DATA_TYPE = 'tinyint' AND COLUMN_TYPE = 'tinyint(1)' THEN 'Boolean' " +
" ELSE DATA_TYPE " +
" END AS 'type', " +
" DATA_TYPE AS 'mappingType', " +
" COLUMN_TYPE AS 'detailType', " +
" CHARACTER_MAXIMUM_LENGTH AS 'length', " +
" NUMERIC_SCALE AS 'decimalPlaces', " +
" IS_NULLABLE AS 'isNull', " +
" COLUMN_DEFAULT AS 'defaultValue' " +
"FROM " +
" INFORMATION_SCHEMA.COLUMNS " +
"WHERE " +
" TABLE_SCHEMA = ? " +
" AND TABLE_NAME = ?");
} catch (SQLException e) {
System.out.println("连接失败"+e.getMessage());
return Result.error("同步失败");
}
preparedStatement.setString(1,dataSource.getDatabaseName());
preparedStatement.setString(2,tableName);
ResultSet executed = preparedStatement.executeQuery();
ResultSetMetaData metaData = executed.getMetaData();
// 创建 DatabaseTableInformation对象
DatabaseTableInformation build = DatabaseTableInformation.builder()
.name(tableName)
.as(tableComment)
.dataTotal(tableRows)
.build();
// 将对象添加到 ArrayList 中
arrayList.add(build);
}
assetStructure.setAccessSourceName(dataSource.getAccessSourceName());
assetStructure.setDatabaseName(dataSource.getDatabaseName());
assetStructure.setDataSourceSystemName(dataSource.getDataSourceSystemName());
assetStructure.setDatabaseTableName(rsmd.getTableName(1));
assetStructure.setDataVolume(arrayList.size());
Integer num = 0;
for (DatabaseTableInformation databaseTableInformation : arrayList) {
num+=databaseTableInformation.getDataTotal();
}
System.out.println(num);
assetStructure.setDataQuantity(num);
dataSourceMapper.addAssets1(assetStructure);
Integer id = assetStructure.getId();
dataSourceMapper.addDatabaseTableInformation(arrayList,id);
return Result.success("同步成功");
} catch (SQLException e) {
System.out.println("连接失败"+e.getMessage());
return Result.error("同步失败");
}
}
@Override
public Result<List<DatabaseTableModel>> findDataBaseTable() {
public Result<List<DatabaseTableModel>> findDataBaseTable(String databaseTableName) {
List<AssetStructure> assetStructureList=dataSourceMapper.assetStructureList();
List<DatabaseTableModel> databaseTableList = new ArrayList<>();
for (AssetStructure assetStructure : assetStructureList) {
DatabaseTableModel databaseTableModel=dataSourceMapper.findByName(assetStructure.getDatabaseName(),assetStructure.getDatabaseTableName());
System.out.println(databaseTableModel);
DatabaseTableModel databaseTableModel=dataSourceMapper.findByName(assetStructure.getDatabaseName(),databaseTableName);
databaseTableList.add(databaseTableModel);
}
System.out.println(databaseTableList);
return Result.success(databaseTableList);
}
@Override
public Result<List<AssetStructure>> findAssetStructure() {
List<AssetStructure> assetStructureList=dataSourceMapper.assetStructureList();
return Result.success(assetStructureList);
}
@Override
public Result<Structure> findStructure() {
Structure structure = new Structure();
List<AssetStructure> assetStructure = dataSourceMapper.findAssetStructure();
structure.setAssetStructureList(assetStructure);
List<DatabaseTableInformation> databaseTableInformationList=dataSourceMapper.findInformation();
structure.setDatabaseTableInformationList(databaseTableInformationList);
return Result.success(structure);
}
@Override
public Result<Structure> structureList(Integer id) {
List<DatabaseTableInformation> information = dataSourceMapper.findInformation(id);
Structure structure = new Structure();
structure.setDatabaseTableInformationList(information);
return Result.success(structure);
}
}

View File

@ -47,7 +47,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</foreach>
</insert>
<insert id="addAssets1">
<insert id="addAssets1" useGeneratedKeys="true" keyProperty="id">
INSERT INTO `data_management`.`asset_structure`
(`access_source_name`, `data_source_system_name`, `database_name`, `data_volume`, `data_quantity`, `core_or_not`)
VALUES
@ -65,13 +65,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</insert>
<insert id="addDatabaseTableInformation">
INSERT INTO `data_management`.`database_table_information`
( `name`, `as`, `dataTotal`)
( `name`, `as`, `dataTotal`,`structure_id`)
VALUES
<foreach collection="databaseTableInformationList" item="databaseTableInformationList" separator=",">
(
#{databaseTableInformationList.name},
#{databaseTableInformationList.as},
#{databaseTableInformationList.dataTotal}
#{databaseTableInformationList.dataTotal},
#{id}
)
</foreach>
@ -94,9 +95,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<select id="findAccessType" resultType="com.muyu.data.source.domain.AccessType">
select * from access_type
</select>
<select id="assetStructureList" resultType="com.muyu.data.source.domain.AssetStructure">
select * from asset_structure
</select>
<select id="findByName" resultType="com.muyu.data.source.domain.model.DatabaseTableModel">
SELECT
TABLE_NAME AS 'tableName',
@ -126,18 +125,21 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</select>
<select id="findByBaseName" resultType="com.muyu.data.source.domain.DatabaseTableInformation">
SELECT
TABLE_NAME as 'name',
TABLE_COMMENT as 'as',
TABLE_ROWS as 'dataTotal'
TABLE_NAME AS `name`,
TABLE_COMMENT AS `as`,
TABLE_ROWS AS `dataTotal`
FROM INFORMATION_SCHEMA.TABLES
WHERE
TABLE_SCHEMA = #{databaseName}
TABLE_SCHEMA = ${databaseName}
</select>
<select id="findAssetStructure" resultType="com.muyu.data.source.domain.AssetStructure">
select * from asset_structure
</select>
<select id="findInformation" resultType="com.muyu.data.source.domain.DatabaseTableInformation">
select * from database_table_information
select * from database_table_information where structure_id=#{id}
</select>
<select id="assetStructureList" resultType="com.muyu.data.source.domain.AssetStructure">
</select>
</mapper>