zoukankan      html  css  js  c++  java
  • spring使用mybatis执行SQL脚本,创建和初始化数据库

    package com.sy.ai.context;
    
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.InputStreamReader;
    import java.io.UnsupportedEncodingException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import lombok.extern.log4j.Log4j2;
    import org.apache.ibatis.jdbc.ScriptRunner;
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.stereotype.Component;
    
    
    @Component
    @Log4j2
    public class ApplicationContextHelper implements ApplicationContextAware {
    
    //    private static ApplicationContext context;
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
            try {
                javax.sql.DataSource dataSource =
                    (javax.sql.DataSource) applicationContext.getBean("dataSource");
                java.sql.Connection connection = dataSource.getConnection();
                Statement st = connection.createStatement();
                String sql = "select count(*) from pg_tables where schemaname = 'public';";
                ResultSet rs = st.executeQuery(sql);
                if (rs.next()) {
                    if (rs.getInt(1) == 0) {
                        log.info("database not inited; then init database");
    
                        ScriptRunner runner = new ScriptRunner(connection);
                        runner.setStopOnError(true);
                        log.info(System.getProperty("user.dir"));
                        try {
                            runner.runScript(new InputStreamReader(
                                this.getClass().getResourceAsStream("/sql/public.sql"),
                                "UTF-8"));
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        connection.commit();
                        connection.close();
                        log.info("execute sql file success");
                    } else {
                        log.info("table ip_camera exists");
                    }
                }
                connection.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
                // System.exit(0);
            }
        }
    }
  • 相关阅读:
    centos7.6 安装与配置 MongoDB yum方式
    MongoDB 介绍
    centos 关闭selinux
    前端 HTML标签属性
    前端 HTML 标签嵌套规则
    前端 HTML 标签分类
    前端 HTML body标签相关内容 常用标签 表单标签 form里面的 input标签介绍
    前端 HTML body标签相关内容 常用标签 表单标签 form 表单控件分类
    前端 HTML form表单标签 select标签 option 下拉框
    POJ 1426
  • 原文地址:https://www.cnblogs.com/ronaldHU/p/15166313.html
Copyright © 2011-2022 走看看