Mysql数据源提交

This commit is contained in:
liangdaliang
2025-02-20 18:07:24 +08:00
parent 3bca275ca8
commit 4ddca7f72c
3 changed files with 62 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
package com.test.common.utils;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author liangdaliang
* @Descriptionmysql执行器
* @date 2025-02-20 17:05
*/
public class MySQLExecutor {
/**
* 执行 SQL 查询,并将结果转换为 List<Map<String, Object>>
*
* @param sql SQL 查询语句(已包含参数值)
* @param url 数据库 URL
* @param user 数据库用户名
* @param password 数据库密码
* @return 查询结果(每行数据为一个 Map
*/
public static List<Map<String, Object>> executeQuery(String sql, String url, String user, String password) {
List<Map<String, Object>> result = new ArrayList<>();
try (Connection connection = DriverManager.getConnection(url, user, password);
Statement statement = connection.createStatement()) {
// 执行查询限制最大100行
try (ResultSet resultSet = statement.executeQuery(sql + " LIMIT 100")) {
// 获取结果集的元数据
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
// 遍历结果集
while (resultSet.next()) {
Map<String, Object> row = new HashMap<>();
for (int i = 1; i <= columnCount; i++) {
// 获取字段名和字段值
String columnName = metaData.getColumnLabel(i);
Object columnValue = resultSet.getObject(i);
row.put(columnName, columnValue);
}
result.add(row);
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return result;
}
}

View File

@@ -78,6 +78,10 @@ public class TestCaseStep extends BaseEntity {
@Excel(name = "http 请求协议(http, https)")
private String apiProtocol;
/** 数据源id */
@Excel(name = "数据源id")
private Long datasourceId;
/** sql 命令 */
@Excel(name = "sql 命令")
private String sqlCommand;

View File

@@ -20,6 +20,7 @@
<result property="apiHost" column="api_host"/>
<result property="apiPort" column="api_port"/>
<result property="apiProtocol" column="api_protocol"/>
<result property="datasourceId" column="datasource_id"/>
<result property="sqlCommand" column="sql_command"/>
<result property="count" column="count"/>
<result property="async" column="async"/>
@@ -52,6 +53,7 @@
api_host,
api_port,
api_protocol,
datasource_id,
sql_command,
count,
async,
@@ -86,6 +88,7 @@
<if test="apiHost != null and apiHost != ''">and api_host = #{apiHost}</if>
<if test="apiPort != null ">and api_port = #{apiPort}</if>
<if test="apiProtocol != null and apiProtocol != ''">and api_protocol = #{apiProtocol}</if>
<if test="datasourceId != null ">and datasource_id = #{datasourceId}</if>
<if test="sqlCommand != null and sqlCommand != ''">and sql_command = #{sqlCommand}</if>
<if test="count != null ">and count = #{count}</if>
<if test="async != null ">and async = #{async}</if>
@@ -121,6 +124,7 @@
<if test="apiHost != null">api_host,</if>
<if test="apiPort != null">api_port,</if>
<if test="apiProtocol != null">api_protocol,</if>
<if test="datasourceId != null">datasource_id,</if>
<if test="sqlCommand != null">sql_command,</if>
<if test="count != null">count,</if>
<if test="async != null">async,</if>
@@ -151,6 +155,7 @@
<if test="apiHost != null">#{apiHost},</if>
<if test="apiPort != null">#{apiPort},</if>
<if test="apiProtocol != null">#{apiProtocol},</if>
<if test="datasourceId != null">#{datasourceId},</if>
<if test="sqlCommand != null">#{sqlCommand},</if>
<if test="count != null">#{count},</if>
<if test="async != null">#{async},</if>
@@ -185,6 +190,7 @@
<if test="apiHost != null">api_host = #{apiHost},</if>
<if test="apiPort != null">api_port = #{apiPort},</if>
<if test="apiProtocol != null">api_protocol = #{apiProtocol},</if>
<if test="datasourceId != null">datasource_id = #{datasourceId},</if>
<if test="sqlCommand != null">sql_command = #{sqlCommand},</if>
<if test="count != null">count = #{count},</if>
<if test="async != null">async = #{async},</if>