spring boot 执行sql文件

时间:2025-01-24 16:28:42

现有个需求需要动态连接到不同数据库,执行sql文件,类似于navicat这种,可以执行sql

1.导入依赖

<dependency>
            <groupId></groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
</dependency>

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>
</dependency>

2.在启动类中加入

@SpringBootApplication(exclude = )

防止spring 项目启动的时候自动注入数据库连接信息(如果项目中有依赖数据库跳过该步骤)

3.

package ;


import .*;
import ;
import ;
import ;

import ;
import .slf4j.Slf4j;
import ;
import ;

/**
 * @Author: li
 * @Date: 2020/11/5/005 19:58
 * @describe sql
 */
@Slf4j
public class SqlExecutor {

    public static ExecuteResult executeSql(String taskStepId, String url, String userName, String userPassword, String sql) {
        ExecuteResult executeResult = new ExecuteResult();
        (taskStepId);

        //sql执行输出流
        StringWriter succeedWriter = new StringWriter();
        PrintWriter succeedOut = new PrintWriter(succeedWriter);
        StringWriter errorWriter = new StringWriter();
        PrintWriter errorOut = new PrintWriter(errorWriter);
        Connection conn = null;
        ScriptRunner runner;

        try {
            conn = getMySqlConnection(url, userName, userPassword);
        } catch (Exception e) {
            ("数据库连接错误");
            (().toString());
            try {
                ();
            } catch (Exception ee) {
                (url + "关闭连接错误!");
            }
            return executeResult;
        }
        runner = new ScriptRunner(conn);
        //设置字符集,不然中文乱码插入错误
        (("UTF-8"));
        (false);
        (true);
        //设置日志
        (succeedOut);
        (errorOut);
        //遇到错误停止
        (true);
        // 绝对路径读取
//            Reader read = new FileReader(new File("C:\\Users\\Ucmed\\Documents\\"));
        // 从class目录下直接读取
//            Reader read = ("");
        Reader read = new StringReader(sql);
        try {
            (read);
        } catch (Exception e) {
            ("sql脚本执行发生异常");
            (());
            return executeResult;
        } finally {
            try {
                ();
                ();
            } catch (Exception e) {
                (url + "关闭连接错误!");
            }
        }
        (true);
        (());
        ("执行成功");
        return executeResult;
    }

    /**
     * @return
     * @throws Exception
     * @功能描述: 获取数据库连接
     */
    public static Connection getMySqlConnection(String url, String userName, String userPassword) throws Exception {
        ("");
        return (url, userName, userPassword);
    }

    public static void main(String[] args) {
        ExecuteResult executeResult = ("123", "jdbc:mysql://192.168.2.42:30306/manager?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai", "root", "123456", "select * from project;");
        (executeResult);
    }
}

 

 

package ;

/**
 * @Author: li
 * @Date: 2020/12/11/011 13:48
 * @describe 接口返回封装类
 */
public class ExecuteResult {

    private String taskStepId;

    private boolean isSucceed = false;

    private String status;

    private String logs;

    public String getTaskStepId() {
        return taskStepId;
    }

    public void setTaskStepId(String taskStepId) {
         = taskStepId;
    }

    public boolean isSucceed() {
        return isSucceed;
    }

    public void setSucceed(boolean succeed) {
        isSucceed = succeed;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
         = status;
    }

    public String getLog() {
        return logs;
    }

    public void setLog(String log) {
         = log;
    }

    @Override
    public String toString() {
        return "ExecuteResult{" +
                "taskStepId='" + taskStepId + '\'' +
                ", isSucceed=" + isSucceed +
                ", status='" + status + '\'' +
                ", logs='" + logs + '\'' +
                '}';
    }
}