• 使用Mybatis执行sql脚本


    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>geostack</groupId>
        <artifactId>sql-exec</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.16</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.4.6</version>
            </dependency>
        </dependencies>
    
    </project>

    Exec.java

    import org.apache.ibatis.jdbc.ScriptRunner;
    
    import java.io.File;
    import java.io.FileReader;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    public class Exec {
    
        private static final String driver = "com.mysql.jdbc.Driver";
        private static final String url = "jdbc:mysql://localhost:3306/userinfo";
        private static final String username = "gfstack";
        private static final String password = "gfstack";
        private static final File file = new File("C:\IDEA_WS\OperationCenter_develop_WS\OperationCenter\resource\config\sql\userinfo.sql");
    
    
        public static void main(String[] args) throws SQLException, ClassNotFoundException {
            mybatisExec();
        }
    
        private static void mybatisExec() throws ClassNotFoundException, SQLException {
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url, username, password);
            ScriptRunner runner = new ScriptRunner(conn);
            try {
                runner.setStopOnError(true);
                runner.runScript(new FileReader(file));
            } catch (Exception e) {
                e.printStackTrace();
            }
            conn.close();
        }
    
    }
  • 相关阅读:
    USB无线网卡导致耳机电流声很大
    知乎广告太多怎么办?
    网络机顶盒固件提取、编辑和打包
    GOOGLE机器学习速成班
    windows xp版本的chrome浏览器去哪里下载呢?
    [转载]HTTP无状态是什么意思?
    [转载]HTTP的无状态是什么意思?
    安卓手机网页打开淘宝总是弹出应用市场
    PLSQL Developer 客户端工具的安装
    VMware虚拟网卡设置问题
  • 原文地址:https://www.cnblogs.com/nihaorz/p/11169987.html
Copyright © 2020-2023  润新知