一: package com.lianzhu.bigdata.config; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; import javax.sql.DataSource; @Slf4j @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class }) @Configuration public class CommandLineStartupRunner implements CommandLineRunner { @Bean("pGdata") @ConfigurationProperties(prefix="spring.datasource.gangbang") public DataSource pGdata() { return DataSourceBuilder.create() .build(); } // @Autowired // DataSource pGdata; @Override public void run(String... args) throws Exception { log.info("执行sql建表语句"); Resource resources = new ClassPathResource("operation.sql"); ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator(); resourceDatabasePopulator.addScripts(resources); resourceDatabasePopulator.execute(pGdata() ); log.info("建表语句执行完成"); } }
二: package com.lianzhu.bigdata.config; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; 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; /** * 自定义启动创建sql DataSourceInitializer */ @Configuration @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class }) public class BatchTaskDataSourceInitializer { /** * 构建Resource对象 */ @Value("classpath:operation.sql") private Resource businessScript; /** * 自定义Bean实现业务的特殊需求 * @param dataSource * @return */ @Bean public DataSourceInitializer dataSourceInitializer(@Qualifier("pGdata") DataSource dataSource) { final DataSourceInitializer initializer = new DataSourceInitializer(); // 设置数据源 initializer.setDataSource(dataSource); initializer.setDatabasePopulator(databasePopulator()); return initializer; } @Bean("pGdata") @ConfigurationProperties(prefix="spring.datasource.gangbang") public DataSource pGdata() { return DataSourceBuilder.create() .build(); } private DatabasePopulator databasePopulator() { final ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); populator.addScripts(businessScript); return populator; } }
springboot 初始Bean DataSourceInitializer 会执行sql语句
标签:脚本,jdbc,springboot,sql,boot,springframework,org,import From: https://www.cnblogs.com/wangbiaohistory/p/17338736.html