zoukankan      html  css  js  c++  java
  • 使用mybatis 自动执行脚本

    使用mybatis 自动执行脚本

    执行步骤

    • 添加包

      <dependency>
         <groupId>org.mybatis.spring.boot</groupId>
         <artifactId>mybatis-spring-boot-starter</artifactId>
         <version>2.2.0</version>
      </dependency>
      <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <scope>runtime</scope>
      </dependency>
      
    • 添加 sql执行脚本

    ​ 脚本建议放在resource目录下面,方便查找和替换

    • 添加脚本初始化类

      @Slf4j
      @Component
      public class InitLockDataBase implements InitializingBean {
      
          @Autowired
          private DataSource dataSource;
      
          private static final String SQL_FILE_NAME = "init.sql";
      
          @Override
          public void afterPropertiesSet(){
              try {
                  var scriptRunner = new ScriptRunner(dataSource.getConnection());
                  var classPathResource = new ClassPathResource(SQL_FILE_NAME);
                  var reader = new InputStreamReader(classPathResource.getInputStream());
                  scriptRunner.runScript(reader);
              } catch (SQLException | IOException e) {
                 log.info("[初始化sql脚本异常] 执行sql脚本异常,请检查",e);
                   throw new IllegalStateException("sql脚本执行异常");
              }
          }
      }
      
    • 启动项目

    注意事项

    1. 注意连接池的配置,spring默认版本是hikariCP的连接池,对应的application.yml中属性也要做修改
    2. 注意sql脚本中sql后的; ,如果未检测到,则认为sql一直未执行完,将会报错.
    3. 本项目使用了jdk15,如果启动不成功请切换对应的jdk版本.

    项目地址

    https://github.com/fulln/locks/releases/tag/scriptRunner

  • 相关阅读:
    带CheckBox的dojo Tree简单实现,并实现级联选取
    dojox.grid.EnhancedGrid
    Java内存模型及GC原理
    团队任务(第三次)
    团队任务二
    团队任务(一)
    词频统计及其效能分析
    贪吃蛇
    第一课
    软工七组团队2-1作业
  • 原文地址:https://www.cnblogs.com/wzqshb/p/14839588.html
Copyright © 2011-2022 走看看