- ExtDbLinkController.java: ssw-common 控制器 (跨 app_code 共享) - SswMapperConfig.java: 共享 @MapperScan 配置 (BS-14 集中化) 对应 CLAUDE.md §3.6 ssw-common 共享 MapperConfig 规范
106 lines
4.6 KiB
Java
106 lines
4.6 KiB
Java
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);
|
|
}
|
|
}
|