fix:完善规则引擎版本,使用连接池
parent
72ccb2bc0f
commit
40c9516a49
|
@ -28,5 +28,10 @@
|
|||
<version>3.6.3</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-data-source-common</artifactId>
|
||||
<version>3.6.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package com.muyu.data.source.client.config;
|
||||
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.data.source.client.jdbcUtils.JDBCConcreteClass;
|
||||
import com.muyu.data.source.config.DataSourceConfig;
|
||||
import com.muyu.data.source.domain.Children;
|
||||
import com.muyu.data.source.domain.DataSource;
|
||||
import com.muyu.data.source.domain.DatabaseType;
|
||||
|
@ -23,60 +25,66 @@ import org.springframework.context.annotation.Import;
|
|||
/**
|
||||
* 数据接入客户端配置类
|
||||
*
|
||||
* @author CHX
|
||||
* on 2024/5/9 星期四
|
||||
* @author CHX on 2024/5/9 星期四
|
||||
*/
|
||||
@ComponentScan
|
||||
@Import(value = {SourceClientRunner.class})
|
||||
public class SourceClientConfig {
|
||||
|
||||
@Autowired
|
||||
private RemoteDataSourceService remoteDataSourceService;
|
||||
@Autowired
|
||||
private RemoteDataTypeService remoteDataTypeService;
|
||||
|
||||
@Autowired
|
||||
private RemoteChildrenService remoteChildrenService;
|
||||
|
||||
|
||||
public List<List<DataModel>> getDataModel(Long id) {
|
||||
List<List<DataModel>> list = new ArrayList<>();
|
||||
//根据表id查询表结构对象
|
||||
Result<Children> childrenResult = remoteChildrenService.selectChildren(id);
|
||||
Children children = childrenResult.getData();
|
||||
//根据数据源id查询数据源对象
|
||||
Result<DataSource> dataSourceResult = remoteDataSourceService.getDataSourceById(children.getAssetId());
|
||||
DataSource dataSource = dataSourceResult.getData();
|
||||
Result<DatabaseType> databaseTypeResult = remoteDataTypeService.getDataType(dataSource.getDataType());
|
||||
DatabaseType databaseType = databaseTypeResult.getData();
|
||||
String jdbcUrl=databaseType.getUrlPre()+dataSource.getHost()+":"+dataSource.getPort()+"/"+dataSource.getDatabaseName()+"?"+dataSource.getConnectionParam();
|
||||
//sql
|
||||
String sql = "";
|
||||
if ("mysql".equals(dataSource.getDataType())){
|
||||
switch (dataSource.getDataType()) {
|
||||
case "mysql":
|
||||
sql = "select * from" + children.getName();
|
||||
break;
|
||||
case "postgresql":
|
||||
sql="select * from "+dataSource.getDatabaseName()+"."+children.getName();
|
||||
break;
|
||||
|
||||
}
|
||||
try {
|
||||
Class.forName(databaseType.getDriverManager());
|
||||
Connection connection = DriverManager.getConnection(jdbcUrl, dataSource.getUser(), dataSource.getPassword());
|
||||
PreparedStatement ps = connection.prepareStatement(sql);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
//调用连接池,获取连接
|
||||
// 调用连接池 获取连接
|
||||
Connection connection = DataSourceConfig.getConnection(dataSource.getId());
|
||||
JDBCConcreteClass jdbcConcreteClass = new JDBCConcreteClass();
|
||||
// 调用抽象类 获取jdbc查询结果
|
||||
ResultSet rs = jdbcConcreteClass.getResultSet(connection, sql);
|
||||
ResultSetMetaData metaData = rs.getMetaData();
|
||||
List<DataModel> dataModelList = new ArrayList<>();
|
||||
while (rs.next()) {
|
||||
for (int i = 1; i <= metaData.getColumnCount(); i++) {
|
||||
// 字段名称
|
||||
String columnName = metaData.getColumnName(i);
|
||||
// 字段值
|
||||
Object value = rs.getObject(i);
|
||||
// 字段类型
|
||||
String columnTypeName = metaData.getColumnTypeName(i);
|
||||
// 字段java映射类型
|
||||
String columnClassName = metaData.getColumnClassName(i);
|
||||
DataModel dataModel = DataModel.builder()
|
||||
.key(columnName)
|
||||
.value(value)
|
||||
.sourceType(columnTypeName)
|
||||
.processType(columnClassName)
|
||||
.processClass(Class.forName(columnClassName))
|
||||
.build();
|
||||
DataModel dataModel = DataModel.builder().key(columnName).value(value).sourceType(columnTypeName)
|
||||
.processType(columnClassName).processClass(Class.forName(columnClassName)).build();
|
||||
dataModelList.add(dataModel);
|
||||
}
|
||||
list.add(dataModelList);
|
||||
|
||||
}
|
||||
|
||||
// 归还连接
|
||||
DataSourceConfig.returnConn(connection);
|
||||
} catch (ClassNotFoundException | SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
|
|
@ -43,25 +43,5 @@ public class SourceClientRunner implements ApplicationRunner {
|
|||
DatabaseType databaseType = databaseTypeResult.getData();
|
||||
DataSourceConfig.init(dataSource,databaseType);
|
||||
});
|
||||
// Long key = dataSourceList.get(0).getId();
|
||||
// log.info("查看连接池");
|
||||
// DataSourceConfig.size(key);
|
||||
// Thread.sleep(500);
|
||||
// log.info("取出一个连接,查看连接池");
|
||||
// Connection connection = DataSourceConfig.getConnection(key);
|
||||
// DataSourceConfig.size(key);
|
||||
// Thread.sleep(500);
|
||||
// JDBCConcreteClass jdbcConcreteClass = new JDBCConcreteClass();
|
||||
// ResultSet resultSet = jdbcConcreteClass.getResultSet(connection, "select * from engine_maintenance");
|
||||
// ResultSetMetaData metaData = resultSet.getMetaData();
|
||||
// for (int i = 1; i <= metaData.getColumnCount(); i++) {
|
||||
// log.info("字段名称:{}",metaData.getColumnClassName(i));
|
||||
// log.info("数据库类型:{}",metaData.getColumnTypeName(i));
|
||||
// log.info("java类型:{}",metaData.getColumnClassName(i));
|
||||
// }
|
||||
//
|
||||
// log.info("返回一个连接,查看连接池");
|
||||
// DataSourceConfig.returnConn(connection);
|
||||
// DataSourceConfig.size(key);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@ import org.springframework.stereotype.Component;
|
|||
public class DataSourceConfig {
|
||||
private static HashMap<Long, DruidDataSource> dataSourceMap=new HashMap<>();
|
||||
|
||||
//初始化连接
|
||||
public static void init(DataSource dataSource, DatabaseType databaseType){
|
||||
DruidDataSource druidDataSource = new DruidDataSource();
|
||||
druidDataSource.setUsername(dataSource.getUser());
|
||||
|
@ -42,6 +43,7 @@ public class DataSourceConfig {
|
|||
dataSourceMap.put(dataSource.getId(), druidDataSource);
|
||||
}
|
||||
|
||||
//获取连接
|
||||
public static Connection getConnection(Long key){
|
||||
DruidDataSource druidDataSource=dataSourceMap.get(key);
|
||||
try {
|
||||
|
@ -52,6 +54,7 @@ public class DataSourceConfig {
|
|||
}
|
||||
}
|
||||
|
||||
//归还连接
|
||||
public static void returnConn(Connection connection){
|
||||
try {
|
||||
connection.close();
|
||||
|
|
|
@ -24,6 +24,11 @@ public class TestController {
|
|||
@Autowired
|
||||
private TestService testService;
|
||||
|
||||
/**
|
||||
* 获取数据实例集合
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/getDataModelList/{id}")
|
||||
public Result<List<List<DataModel>>> getDataModelList(@PathVariable("id")Long id){
|
||||
List<List<DataModel>> dataModelList=testService.getDataModelList(id);
|
||||
|
|
Loading…
Reference in New Issue