This commit is contained in:
zlwu committed 2025-08-13 15:17:37 +08:00
1 parent c49a28bd24
commit a2a0f54e84
2 files changed
+100 -95

No files matched your search

@@ -1,7 +1,7 @@
package org.dromara.rs.controller;
import org.dromara.common.core.domain.R;
import org.dromara.rs.mapper.RsSystemMapper;
// import org.dromara.rs.mapper.RsSystemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -23,66 +23,69 @@ import java.util.Map;
@RequestMapping("/rs/db")
public class SqlUpdateController {
@Autowired
private RsSystemMapper rsSystemMapper;
// 编译不过,吴自龙注释,
// @Autowired
// private RsSystemMapper rsSystemMapper;
/**
* 执行培训模块SQL更新脚本
*/
@GetMapping("/update/training")
public R<Map<String, Object>> executeTrainingUpdateScript() {
Map<String, Object> result = new HashMap<>();
try {
// 读取SQL脚本
InputStream is = getClass().getClassLoader().getResourceAsStream("mapper/rs/training-update.sql");
if (is == null) {
result.put("success", false);
result.put("message", "SQL脚本文件不存在");
return R.ok(result);
}
// @GetMapping("/update/training")
// public R<Map<String, Object>> executeTrainingUpdateScript() {
// Map<String, Object> result = new HashMap<>();
// try {
// // 读取SQL脚本
// InputStream is =
// getClass().getClassLoader().getResourceAsStream("mapper/rs/training-update.sql");
// if (is == null) {
// result.put("success", false);
// result.put("message", "SQL脚本文件不存在");
// return R.ok(result);
// }
// 解析SQL语句
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
// 忽略注释行
if (line.trim().startsWith("--") || line.trim().isEmpty()) {
continue;
}
sb.append(line);
// 如果是SQL语句结束
if (line.trim().endsWith(";")) {
String sql = sb.toString().trim();
if (sql.endsWith(";")) {
sql = sql.substring(0, sql.length() - 1); // 移除分号,MyBatis会自动处理
}
executeSql(sql);
sb = new StringBuilder();
}
}
result.put("success", true);
result.put("message", "SQL脚本执行成功");
return R.ok(result);
} catch (Exception e) {
e.printStackTrace();
result.put("success", false);
result.put("message", "执行SQL脚本出错: " + e.getMessage());
return R.fail("执行SQL脚本出错: " + e.getMessage());
}
}
// // 解析SQL语句
// BufferedReader reader = new BufferedReader(new InputStreamReader(is));
// StringBuilder sb = new StringBuilder();
// String line;
// while ((line = reader.readLine()) != null) {
// // 忽略注释行
// if (line.trim().startsWith("--") || line.trim().isEmpty()) {
// continue;
// }
// sb.append(line);
// // 如果是SQL语句结束
// if (line.trim().endsWith(";")) {
// String sql = sb.toString().trim();
// if (sql.endsWith(";")) {
// sql = sql.substring(0, sql.length() - 1); // 移除分号,MyBatis会自动处理
// }
// executeSql(sql);
// sb = new StringBuilder();
// }
// }
// result.put("success", true);
// result.put("message", "SQL脚本执行成功");
// return R.ok(result);
// } catch (Exception e) {
// e.printStackTrace();
// result.put("success", false);
// result.put("message", "执行SQL脚本出错: " + e.getMessage());
// return R.fail("执行SQL脚本出错: " + e.getMessage());
// }
// }
//// 编译不过,吴自龙注释,
/**
* 执行单条SQL语句
*/
private void executeSql(String sql) {
try {
rsSystemMapper.executeSql(sql);
} catch (Exception e) {
// 记录错误但继续执行
System.out.println("SQL执行警告: " + e.getMessage());
}
}
}
// private void executeSql(String sql) {
// try {
// rsSystemMapper.executeSql(sql);
// } catch (Exception e) {
// // 记录错误但继续执行
// System.out.println("SQL执行警告: " + e.getMessage());
// }
// }
}
@@ -1,6 +1,6 @@
package org.dromara.rs.util;
import org.dromara.rs.mapper.RsSystemMapper;
// import org.dromara.rs.mapper.RsSystemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
@@ -14,42 +14,44 @@ import java.io.InputStreamReader;
@Component
public class SqlScriptRunner {
@Autowired
private RsSystemMapper rsSystemMapper;
// 吴自龙注释,编译不过
// @Autowired
// private RsSystemMapper rsSystemMapper;
/**
* 执行SQL脚本
*
* @param scriptPath 脚本路径(相对于classpath)
* @throws Exception 执行异常
*/
public void executeScript(String scriptPath) throws Exception {
ClassPathResource resource = new ClassPathResource(scriptPath);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream()))) {
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
// 忽略注释行和空行
if (line.trim().startsWith("--") || line.trim().isEmpty()) {
continue;
}
sb.append(line);
// 如果是SQL语句结束
if (line.trim().endsWith(";")) {
String sql = sb.toString().trim();
if (sql.endsWith(";")) {
sql = sql.substring(0, sql.length() - 1); // 移除分号,MyBatis会自动处理
}
try {
rsSystemMapper.executeSql(sql);
} catch (Exception e) {
// 记录错误但继续执行
System.out.println("SQL执行警告: " + e.getMessage());
}
sb = new StringBuilder();
}
}
}
}
}
// /**
// * 执行SQL脚本
// *
// * @param scriptPath 脚本路径(相对于classpath)
// * @throws Exception 执行异常
// */
// public void executeScript(String scriptPath) throws Exception {
// ClassPathResource resource = new ClassPathResource(scriptPath);
// try (BufferedReader reader = new BufferedReader(new
// InputStreamReader(resource.getInputStream()))) {
// StringBuilder sb = new StringBuilder();
// String line;
// while ((line = reader.readLine()) != null) {
// // 忽略注释行和空行
// if (line.trim().startsWith("--") || line.trim().isEmpty()) {
// continue;
// }
// sb.append(line);
// // 如果是SQL语句结束
// if (line.trim().endsWith(";")) {
// String sql = sb.toString().trim();
// if (sql.endsWith(";")) {
// sql = sql.substring(0, sql.length() - 1); // 移除分号,MyBatis会自动处理
// }
// try {
// rsSystemMapper.executeSql(sql);
// } catch (Exception e) {
// // 记录错误但继续执行
// System.out.println("SQL执行警告: " + e.getMessage());
// }
// sb = new StringBuilder();
// }
// }
// }
// }
}