auto(2b9ed1c9fb01): session end @ 2026-07-07T15:55:04 (+3f)
This commit is contained in:
1 parent
bc2706aa7c
commit
c55a1fa09b
3 files changed
+340
No files matched your search
@@ -0,0 +1,61 @@
|
||||
package ssw.common.service;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 数据库连接服务接口
|
||||
*/
|
||||
public interface DbLinkService {
|
||||
|
||||
/**
|
||||
* 创建数据库连接
|
||||
*
|
||||
* @param fullName 连接名称
|
||||
* @param dbType 数据库类型 (MySQL/PostgreSQL/Oracle/SQLServer)
|
||||
* @param host 主机地址
|
||||
* @param port 端口
|
||||
* @param userName 用户名
|
||||
* @param password 密码
|
||||
* @param serviceName 服务名/数据库名
|
||||
* @param description 描述
|
||||
* @return 创建成功返回数据源ID
|
||||
*/
|
||||
String createDbLink(String fullName, String dbType, String host, Integer port,
|
||||
String userName, String password, String serviceName, String description);
|
||||
|
||||
/**
|
||||
* 根据编码获取数据库连接信息
|
||||
*
|
||||
* @param enCode 连接编码
|
||||
* @return 连接ID
|
||||
*/
|
||||
String getIdByEnCode(String enCode);
|
||||
|
||||
/**
|
||||
* 测试数据库连接
|
||||
*
|
||||
* @param dbType 数据库类型
|
||||
* @param host 主机地址
|
||||
* @param port 端口
|
||||
* @param userName 用户名
|
||||
* @param password 密码
|
||||
* @param serviceName 服务名/数据库名
|
||||
* @return 测试结果
|
||||
*/
|
||||
Map<String, Object> testConnection(String dbType, String host, Integer port,
|
||||
String userName, String password, String serviceName);
|
||||
|
||||
/**
|
||||
* 创建数据库
|
||||
*
|
||||
* @param host 主机地址
|
||||
* @param port 端口
|
||||
* @param userName 用户名
|
||||
* @param password 密码
|
||||
* @param dbName 数据库名
|
||||
* @param dbType 数据库类型 (MySQL/PostgreSQL)
|
||||
* @return 创建结果
|
||||
*/
|
||||
boolean createDatabase(String host, Integer port, String userName, String password,
|
||||
String dbName, String dbType);
|
||||
}
|
||||
+174
@@ -0,0 +1,174 @@
|
||||
package ssw.common.service.impl;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ssw.common.service.DbLinkService;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 数据库连接服务实现
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class ExtDbLinkServiceImpl implements DbLinkService {
|
||||
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
@Override
|
||||
public String createDbLink(String fullName, String dbType, String host, Integer port,
|
||||
String userName, String password, String serviceName, String description) {
|
||||
// 检查是否已存在
|
||||
String checkSql = "SELECT F_Id FROM base_db_link WHERE F_Full_Name = ?";
|
||||
try {
|
||||
Map<String, Object> existResult = jdbcTemplate.queryForMap(checkSql, fullName);
|
||||
String existId = (String) existResult.get("F_Id");
|
||||
log.warn("数据源已存在: {}, id={}", fullName, existId);
|
||||
return existId;
|
||||
} catch (Exception e) {
|
||||
// 不存在,继续创建
|
||||
}
|
||||
|
||||
// 创建新数据源(使用雪花ID)
|
||||
String id = String.valueOf(IdUtil.getSnowflakeNextId());
|
||||
String insertSql = "INSERT INTO base_db_link " +
|
||||
"(F_Id, F_Full_Name, F_Db_Type, F_Host, F_Port, F_User_Name, F_Password, F_Service_Name, " +
|
||||
"F_Description, F_Db_Schema, F_Table_Space, F_Oracle_Param, F_Sort_Code, " +
|
||||
"F_Creator_Time, F_Creator_User_Id, F_Last_Modify_Time, F_Last_Modify_User_Id, " +
|
||||
"F_Delete_Mark, F_Tenant_Id) " +
|
||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, '', '', '{}', 0, NOW(), '349057407209541', NOW(), '349057407209541', 1, '0')";
|
||||
|
||||
jdbcTemplate.update(insertSql, id, fullName, dbType, host, port, userName, password, serviceName, description);
|
||||
log.info("数据源创建成功: id={}, fullName={}", id, fullName);
|
||||
|
||||
// 输出需要手动添加的配置信息 (URL/driver 动态按 dbType, 不再硬编码 MySQL)
|
||||
log.info("========== 请手动添加到 application-dev.yml ==========");
|
||||
log.info("在 datasource 配置块后添加:");
|
||||
log.info(" {}:", fullName);
|
||||
log.info(" url: {}", buildJdbcUrl(dbType, port, host, serviceName));
|
||||
log.info(" username: ${{spring.datasource.username}}");
|
||||
log.info(" password: ${{spring.datasource.password}}");
|
||||
log.info(" driver-class-name: {}", getDriverClassName(dbType));
|
||||
log.info("======================================================");
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIdByEnCode(String enCode) {
|
||||
String sql = "SELECT F_Id FROM base_db_link WHERE F_Full_Name = ?";
|
||||
try {
|
||||
Map<String, Object> result = jdbcTemplate.queryForMap(sql, enCode);
|
||||
return (String) result.get("F_Id");
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> testConnection(String dbType, String host, Integer port,
|
||||
String userName, String password, String serviceName) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("success", false);
|
||||
result.put("message", "");
|
||||
|
||||
Connection conn = null;
|
||||
try {
|
||||
Class.forName(getDriverClassName(dbType));
|
||||
String url = buildJdbcUrl(dbType, port, host, serviceName);
|
||||
conn = DriverManager.getConnection(url, userName, password);
|
||||
result.put("success", true);
|
||||
result.put("message", "连接成功");
|
||||
} catch (Exception e) {
|
||||
log.error("数据库连接测试失败", e);
|
||||
result.put("success", false);
|
||||
result.put("message", e.getMessage());
|
||||
} finally {
|
||||
if (conn != null) {
|
||||
try {
|
||||
conn.close();
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean createDatabase(String host, Integer port, String userName, String password,
|
||||
String dbName, String dbType) {
|
||||
Connection conn = null;
|
||||
try {
|
||||
Class.forName(getDriverClassName(dbType));
|
||||
// 连接时不指定数据库
|
||||
String url = buildJdbcUrl(dbType, port, host, null);
|
||||
conn = DriverManager.getConnection(url, userName, password);
|
||||
|
||||
String sql;
|
||||
if ("MySQL".equalsIgnoreCase(dbType)) {
|
||||
sql = "CREATE DATABASE IF NOT EXISTS `" + dbName + "` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci";
|
||||
} else if ("PostgreSQL".equalsIgnoreCase(dbType)) {
|
||||
sql = "CREATE DATABASE \"" + dbName + "\"";
|
||||
} else {
|
||||
throw new RuntimeException("不支持的数据库类型: " + dbType);
|
||||
}
|
||||
|
||||
try (PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.executeUpdate();
|
||||
}
|
||||
log.info("数据库创建成功: {}", dbName);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("数据库创建失败: dbName={}", dbName, e);
|
||||
return false;
|
||||
} finally {
|
||||
if (conn != null) {
|
||||
try {
|
||||
conn.close();
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String getDriverClassName(String dbType) {
|
||||
switch (dbType.toLowerCase()) {
|
||||
case "mysql":
|
||||
return "com.mysql.cj.jdbc.Driver";
|
||||
case "postgresql":
|
||||
return "org.postgresql.Driver";
|
||||
case "oracle":
|
||||
return "oracle.jdbc.OracleDriver";
|
||||
case "sqlserver":
|
||||
return "com.microsoft.sqlserver.jdbc.SQLServerDriver";
|
||||
default:
|
||||
return "com.mysql.cj.jdbc.Driver";
|
||||
}
|
||||
}
|
||||
|
||||
private String buildJdbcUrl(String dbType, Integer port, String host, String database) {
|
||||
if ("MySQL".equalsIgnoreCase(dbType)) {
|
||||
return String.format("jdbc:mysql://%s:%d/%s?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai",
|
||||
host, port, database != null ? database : "");
|
||||
} else if ("PostgreSQL".equalsIgnoreCase(dbType)) {
|
||||
return String.format("jdbc:postgresql://%s:%d/%s", host, port, database != null ? database : "postgres");
|
||||
} else if ("Oracle".equalsIgnoreCase(dbType)) {
|
||||
return String.format("jdbc:oracle:thin:@%s:%d:%s", host, port, database);
|
||||
} else if ("SQLServer".equalsIgnoreCase(dbType)) {
|
||||
return String.format("jdbc:sqlserver://%s:%d;databaseName=%s", host, port, database);
|
||||
}
|
||||
return String.format("jdbc:mysql://%s:%d/%s?useUnicode=true&characterEncoding=utf8", host, port, database);
|
||||
}
|
||||
}
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
package ssw.common.controller;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jnpf.base.ActionResult;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ssw.common.service.DbLinkService;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 数据库连接控制器
|
||||
* 提供应用初始化所需的数据库连接和创建功能
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/api/common/dblinkExt")
|
||||
public class ExtDbLinkController {
|
||||
|
||||
@Autowired
|
||||
private DbLinkService dbLinkService;
|
||||
|
||||
// 默认数据源配置(从 application-dev.yml 读取,避免密码泄漏)
|
||||
@Value("${spring.datasource.host:127.0.0.1}")
|
||||
private String defaultHost;
|
||||
|
||||
@Value("${spring.datasource.port:166}")
|
||||
private Integer defaultPort;
|
||||
|
||||
@Value("${spring.datasource.username:root}")
|
||||
private String defaultUserName;
|
||||
|
||||
@Value("${spring.datasource.password:}")
|
||||
private String defaultPassword;
|
||||
|
||||
/**
|
||||
* 创建数据库连接(数据源)
|
||||
* 默认使用系统配置的数据源参数,无需传入敏感信息
|
||||
*/
|
||||
@PostMapping("/create")
|
||||
@Operation(summary = "创建数据库连接", description = "创建新的数据库连接(数据源),默认使用系统配置")
|
||||
public ActionResult<String> createDbLink(@RequestBody Map<String, Object> params) {
|
||||
String fullName = (String) params.get("fullName");
|
||||
// 如果未提供参数,使用系统默认配置
|
||||
String dbType = (String) params.getOrDefault("dbType", "MySQL");
|
||||
String host = (String) params.getOrDefault("host", defaultHost);
|
||||
Integer port = params.get("port") != null ? ((Number) params.get("port")).intValue() : defaultPort;
|
||||
String userName = (String) params.getOrDefault("userName", defaultUserName);
|
||||
String password = (String) params.getOrDefault("password", defaultPassword);
|
||||
String serviceName = (String) params.get("serviceName");
|
||||
String description = (String) params.getOrDefault("description", "");
|
||||
|
||||
String id = dbLinkService.createDbLink(fullName, dbType, host, port, userName, password, serviceName, description);
|
||||
return ActionResult.success(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据名称获取数据源ID
|
||||
*/
|
||||
@GetMapping("/id/{enCode}")
|
||||
@Operation(summary = "获取数据源ID", description = "根据编码获取数据源ID")
|
||||
public ActionResult<String> getIdByEnCode(@PathVariable String enCode) {
|
||||
String id = dbLinkService.getIdByEnCode(enCode);
|
||||
return ActionResult.success(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试数据库连接
|
||||
* 默认使用系统配置的数据源参数
|
||||
*/
|
||||
@PostMapping("/test")
|
||||
@Operation(summary = "测试数据库连接", description = "测试数据库连接是否成功")
|
||||
public ActionResult<Map<String, Object>> testConnection(@RequestBody Map<String, Object> params) {
|
||||
String dbType = (String) params.getOrDefault("dbType", "MySQL");
|
||||
String host = (String) params.getOrDefault("host", defaultHost);
|
||||
Integer port = params.get("port") != null ? ((Number) params.get("port")).intValue() : defaultPort;
|
||||
String userName = (String) params.getOrDefault("userName", defaultUserName);
|
||||
String password = (String) params.getOrDefault("password", defaultPassword);
|
||||
String serviceName = (String) params.get("serviceName");
|
||||
|
||||
Map<String, Object> result = dbLinkService.testConnection(dbType, host, port, userName, password, serviceName);
|
||||
return ActionResult.success(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建数据库
|
||||
* 默认使用系统配置的数据源参数
|
||||
*/
|
||||
@PostMapping("/createDatabase")
|
||||
@Operation(summary = "创建数据库", description = "在指定数据库服务器上创建新数据库")
|
||||
public ActionResult<Boolean> createDatabase(@RequestBody Map<String, Object> params) {
|
||||
String host = (String) params.getOrDefault("host", defaultHost);
|
||||
Integer port = params.get("port") != null ? ((Number) params.get("port")).intValue() : defaultPort;
|
||||
String userName = (String) params.getOrDefault("userName", defaultUserName);
|
||||
String password = (String) params.getOrDefault("password", defaultPassword);
|
||||
String dbName = (String) params.get("dbName");
|
||||
String dbType = (String) params.getOrDefault("dbType", "MySQL");
|
||||
|
||||
boolean success = dbLinkService.createDatabase(host, port, userName, password, dbName, dbType);
|
||||
return ActionResult.success(success);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user