@@ -0,0 +1,35 @@ | |||
package com.hz.pm.api.sys.controller; | |||
import com.hz.pm.api.sys.manage.SqlRunnerManage; | |||
import com.hz.pm.api.sys.model.req.SqlRunnerReq; | |||
import lombok.RequiredArgsConstructor; | |||
import org.springframework.web.bind.annotation.PostMapping; | |||
import org.springframework.web.bind.annotation.RequestBody; | |||
import org.springframework.web.bind.annotation.RequestMapping; | |||
import org.springframework.web.bind.annotation.RestController; | |||
import springfox.documentation.annotations.ApiIgnore; | |||
import javax.validation.Valid; | |||
/** | |||
* <p> | |||
* SqlRunnerController | |||
* </p> | |||
* | |||
* @author WendyYang | |||
* @since 23:04 2024/12/16 | |||
*/ | |||
@ApiIgnore | |||
@RestController | |||
@RequiredArgsConstructor | |||
@RequestMapping("/api/v1/sqlRunner") | |||
public class SqlRunnerController { | |||
private final SqlRunnerManage sqlRunnerManage; | |||
@PostMapping("/execute") | |||
public Object execute(@Valid @RequestBody SqlRunnerReq req) { | |||
return sqlRunnerManage.execute(req); | |||
} | |||
} |
@@ -0,0 +1,55 @@ | |||
package com.hz.pm.api.sys.manage; | |||
import cn.hutool.json.JSONUtil; | |||
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner; | |||
import com.hz.pm.api.common.exception.ReturnException; | |||
import com.hz.pm.api.sys.model.req.SqlRunnerReq; | |||
import com.ningdatech.basic.exception.BizException; | |||
import org.springframework.beans.factory.annotation.Value; | |||
import org.springframework.stereotype.Component; | |||
import org.springframework.transaction.annotation.Transactional; | |||
/** | |||
* <p> | |||
* SqlRunnerManage | |||
* </p> | |||
* | |||
* @author WendyYang | |||
* @since 22:43 2024/12/16 | |||
*/ | |||
@Component | |||
public class SqlRunnerManage { | |||
@Value("${sql-runner.token:}") | |||
private String token; | |||
@Value("${sql-runner.open:false}") | |||
private Boolean open; | |||
@Transactional(rollbackFor = Exception.class) | |||
public synchronized Object execute(SqlRunnerReq req) { | |||
if (Boolean.FALSE.equals(open)) { | |||
throw ReturnException.wrap("未开启执行sql功能"); | |||
} | |||
if (!token.equals(req.getExecToken())) { | |||
throw ReturnException.wrap("execToken错误"); | |||
} | |||
try (SqlRunner runner = SqlRunner.db()) { | |||
switch (req.getExecType()) { | |||
case DELETE: | |||
return runner.delete(req.getExecSql()); | |||
case INSERT: | |||
return runner.insert(req.getExecSql()); | |||
case SELECT: | |||
return runner.selectList(req.getExecSql()); | |||
case UPDATE: | |||
return runner.update(req.getExecSql()); | |||
default: | |||
throw ReturnException.wrap("不支持的execType"); | |||
} | |||
} catch (Exception e) { | |||
throw BizException.wrap("执行sql异常"); | |||
} | |||
} | |||
} |
@@ -0,0 +1,40 @@ | |||
package com.hz.pm.api.sys.model.req; | |||
import lombok.Data; | |||
import javax.validation.constraints.NotBlank; | |||
import javax.validation.constraints.NotNull; | |||
/** | |||
* <p> | |||
* SqlRunnerReq | |||
* </p> | |||
* | |||
* @author WendyYang | |||
* @since 22:48 2024/12/16 | |||
*/ | |||
@Data | |||
public class SqlRunnerReq { | |||
public enum ExecType { | |||
INSERT, | |||
UPDATE, | |||
DELETE, | |||
SELECT | |||
} | |||
@NotBlank(message = "execToken不能为空") | |||
private String execToken; | |||
@NotNull(message = "execType不能为空") | |||
private ExecType execType; | |||
@NotBlank(message = "execSql不能为空") | |||
private String execSql; | |||
} |
@@ -44,7 +44,7 @@ spring: | |||
datasource: | |||
type: com.zaxxer.hikari.HikariDataSource | |||
driverClassName: dm.jdbc.driver.DmDriver | |||
url: jdbc:dm://47.98.125.47:5236/HZ_PROJECT_MANAGEMENT1?nullToEmpty=true&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8 | |||
url: jdbc:dm://47.98.125.47:5236/HZ_PROJECT_MANAGEMENT1?nullToEmpty=true&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8&clobAsString=true | |||
username: SYSDBA | |||
password: Ndkj@1104@DM | |||
# 数据源 | |||
@@ -77,6 +77,7 @@ mybatis-plus: | |||
db-config: | |||
logic-delete-value: 1 | |||
logic-not-delete-value: 0 | |||
enable-sql-runner: true | |||
logging: | |||
config: classpath:logback-spring.xml | |||
#日志配置 | |||
@@ -238,4 +239,7 @@ major-project-eval: | |||
ding-talk: | |||
client: | |||
open: true | |||
open: true | |||
sql-runner: | |||
open: true | |||
token: CXEMVQJ7N0219LJURFDLO9J9 |
@@ -46,7 +46,7 @@ spring: | |||
datasource: | |||
type: com.zaxxer.hikari.HikariDataSource | |||
driverClassName: dm.jdbc.driver.DmDriver | |||
url: jdbc:dm://10.54.38.191:5236/HZ_PROJECT_MANAGEMENT?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8 | |||
url: jdbc:dm://10.54.38.191:5236/HZ_PROJECT_MANAGEMENT?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8&clobAsString=true | |||
username: XMXTGL | |||
password: XMXTGL@2023 | |||
# 数据源 | |||
@@ -79,6 +79,7 @@ mybatis-plus: | |||
db-config: | |||
logic-delete-value: 1 | |||
logic-not-delete-value: 0 | |||
enable-sql-runner: true | |||
logging: | |||
config: classpath:logback-spring.xml | |||
#日志配置 | |||
@@ -258,4 +259,7 @@ early-warning-without-submit: | |||
open: true | |||
major-project-eval: | |||
open: true | |||
cron: 0 0/5 6-22 * * ? | |||
cron: 0 0/5 6-22 * * ? | |||
sql-runner: | |||
open: true | |||
token: CXEMVQJ7N0219LJURFDLO9J5 |