diff --git a/hz-pm-api/src/main/java/com/hz/pm/api/sys/controller/SqlRunnerController.java b/hz-pm-api/src/main/java/com/hz/pm/api/sys/controller/SqlRunnerController.java new file mode 100644 index 0000000..bf9bd98 --- /dev/null +++ b/hz-pm-api/src/main/java/com/hz/pm/api/sys/controller/SqlRunnerController.java @@ -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; + +/** + *

+ * SqlRunnerController + *

+ * + * @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); + } + +} diff --git a/hz-pm-api/src/main/java/com/hz/pm/api/sys/manage/SqlRunnerManage.java b/hz-pm-api/src/main/java/com/hz/pm/api/sys/manage/SqlRunnerManage.java new file mode 100644 index 0000000..dc0791c --- /dev/null +++ b/hz-pm-api/src/main/java/com/hz/pm/api/sys/manage/SqlRunnerManage.java @@ -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; + +/** + *

+ * SqlRunnerManage + *

+ * + * @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异常"); + } + } + +} diff --git a/hz-pm-api/src/main/java/com/hz/pm/api/sys/model/req/SqlRunnerReq.java b/hz-pm-api/src/main/java/com/hz/pm/api/sys/model/req/SqlRunnerReq.java new file mode 100644 index 0000000..5c6dbd8 --- /dev/null +++ b/hz-pm-api/src/main/java/com/hz/pm/api/sys/model/req/SqlRunnerReq.java @@ -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; + +/** + *

+ * SqlRunnerReq + *

+ * + * @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; + +} diff --git a/hz-pm-api/src/main/resources/application-dev.yml b/hz-pm-api/src/main/resources/application-dev.yml index 96b816e..e81019d 100644 --- a/hz-pm-api/src/main/resources/application-dev.yml +++ b/hz-pm-api/src/main/resources/application-dev.yml @@ -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 \ No newline at end of file + open: true +sql-runner: + open: true + token: CXEMVQJ7N0219LJURFDLO9J9 \ No newline at end of file diff --git a/hz-pm-api/src/main/resources/application-prod.yml b/hz-pm-api/src/main/resources/application-prod.yml index aa98713..62ac377 100644 --- a/hz-pm-api/src/main/resources/application-prod.yml +++ b/hz-pm-api/src/main/resources/application-prod.yml @@ -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 * * ? \ No newline at end of file + cron: 0 0/5 6-22 * * ? +sql-runner: + open: true + token: CXEMVQJ7N0219LJURFDLO9J5 \ No newline at end of file