guohongjin
2024-05-15 5b7639f0bd9e056738ec15100ed0532e965c6cd5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package cn.stylefeng.roses.kernel.db.api.util;
 
import cn.hutool.core.io.IoUtil;
import cn.stylefeng.roses.kernel.db.api.exception.DaoException;
import cn.stylefeng.roses.kernel.db.api.exception.enums.DatabaseExceptionEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.jdbc.datasource.init.ScriptUtils;
 
import java.sql.Connection;
import java.sql.DriverManager;
 
/**
 * sql文件执行
 *
 * @author fengshuonan
 * @date 2019-06-18-17:10
 */
@Slf4j
public class SqlRunUtil {
 
    /**
     * 执行sql脚本文件,使用Spring工具类
     *
     * @author fengshuonan
     * @date 2021/5/19 10:52
     */
    public static void runClassPathSql(String classpathFileName, String driverClassName, String url, String username, String password) {
        Connection conn = null;
        try {
            Class.forName(driverClassName);
            conn = DriverManager.getConnection(url, username, password);
 
            ClassPathResource classPathResource = new ClassPathResource(classpathFileName);
            EncodedResource encodedResource = new EncodedResource(classPathResource, "utf-8");
            ScriptUtils.executeSqlScript(conn, encodedResource);
        } catch (Exception e) {
            log.error("执行sql错误!", e);
            throw new DaoException(DatabaseExceptionEnum.SQL_EXEC_ERROR, e.getMessage());
        } finally {
            IoUtil.close(conn);
        }
    }
 
    /**
     * 执行系统路径sql的文件
     *
     * @author fengshuonan
     * @date 2021/5/19 10:52
     */
    public static void runFileSystemSql(SqlSessionFactory sqlSessionFactory, String sqlPath) {
        Connection conn = null;
        try {
            SqlSession sqlSession = sqlSessionFactory.openSession();
            conn = sqlSession.getConnection();
 
            FileSystemResource classPathResource = new FileSystemResource(sqlPath);
            EncodedResource encodedResource = new EncodedResource(classPathResource, "GBK");
            ScriptUtils.executeSqlScript(conn, encodedResource);
        } catch (Exception e) {
            log.error("执行sql错误!", e);
            throw new DaoException(DatabaseExceptionEnum.SQL_EXEC_ERROR, e.getMessage());
        } finally {
            IoUtil.close(conn);
        }
    }
 
}