refactor: init schema and data

This commit is contained in:
小锅盖 2021-11-11 08:17:38 +08:00
parent fd32a69566
commit e622152c20
5 changed files with 81 additions and 22 deletions

View File

@ -18,6 +18,7 @@ package com.baomidou.dynamic.datasource.creator;
import com.baomidou.dynamic.datasource.ds.ItemDataSource; import com.baomidou.dynamic.datasource.ds.ItemDataSource;
import com.baomidou.dynamic.datasource.enums.SeataMode; import com.baomidou.dynamic.datasource.enums.SeataMode;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty; import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DatasourceInitProperty;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties; import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import com.baomidou.dynamic.datasource.support.ScriptRunner; import com.baomidou.dynamic.datasource.support.ScriptRunner;
import com.p6spy.engine.spy.P6DataSource; import com.p6spy.engine.spy.P6DataSource;
@ -57,10 +58,11 @@ public abstract class AbstractDataSourceCreator {
} }
private void runScrip(DataSource dataSource, DataSourceProperty dataSourceProperty) { private void runScrip(DataSource dataSource, DataSourceProperty dataSourceProperty) {
String schema = dataSourceProperty.getSchema(); DatasourceInitProperty initProperty = dataSourceProperty.getInit();
String data = dataSourceProperty.getData(); String schema = initProperty.getSchema();
String data = initProperty.getData();
if (StringUtils.hasText(schema) || StringUtils.hasText(data)) { if (StringUtils.hasText(schema) || StringUtils.hasText(data)) {
ScriptRunner scriptRunner = new ScriptRunner(dataSourceProperty.isContinueOnError(), dataSourceProperty.getSeparator()); ScriptRunner scriptRunner = new ScriptRunner(initProperty.isContinueOnError(), initProperty.getSeparator());
if (StringUtils.hasText(schema)) { if (StringUtils.hasText(schema)) {
scriptRunner.runScript(dataSource, schema); scriptRunner.runScript(dataSource, schema);
} }

View File

@ -72,14 +72,6 @@ public class DataSourceProperty {
* jndi数据源名称(设置即表示启用) * jndi数据源名称(设置即表示启用)
*/ */
private String jndiName; private String jndiName;
/**
* 自动运行的建表脚本
*/
private String schema;
/**
* 自动运行的数据脚本
*/
private String data;
/** /**
* 是否启用seata * 是否启用seata
*/ */
@ -93,13 +85,10 @@ public class DataSourceProperty {
*/ */
private Boolean lazy; private Boolean lazy;
/** /**
* 错误是否继续 默认 true * 初始化
*/ */
private boolean continueOnError = true; @NestedConfigurationProperty
/** private DatasourceInitProperty init = new DatasourceInitProperty();
* 分隔符 默认 ;
*/
private String separator = ";";
/** /**
* Druid参数配置 * Druid参数配置
*/ */

View File

@ -0,0 +1,46 @@
/*
* Copyright © 2018 organization baomidou
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.baomidou.dynamic.datasource.spring.boot.autoconfigure;
import lombok.Data;
/**
* 动态数据源初始化脚本配置
*
* @author TaoYu
* @since 3.5.0
*/
@Data
public class DatasourceInitProperty {
/**
* 自动运行的建表脚本
*/
private String schema;
/**
* 自动运行的数据脚本
*/
private String data;
/**
* 错误是否继续 默认 true
*/
private boolean continueOnError = true;
/**
* 分隔符 默认 ;
*/
private String separator = ";";
}

View File

@ -72,6 +72,10 @@ public class DynamicDataSourceProperties {
* seata使用模式默认AT * seata使用模式默认AT
*/ */
private SeataMode seataMode = SeataMode.AT; private SeataMode seataMode = SeataMode.AT;
/**
* 全局默认publicKey
*/
private String publicKey = CryptoUtils.DEFAULT_PUBLIC_KEY_STRING;
/** /**
* 每一个数据源 * 每一个数据源
*/ */
@ -101,11 +105,6 @@ public class DynamicDataSourceProperties {
@NestedConfigurationProperty @NestedConfigurationProperty
private Dbcp2Config dbcp2 = new Dbcp2Config(); private Dbcp2Config dbcp2 = new Dbcp2Config();
/**
* 全局默认publicKey
*/
private String publicKey = CryptoUtils.DEFAULT_PUBLIC_KEY_STRING;
/** /**
* aop with default ds annotation * aop with default ds annotation
*/ */

View File

@ -0,0 +1,23 @@
/*
* Copyright © 2018 organization baomidou
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.baomidou.dynamic.datasource.spring.boot.autoconfigure.event;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
public interface DataSourcePropertyEvent {
void beforeCreate(DataSourceProperty dataSourceProperty);
}