feat: 同步资产结构到数据库
parent
f9f39497ab
commit
d0131f4d97
|
@ -0,0 +1,44 @@
|
||||||
|
package com.muyu.data.source.domain;
|
||||||
|
|
||||||
|
import com.muyu.common.core.web.domain.BaseEntity;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 资产结构对象 AssetStructure
|
||||||
|
*
|
||||||
|
* @author DeKangLiu
|
||||||
|
* on 2024/4/22
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class AssetStructure{
|
||||||
|
/** 接入源名称 */
|
||||||
|
private String accessSourceName;
|
||||||
|
|
||||||
|
/** 数据来源系统名称 */
|
||||||
|
private String dataSourceSystemName;
|
||||||
|
|
||||||
|
/** 数据库名称 */
|
||||||
|
private String databaseName;
|
||||||
|
|
||||||
|
/**数据库表名*/
|
||||||
|
private String databaseTableName;
|
||||||
|
|
||||||
|
/**数据量*/
|
||||||
|
private Integer dataVolume;
|
||||||
|
|
||||||
|
/**数据条数*/
|
||||||
|
private Integer dataQuantity;
|
||||||
|
|
||||||
|
/**是否核心*/
|
||||||
|
private String coreOrNot;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -82,6 +82,13 @@
|
||||||
<groupId>com.muyu</groupId>
|
<groupId>com.muyu</groupId>
|
||||||
<artifactId>muyu-common-swagger</artifactId>
|
<artifactId>muyu-common-swagger</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>druid</artifactId>
|
||||||
|
<version>1.2.20</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import java.util.List;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import com.muyu.data.source.domain.AccessType;
|
import com.muyu.data.source.domain.AccessType;
|
||||||
|
import com.muyu.data.source.domain.AssetStructure;
|
||||||
import io.swagger.annotations.*;
|
import io.swagger.annotations.*;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
@ -121,4 +122,9 @@ public class DataSourceController extends BaseController {
|
||||||
public Result testConnection(@RequestBody DataSourceSaveReq dataSourceSaveReq){
|
public Result testConnection(@RequestBody DataSourceSaveReq dataSourceSaveReq){
|
||||||
return dataSourceService.testConnection(dataSourceSaveReq);
|
return dataSourceService.testConnection(dataSourceSaveReq);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/synchronization")
|
||||||
|
public Result synchronization(@RequestBody DataSource dataSource){
|
||||||
|
return dataSourceService.synchronization(dataSource);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,9 @@ package com.muyu.data.source.mapper;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
import com.muyu.data.source.domain.AccessType;
|
import com.muyu.data.source.domain.AccessType;
|
||||||
|
import com.muyu.data.source.domain.AssetStructure;
|
||||||
import com.muyu.data.source.domain.DataSource;
|
import com.muyu.data.source.domain.DataSource;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数据源Mapper接口
|
* 数据源Mapper接口
|
||||||
|
@ -14,4 +16,8 @@ import com.muyu.data.source.domain.DataSource;
|
||||||
public interface DataSourceMapper extends BaseMapper<DataSource> {
|
public interface DataSourceMapper extends BaseMapper<DataSource> {
|
||||||
|
|
||||||
List<AccessType> findAccessType();
|
List<AccessType> findAccessType();
|
||||||
|
|
||||||
|
void addAssets(@Param("assetStructure") List<AssetStructure> assetStructure);
|
||||||
|
|
||||||
|
void addAssets1(AssetStructure assetStructure);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import java.util.List;
|
||||||
|
|
||||||
import com.muyu.common.core.domain.Result;
|
import com.muyu.common.core.domain.Result;
|
||||||
import com.muyu.data.source.domain.AccessType;
|
import com.muyu.data.source.domain.AccessType;
|
||||||
|
import com.muyu.data.source.domain.AssetStructure;
|
||||||
import com.muyu.data.source.domain.DataSource;
|
import com.muyu.data.source.domain.DataSource;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
import com.muyu.data.source.domain.req.DataSourceSaveReq;
|
import com.muyu.data.source.domain.req.DataSourceSaveReq;
|
||||||
|
@ -26,4 +27,7 @@ public interface DataSourceService extends IService<DataSource> {
|
||||||
List<AccessType> findAccessType();
|
List<AccessType> findAccessType();
|
||||||
|
|
||||||
Result testConnection(DataSourceSaveReq dataSourceQueryReq);
|
Result testConnection(DataSourceSaveReq dataSourceQueryReq);
|
||||||
|
|
||||||
|
|
||||||
|
Result synchronization(DataSource dataSource);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,15 @@
|
||||||
package com.muyu.data.source.service.impl;
|
package com.muyu.data.source.service.impl;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.*;
|
||||||
import java.sql.DriverManager;
|
import java.util.ArrayList;
|
||||||
import java.sql.SQLException;
|
import java.util.Date;
|
||||||
import java.sql.Statement;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.muyu.common.core.domain.Result;
|
import com.muyu.common.core.domain.Result;
|
||||||
import com.muyu.common.core.utils.ObjUtils;
|
import com.muyu.common.core.utils.ObjUtils;
|
||||||
|
import com.muyu.common.security.utils.SecurityUtils;
|
||||||
import com.muyu.data.source.domain.AccessType;
|
import com.muyu.data.source.domain.AccessType;
|
||||||
|
import com.muyu.data.source.domain.AssetStructure;
|
||||||
import com.muyu.data.source.domain.req.DataSourceSaveReq;
|
import com.muyu.data.source.domain.req.DataSourceSaveReq;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -115,4 +116,53 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
|
||||||
|
|
||||||
return Result.error("连接失败");
|
return Result.error("连接失败");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Result synchronization(DataSource dataSource) {
|
||||||
|
|
||||||
|
AssetStructure assetStructure = new AssetStructure();
|
||||||
|
|
||||||
|
ArrayList<String> tableNames = new ArrayList<>();
|
||||||
|
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 ";
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
System.out.println(tableNames);
|
||||||
|
for (String tableName : tableNames) {
|
||||||
|
assetStructure.setAccessSourceName(dataSource.getAccessSourceName());
|
||||||
|
assetStructure.setDatabaseName(dataSource.getDatabaseName());
|
||||||
|
assetStructure.setDataSourceSystemName(dataSource.getDataSourceSystemName());
|
||||||
|
for (int a = 0; a < rsmd.getColumnCount(); a++) {
|
||||||
|
assetStructure.setDatabaseTableName(rsmd.getTableName(a+1));
|
||||||
|
assetStructure.setDataQuantity(rsmd.getColumnDisplaySize(a+1));
|
||||||
|
assetStructure.setDataVolume(rsmd.getColumnDisplaySize(a+1));
|
||||||
|
}
|
||||||
|
assetStructure.setDatabaseTableName(tableName);
|
||||||
|
// assetStructure.setCreateBy(SecurityUtils.getUsername());
|
||||||
|
// assetStructure.setCreateTime(new Date());
|
||||||
|
dataSourceMapper.addAssets1(assetStructure);
|
||||||
|
}
|
||||||
|
return Result.success("同步成功");
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.out.println("连接失败"+e.getMessage());
|
||||||
|
return Result.error("同步失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,41 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
<sql id="selectDataSourceVo">
|
<sql id="selectDataSourceVo">
|
||||||
select id, access_source_name, data_source_system_name, host_address, host_port, data_access_type_id, database_name, data_connection_parameter, initial_quantity, maximum_quantity, maximum_time, maximum_frequency, remark, create_by, create_time, update_by, update_time ,database_user_name ,database_user_password from data_source
|
select id, access_source_name, data_source_system_name, host_address, host_port, data_access_type_id, database_name, data_connection_parameter, initial_quantity, maximum_quantity, maximum_time, maximum_frequency, remark, create_by, create_time, update_by, update_time ,database_user_name ,database_user_password from data_source
|
||||||
</sql>
|
</sql>
|
||||||
|
<insert id="addAssets">
|
||||||
|
|
||||||
|
INSERT INTO `data_management`.`asset_structure`
|
||||||
|
(`access_source_name`, `data_source_system_name`, `database_name`, `database_table_name`, `data_volume`, `data_quantity`, `core_or_not`)
|
||||||
|
VALUES
|
||||||
|
<foreach collection="assetStructure" item="assetStructure" separator=",">
|
||||||
|
(
|
||||||
|
#{assetStructure.accessSourceName},
|
||||||
|
#{assetStructure.dataSourceSystemName},
|
||||||
|
#{assetStructure.databaseName},
|
||||||
|
#{assetStructure.databaseTableName},
|
||||||
|
#{assetStructure.dataVolume},
|
||||||
|
#{assetStructure.dataQuantity},
|
||||||
|
'Y'
|
||||||
|
)
|
||||||
|
</foreach>
|
||||||
|
|
||||||
|
</insert>
|
||||||
|
<insert id="addAssets1">
|
||||||
|
INSERT INTO `data_management`.`asset_structure`
|
||||||
|
(`access_source_name`, `data_source_system_name`, `database_name`, `database_table_name`, `data_volume`, `data_quantity`, `core_or_not`)
|
||||||
|
VALUES
|
||||||
|
|
||||||
|
(
|
||||||
|
#{accessSourceName},
|
||||||
|
#{dataSourceSystemName},
|
||||||
|
#{databaseName},
|
||||||
|
#{databaseTableName},
|
||||||
|
#{dataVolume},
|
||||||
|
#{dataQuantity},
|
||||||
|
'Y'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
</insert>
|
||||||
<select id="findAccessType" resultType="com.muyu.data.source.domain.AccessType">
|
<select id="findAccessType" resultType="com.muyu.data.source.domain.AccessType">
|
||||||
select * from access_type
|
select * from access_type
|
||||||
</select>
|
</select>
|
||||||
|
|
Loading…
Reference in New Issue