background
- Before the project goes online, a batch of test data needs to be provided to the database. The data requirements are: after each modification of the defect and restart of the project, the test data will be initialized to the original data
main idea
- In the SpringBoot architecture, the DataSourceInitializer class can initialize data after the project is started, and we can initialize the data by automatically executing a custom sql script. By customizing the DataSourceInitializer Bean, you can implement specific scripts according to business requirements.
use
- Prerequisite: The project data source configuration is completed
- method
- Customize the DataSourceInitializer Bean through the three annotations @Configuration, @Bean and @Value, and now the business logic of automating the execution of the script is implemented in the definition of the Bean.
- code show as below
package com.test.integration.rpc.initdata; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.Resource; import org.springframework.jdbc.datasource.init.DataSourceInitializer; import org.springframework.jdbc.datasource.init.DatabasePopulator; import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; import javax.sql.DataSource; /** * @author test * @Description: 自定义初始化数据源 * @date 2020/4/11 */ @Configuration public class CustomizeDataSourceInitializer { @Value("classpath:testSql/test_farms.sql") private Resource functionScriptFarms; @Value("classpath:testSql/test_miners.sql") private Resource functionScriptMiners; @Value("classpath:testSql/test_pool_config.sql") private Resource functionScriptPoolConfig; @Bean public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) { final DataSourceInitializer initializer = new DataSourceInitializer(); // 设置数据源 initializer.setDataSource(dataSource); initializer.setDatabasePopulator(databasePopulator()); return initializer; } private DatabasePopulator databasePopulator() { final ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); populator.addScripts(functionScriptFarms); populator.addScripts(functionScriptMiners); populator.addScripts(functionScriptPoolConfig); return populator; } }
-
- The SQL script is as follows: example test_pool_config.sql
delete from `pool_configs`; INSERT INTO `pool_configs`(`pool_config_id`, `cdate`, `mdate`, `pool_config_mid` ) VALUES (1, '2020-04-10 18:02:54', '2020-04-10 18:02:54', 'bcf2a18a-b38b-4e3a-8852-7efbabc7954d'); INSERT INTO `pool_configs`(`pool_config_id`, `cdate`, `mdate`, `pool_config_mid` ) VALUES (2, '2020-04-10 18:02:54', '2020-04-10 18:02:54', 'bcf3a18a-b38b-4e3a-8852-7efbabc7954d'); INSERT INTO `pool_configs`(`pool_config_id`, `cdate`, `mdate`, `pool_config_mid` ) VALUES (3, '2020-04-10 18:02:54', '2020-04-10 18:02:54', 'bcf4a18a-b38b-4e3a-8852-7efbabc7954d'); INSERT INTO `pool_configs`(`pool_config_id`, `cdate`, `mdate`, `pool_config_mid` ) VALUES (4, '2020-04-10 18:02:54', '2020-04-10 18:02:54', 'bcf5a18a-b38b-4e3a-8852-7efbabc7954d ' );