SpringBoot启动并初始化执行sql脚本

41 篇文章 0 订阅
16 篇文章 0 订阅

提示这是我的个人IT资源网站,所有资源都免费,注册登录后就可以看到密码,需要什么大家尽情选取!

如果我们想在项目启动的时候去执行一些sql脚本该怎么办呢,SpringBoot给我们提供了这个功能,可以在启动SpringBoot的项目时,执行脚本,下面我们来看一下。

我们先看一下源码
在这里插入图片描述

boolean createSchema() {
	//会从application.properties或application.yml中获取sql脚本列表
	List<Resource> scripts = this.getScripts("spring.datasource.schema", this.properties.getSchema(), "schema");
    if (!scripts.isEmpty()) {
    	if (!this.isEnabled()) {
        	logger.debug("Initialization disabled (not running DDL scripts)");
            return false;
        }

        String username = this.properties.getSchemaUsername();
        String password = this.properties.getSchemaPassword();
        //运行sql脚本
        this.runScripts(scripts, username, password);
    }
    return !scripts.isEmpty();
}
private List<Resource> getScripts(String propertyName, List<String> resources, String fallback) {
	if (resources != null) {
		//如果配置文件中配置,则加载配置文件
    	return this.getResources(propertyName, resources, true);
   	} else {
   		//指定schema要使用的Platform(mysql、oracle),默认为all
    	String platform = this.properties.getPlatform();
        List<String> fallbackResources = new ArrayList();
        //如果配置文件中没配置,则会去类路径下找名称为schema或schema-platform的文件
        fallbackResources.add("classpath*:" + fallback + "-" + platform + ".sql");
       	fallbackResources.add("classpath*:" + fallback + ".sql");
       	return this.getResources(propertyName, fallbackResources, false);
    }
}
private List<Resource> getResources(String propertyName, List<String> locations, boolean validate) {
	List<Resource> resources = new ArrayList();
	Iterator var5 = locations.iterator();

    while(var5.hasNext()) {
    	String location = (String)var5.next();
        Resource[] var7 = this.doGetResources(location);
        int var8 = var7.length;

        for(int var9 = 0; var9 < var8; ++var9) {
        	Resource resource = var7[var9];
        	//验证文件是否存在
        	if (resource.exists()) {
            	resources.add(resource);
           	} else if (validate) {
            	throw new InvalidConfigurationPropertyValueException(propertyName, resource, "The specified resource does not exist.");
            }
        }
    }
    return resources;
}

从源码观察,大致知道是什么意思了,SpringBoot默认会从类路径下去找脚本文件,但是类路径下只能放规定名称为schema或schema-platform的脚本文件,如果我们想要分好多个脚本文件,那么这种方式就不合适了,那么就需要我们在application.properties或application.yml中去配置脚本列表,那么这个初始化脚本操作能不能在配置文件中控制呢,可以的,有一个initialization-mode属性,可以设置三个值,always为始终执行初始化,embedded只初始化内存数据库(默认值),如h2等,never为不执行初始化。

spring:
  datasource:
    username: root
    password: liuzhenyu199577
    url: jdbc:mysql://localhost:3306/jdbc
    driver-class-name: com.mysql.cj.jdbc.Driver
    initialization-mode: always 

下面我们验证一下这两种方式,

  1. 默认放置schema或schema-platform的脚本文件
    在这里插入图片描述
CREATE TABLE IF NOT EXISTS department (ID VARCHAR(40) NOT NULL, NAME VARCHAR(100), PRIMARY KEY (ID));

查看数据库没有department这个表,接下来我们启动程序
在这里插入图片描述
启动之后,我们再看一下,就执行了
在这里插入图片描述

  1. 配置文件中指定多个sql脚本
spring:
  datasource:
    username: root
    password: liuzhenyu199577
    url: jdbc:mysql://localhost:3306/jdbc
    driver-class-name: com.mysql.cj.jdbc.Driver
    initialization-mode: always
    schema:
      - classpath:department.sql
      - classpath:department2.sql
      - classpath:department3.sql

在这里插入图片描述
三个sql脚本都是插入语句

INSERT INTO department (ID,NAME) VALUES ('1','2')
INSERT INTO department (ID,NAME) VALUES ('2','3')
INSERT INTO department (ID,NAME) VALUES ('3','4')

现在表中无任何数据,接下来我们启动程序
在这里插入图片描述
启动之后,我们再看一下,表中就有了三条数据
在这里插入图片描述
以上就是SpringBoot启动并初始化sql脚本的步骤

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值