使用Mybatis执行sql脚本

摘要:
pom。xml˂?xmlversion=“1.0”encoding=“UTF-8”?

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

importorg.apache.ibatis.jdbc.ScriptRunner;
importjava.io.File;
importjava.io.FileReader;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.SQLException;
public classExec {
    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) throwsSQLException, ClassNotFoundException {
        mybatisExec();
    }
    private static void mybatisExec() throwsClassNotFoundException, SQLException {
        Class.forName(driver);
        Connection conn =DriverManager.getConnection(url, username, password);
        ScriptRunner runner = newScriptRunner(conn);
        try{
            runner.setStopOnError(true);
            runner.runScript(newFileReader(file));
        } catch(Exception e) {
            e.printStackTrace();
        }
        conn.close();
    }
}

免责声明:文章转载自《使用Mybatis执行sql脚本》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Oracle 修改序列步长CentOS 6或7 启动故障修复及root密码破解下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

linux环境下java调用C/C++动态库(JNI技术:参数为指针与结构体)

一、JNI技术  JNI是Java Native Interface的缩写,通过使用 Java本地接口书写程序,可以确保代码在不同的平台上方便移植.  SUN公司发布的Java 本地接口(JNI)提供了将Java与C/C++、汇编等本地代码集成的方案,该规范使得在 Java 虚拟机内运行的 Java 代码能够与其它编程语言互相操作,包括创建本地方法、更新J...

拉仇恨!webhook + 企业微信给同事做了个代码提交监听工具

本文案例收录在 https://github.com/chengxy-nds/Springboot-Notebook 大家好,我是小富~ 最近接个任务,用webhook做了个代码提交监听功能,就是有人向远程仓库提交代码后,会在企业微信群内发送一条消息,类似 @XXX 在XXX时间,向XXX项目提交 XXXX 代码 这样的文案。 至于为啥要做这么个工具,没办...

Python——PYQT:控件基本使用

QtGui.QComboBox控件常用函数: .addItem(string) #添加字符串项到Item .addItems(list) #添加列表或元组元素到Item .clear() #清除所有Item .clearEditText() #清除编辑框内容 .count() #返回Item数目 .currentIndex...

request请求地址

1、String contextPath = httpServletRequest.getServletContext().getContextPath(); /项目名称 2、String contextPath2 = httpServletRequest.getContextPath(); /项目名称 3、String requestURI = http...

[go]gin框架

gin中文文档gin example repo gin参考 gin框架基础使用-梳理版 - 最简单的gin启服务 package main import "github.com/gin-gonic/gin" func main() { r := gin.Default() r.Run() //端口默认8080 } RESPONSE RENDER...

记录Java8中的字符串转数组再通过指定符号拼接

1.字符串转换字符串数组 String[] nameArr = StringUtils.tokenizeToStringArray(nameAttr, MULTI_VALUE_ATTRIBUTE_DELIMITERS); // nameAttr="made,in;china,;qqq,.aaa" ,MULTI_VALUE_ATTRIBUTE_DELIMI...