fix(添加测试接口)
commit
f947087b9c
|
@ -0,0 +1,51 @@
|
|||
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.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
/**
|
||||
* 数据展示
|
||||
*
|
||||
* @ClassName Asset
|
||||
* @Author AnNan.Wang
|
||||
* @Date 2024/5/14 8:59
|
||||
*/
|
||||
|
||||
@Data
|
||||
@SuperBuilder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@TableName(value = "asset")
|
||||
public class Asset {
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
@TableId(value = "id",type = IdType.AUTO)
|
||||
private Long id;
|
||||
/**
|
||||
* key
|
||||
*/
|
||||
private String assetKey;
|
||||
/**
|
||||
* 类型
|
||||
*/
|
||||
private String type;
|
||||
/**
|
||||
* 值
|
||||
*/
|
||||
private Object value;
|
||||
/**
|
||||
* 数据源id
|
||||
*/
|
||||
private Long databaseId;
|
||||
/**
|
||||
* 表名
|
||||
*/
|
||||
private String tableName;
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.muyu.data.source.controller;
|
||||
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.data.source.domain.Asset;
|
||||
import com.muyu.data.source.service.AssetService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 数据展示的 Controller层
|
||||
*
|
||||
* @ClassName AssetController
|
||||
* @Author AnNan.Wang
|
||||
* @Date 2024/5/14 10:31
|
||||
*/
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/assets")
|
||||
public class AssetController {
|
||||
@Autowired
|
||||
private AssetService assetService;
|
||||
|
||||
@GetMapping("/assetsList")
|
||||
public Result<List<Asset>> assetsList(){
|
||||
return assetService.assetsList();
|
||||
}
|
||||
}
|
|
@ -170,9 +170,4 @@ public class DataSourceController extends BaseController {
|
|||
return dataSourceService.information();
|
||||
}
|
||||
|
||||
//获取数据
|
||||
@PostMapping("/tableParticulars")
|
||||
public Result<String> tableParticulars(@RequestBody DataTableValue dataTableValue){
|
||||
return dataSourceService.tableParticulars(dataTableValue);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package com.muyu.data.source.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.muyu.data.source.domain.Asset;
|
||||
|
||||
/**
|
||||
* Asset 的 Mapper 层接口
|
||||
*
|
||||
* @author AnNan.Wang
|
||||
* @ClassName: AssetMapper
|
||||
* @createTime: 2024/5/14 10:34
|
||||
*/
|
||||
public interface AssetMapper extends BaseMapper<Asset> {
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.muyu.data.source.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.data.source.domain.Asset;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Asset的 Service 层接口
|
||||
*
|
||||
* @author AnNan.Wang
|
||||
* @ClassName: AssetService
|
||||
* @createTime: 2024/5/14 10:32
|
||||
*/
|
||||
public interface AssetService extends IService<Asset> {
|
||||
Result<List<Asset>> assetsList();
|
||||
|
||||
|
||||
}
|
|
@ -49,6 +49,5 @@ public interface DataSourceService extends IService<DataSource> {
|
|||
|
||||
Result<List<DataSourceTableRed>> information();
|
||||
|
||||
Result<String> tableParticulars(DataTableValue dataTableValue);
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
package com.muyu.data.source.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.data.source.domain.Asset;
|
||||
import com.muyu.data.source.mapper.AssetMapper;
|
||||
import com.muyu.data.source.service.AssetService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* AssetService 层业务实现接口
|
||||
*
|
||||
* @ClassName AssetServiceImpl
|
||||
* @Author AnNan.Wang
|
||||
* @Date 2024/5/14 10:32
|
||||
*/
|
||||
|
||||
@Service
|
||||
public class AssetServiceImpl extends ServiceImpl<AssetMapper, Asset>
|
||||
implements AssetService{
|
||||
@Autowired
|
||||
private AssetMapper assetMapper;
|
||||
|
||||
|
||||
@Override
|
||||
public Result<List<Asset>> assetsList() {
|
||||
System.out.println(assetMapper.selectList(null));
|
||||
return Result.success(
|
||||
assetMapper.selectList(null)
|
||||
);
|
||||
}
|
||||
}
|
|
@ -2,7 +2,11 @@ package com.muyu.data.source.service.impl;
|
|||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.common.core.utils.ObjUtils;
|
||||
|
@ -12,6 +16,7 @@ import com.muyu.data.source.domain.model.DatabaseTableModel;
|
|||
import com.muyu.data.source.domain.red.DataSourceTableRed;
|
||||
import com.muyu.data.source.domain.req.DataSourceSaveReq;
|
||||
import com.muyu.data.source.domain.req.DatabaseConnect;
|
||||
import com.muyu.data.source.mapper.AssetMapper;
|
||||
import com.muyu.data.source.mapper.DatabaseTableInformationMapper;
|
||||
import com.muyu.data.source.mapper.DatabaseTableMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
@ -92,29 +97,141 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
|
|||
|
||||
return list(queryWrapper);
|
||||
}
|
||||
@Autowired
|
||||
private AssetMapper assetMapper;
|
||||
|
||||
@Override
|
||||
public List<AccessType> findAccessType() {
|
||||
return dataSourceMapper.findAccessType();
|
||||
}
|
||||
|
||||
private final static String postgresSqlTable = "SELECT \n" +
|
||||
" TABLE_NAME AS 'tableName',\n" +
|
||||
" COLUMN_NAME AS 'name',\n" +
|
||||
" COLUMN_COMMENT AS 'comment',\n" +
|
||||
" CASE WHEN COLUMN_KEY = 'PRI' THEN '是' ELSE '否' END AS 'isPrimaryKey',\n" +
|
||||
"\n" +
|
||||
" CASE\n" +
|
||||
" WHEN DATA_TYPE = 'int' THEN 'Integer'\n" +
|
||||
" WHEN DATA_TYPE = 'varchar' THEN 'String'\n" +
|
||||
" WHEN DATA_TYPE = 'decimal' THEN 'BigDecimal'\n" +
|
||||
" WHEN DATA_TYPE = 'tinyint' AND COLUMN_TYPE = 'tinyint(1)' THEN 'Boolean'\n" +
|
||||
"\n" +
|
||||
" ELSE DATA_TYPE\n" +
|
||||
" END AS 'type',\n" +
|
||||
" DATA_TYPE AS 'mappingType',\n" +
|
||||
" COLUMN_TYPE AS 'detailType',\n" +
|
||||
" CHARACTER_MAXIMUM_LENGTH AS 'length',\n" +
|
||||
" NUMERIC_SCALE AS 'decimalPlaces',\n" +
|
||||
" IS_NULLABLE AS 'isNull',\n" +
|
||||
" COLUMN_DEFAULT AS 'defaultValue'\n" +
|
||||
" FROM\n" +
|
||||
" INFORMATION_SCHEMA.COLUMNS\n";
|
||||
@Override
|
||||
public Result testConnection(DataSourceSaveReq dataSourceQueryReq) {
|
||||
String user = dataSourceQueryReq.getDatabaseUserName();
|
||||
String password = dataSourceQueryReq.getDatabaseUserPassword();
|
||||
String jdbcDriver = "com.mysql.cj.jdbc.Driver";
|
||||
String url = "jdbc:mysql://"+dataSourceQueryReq.getHostAddress()+":"+dataSourceQueryReq.getHostPort()+"/"+dataSourceQueryReq.getDatabaseName();
|
||||
ArrayList<String> arrayList = new ArrayList<>();
|
||||
//数据展示ArrayList集合
|
||||
ArrayList<Asset> assetArrayList = new ArrayList<>();
|
||||
try {
|
||||
Connection con = DriverManager.getConnection(url, user, password);
|
||||
if (con!=null){
|
||||
Statement statement = con.createStatement();
|
||||
|
||||
String query = "SHOW TABLES";
|
||||
|
||||
ResultSet resultSet = statement.executeQuery(query);
|
||||
|
||||
while (resultSet.next()) {
|
||||
String string = resultSet.getString(1);
|
||||
arrayList.add(string);
|
||||
}
|
||||
|
||||
//数据库表对象ArrayList集合
|
||||
ArrayList<DatabaseTable> tableArrayList = new ArrayList<>();
|
||||
//查询出表结构SQL
|
||||
String mysql = postgresSqlTable+
|
||||
" WHERE\n" +
|
||||
" TABLE_SCHEMA = ?\n" +
|
||||
" AND TABLE_NAME = ?";
|
||||
arrayList.forEach(res->{
|
||||
try {
|
||||
PreparedStatement preparedStatement = con.prepareStatement(mysql);
|
||||
preparedStatement.setString(1,dataSourceQueryReq.getDatabaseName());
|
||||
preparedStatement.setString(2,res);
|
||||
|
||||
ResultSet executeQuery = preparedStatement.executeQuery();
|
||||
while (executeQuery.next()) {
|
||||
DatabaseTable databaseTable = new DatabaseTable();
|
||||
databaseTable.setTableName(res);
|
||||
databaseTable.setName( executeQuery.getString("name"));
|
||||
databaseTable.setComment( executeQuery.getString("comment"));
|
||||
databaseTable.setIsPrimaryKey( executeQuery.getString("isPrimaryKey"));
|
||||
databaseTable.setType(executeQuery.getString("type"));
|
||||
databaseTable.setDetailType( executeQuery.getString("mappingType"));
|
||||
databaseTable.setLength( executeQuery.getLong("length"));
|
||||
databaseTable.setDecimalPlaces( executeQuery.getDouble("decimalPlaces"));
|
||||
databaseTable.setIsNull( executeQuery.getString("isNull"));
|
||||
databaseTable.setDefaultValue( executeQuery.getString("defaultValue"));
|
||||
databaseTable.setDatabaseName(dataSourceQueryReq.getDatabaseName());
|
||||
|
||||
String selectedSql = "SELECT * FROM " +res;
|
||||
|
||||
Statement conStatement = con.createStatement();
|
||||
ResultSet executedQuery = conStatement.executeQuery(selectedSql);
|
||||
ResultSetMetaData metaData = executedQuery.getMetaData();
|
||||
int columnCount = metaData.getColumnCount();
|
||||
|
||||
|
||||
|
||||
while (executedQuery.next()) {
|
||||
for (int i = 1; i <= columnCount; i++) {
|
||||
Asset asset = new Asset();
|
||||
|
||||
String catalogName = metaData.getColumnName(i);
|
||||
String key =dataSourceQueryReq.getAccessSourceName()+"-"+res+"-"+catalogName;
|
||||
|
||||
asset.setAssetKey(key);
|
||||
|
||||
asset.setValue(executedQuery.getObject(i));
|
||||
|
||||
asset.setType(metaData.getColumnTypeName(i));
|
||||
|
||||
asset.setDatabaseId(dataSourceQueryReq.getId());
|
||||
asset.setTableName(res);
|
||||
assetArrayList.add(asset);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
List<Asset> uniqueAssets = assetArrayList.stream()
|
||||
.collect(Collectors.toMap(
|
||||
asset -> Arrays.asList(asset.getAssetKey(), asset.getType(), asset.getValue(), asset.getDatabaseId(), asset.getTableName()),
|
||||
Function.identity(),
|
||||
(existing, replacement) -> existing,
|
||||
LinkedHashMap::new))
|
||||
.values() // 获取值的集合,即去重后的 Asset 对象集合
|
||||
.stream() // 转换回 Stream
|
||||
.toList();
|
||||
for (Asset uniqueAsset : uniqueAssets) {
|
||||
assetMapper.insert(uniqueAsset);
|
||||
}
|
||||
return Result.success("连接成功");
|
||||
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
} catch (Exception e) {
|
||||
log.error("连接失败:"+e.getMessage());
|
||||
return Result.error("连接失败");
|
||||
}
|
||||
|
||||
return Result.success();
|
||||
return Result.error("连接失败");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -334,30 +451,4 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
|
|||
arrayList
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Result<String> tableParticulars(DataTableValue dataTableValue) {
|
||||
String url = "jdbc:mysql://" + dataTableValue.getHostAddress() + ":" + dataTableValue.getHostPort() + "/" + dataTableValue.getDatabaseName();
|
||||
String UserName = dataTableValue.getDatabaseUserName();
|
||||
String password = dataTableValue.getDatabaseUserPassword();
|
||||
|
||||
try {
|
||||
Connection con = DriverManager.getConnection(url, UserName, password);
|
||||
|
||||
PreparedStatement preparedStatement = con.prepareStatement("SELECT * FROM " + dataTableValue.getTableName());
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
|
||||
while(resultSet.next()) {
|
||||
String userId = resultSet.getString("user_id");
|
||||
String name = resultSet.getString("name");
|
||||
int age = resultSet.getInt("pwd");
|
||||
log.info("user_id:{},name:{},pwd:{}", userId, name, age);
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue