springboot数据初始化

我们常利用springboot数据初始化进行测试数据准备,或者项目启动的数据准备等,下面来具体了解下配置的含义。

springboot2数据初始化配置如下

spring.datasource.initialization-mode = always
spring.datasource.schema = classpath:xx/schema.sql
spring.datasource.data = classpath:xx/data.sql

从以下两方面来说明配置的含义

  • 数据源

显式指定数据源

spring.datasource.url = jdbc:mysql://127.0.0.1:3360/test
spring.datasource.username = xx
spring.datasource.password = xx

以上是springboot1用spring.datasource.url/username/password指定数据源;
springboot2将schema和data的数据源分开来,要分别指定schema和data的数据源属性;

	private List<String> schema;
	private String schemaUsername;
	private String schemaPassword;
	private List<String> data;
	private String dataUsername;
	private String dataPassword;

不指定数据源
不指定数据源的话,会默认读取项目中配置过的DataSource实例作为数据源;
注意如果只声明了url,但没有指定username和password,将会默认读取项目中配置过的DataSource实例作为数据源;

  • schema & data

schema.sql用于执行ddl语句,data.sql用于执行dml语句;
spring.datasource.schemaspring.datasource.data可以传多个sql文件;
在springboot1默认会执行data和schema文件,不需要显式设置,显式指定的默认设置为spring.datasource.initialized = true
在springboot2里,必须要设置spring.datasource.initialization-mode为aways,因为其默认值不会执行data和schema文件;
如果没有配置data和schema文件的话,会去找classpath路径下的data和schema开头的sql文件执行;

可以参考以下springboot2源码

class DataSourceInitializer {
    ...
	/**
	 * Create the schema if necessary.
	 * @return {@code true} if the schema was created
	 * @see DataSourceProperties#getSchema()
	 */
	public boolean createSchema() {
		List<Resource> scripts = getScripts("spring.datasource.schema", this.properties.getSchema(), "schema");
		if (!scripts.isEmpty()) {
			if (!isEnabled()) {
				logger.debug("Initialization disabled (not running DDL scripts)");
				return false;
			}
			String username = this.properties.getSchemaUsername();
			String password = this.properties.getSchemaPassword();
			runScripts(scripts, username, password);
		}
		return !scripts.isEmpty();
	}

	/**
	 * Initialize the schema if necessary.
	 * @see DataSourceProperties#getData()
	 */
	public void initSchema() {
		List<Resource> scripts = getScripts("spring.datasource.data", this.properties.getData(), "data");
		if (!scripts.isEmpty()) {
			if (!isEnabled()) {
				logger.debug("Initialization disabled (not running data scripts)");
				return;
			}
			String username = this.properties.getDataUsername();
			String password = this.properties.getDataPassword();
			runScripts(scripts, username, password);
		}
	}

	private boolean isEnabled() {
		DataSourceInitializationMode mode = this.properties.getInitializationMode();
		if (mode == DataSourceInitializationMode.NEVER) {
			return false;
		}
		if (mode == DataSourceInitializationMode.EMBEDDED && !isEmbedded()) {
			return false;
		}
		return true;
	}

	private boolean isEmbedded() {
		try {
			return EmbeddedDatabaseConnection.isEmbedded(this.dataSource);
		}
		catch (Exception ex) {
			logger.debug("Could not determine if datasource is embedded", ex);
			return false;
		}
	}

	private List<Resource> getScripts(String propertyName, List<String> resources, String fallback) {
		if (resources != null) {
			return getResources(propertyName, resources, true);
		}
		String platform = this.properties.getPlatform();
		List<String> fallbackResources = new ArrayList<>();
		fallbackResources.add("classpath*:" + fallback + "-" + platform + ".sql");
		fallbackResources.add("classpath*:" + fallback + ".sql");
		return getResources(propertyName, fallbackResources, false);
	}
    ...
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值