如何在应用程序启动/加载期间执行SQL插入查询以填充数据库?

问题描述 投票:3回答:4

我想在加载应用程序期间将一些数据加载到mysql数据库中。我正在使用Hibernate来管理应用程序的数据库。我可以通过使用Bootstrap在groovy中完成它,但我想用Java实现它。我想提一下,它是基于Spring MVC的Web应用程序。

在互联网上搜索时,我发现但是使用名为import_file的hibernate属性,我可以实现它,但我正在寻找替代路线。

java sql spring hibernate
4个回答
12
投票

你也可以利用Spring的DataSourceInitializer。以下是Java Config的示例。

@Bean
public DataSourceInitializer dataSourceInitializer() {
    ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
    resourceDatabasePopulator.addScript(new ClassPathResource("/data.sql"));

        DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
        dataSourceInitializer.setDataSource(dataSource());
        dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);
        return dataSourceInitializer;
    }

3
投票

Spring已经提供了一种使用DatabasePopulator初始化带有内容的数据库的方法。这是我发现的一个quick example,用于Spring Batch示例应用程序。要在该代码中查看的类是ResourceDatabasePopulator。 Another example是Spring Social项目样本。


2
投票

我将在Spring上下文配置中注册ApplicationListener的实例,该实例侦听ContextRefreshedEvent,它在应用程序上下文完成初始化或刷新时发出信号。在此之后,您可以设置数据库填充。

您将在下面找到ApplicationListener实现(它依赖于负责执行数据库操作的DAO)和实现此目的所需的Spring配置(Java和XML)。您需要选择特定于您的应用的配置:

基于Java的配置

@Configuration
public class JavaConfig {

    @Bean
    public ApplicationListener<ContextRefreshedEvent> contextInitFinishListener() {
        return new ContextInitFinishListener(personRepository());
    }

    @Bean
    public PersonRepository personRepository() {
        return new PersonRepository();
    }
}

XML

    <bean class="com.package.ContextInitFinishListener">
        <constructor-arg>
            <bean class="com.package.PersonRepository"/>
        </constructor-arg>
    </bean>

这是ContextInitFinishListener类的代码:

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

public class ContextInitFinishListener implements ApplicationListener<ContextRefreshedEvent> {

    private PersonRepository personRepository;

    public ContextInitFinishListener(PersonRepository personRepository) {
        this.personRepository = personRepository;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        //populate database with required values, using PersonRepository
    }
}

注意:为了示例的目的,PersonRepository只是一个通用的DAO,它意味着代表您在应用中使用的DAO


0
投票

我在Spring启动控制台应用程序测试中使用如下。

ResourceDatabasePopulator rdp = new ResourceDatabasePopulator();
 rdp.addScript(new ClassPathResource("sql/create-tables.sql"));
 rdp.execute(dataSource);

根据应用程序类型或数据分层框架,有不同的方法来获取数据源。

如果你使用spring boot autoconfigure h2 datasource你可以使用。

@Autowired
Datasource datasource;

获取数据源throgh外部配置类如下

@Value("${spring.datasource.driver-class-name}")
private String driverClass;

@Value("${spring.datasource.url}")
private String dbUrl;

@Value("${spring.datasource.username}")
private String dbUserName;

@Value("${spring.datasource.password}")
private String dbPassword;


@Bean
public DataSource dataSource(){
    SingleConnectionDataSource dataSource = new 
    SingleConnectionDataSource();
    dataSource.setDriverClassName(driverClass);
    dataSource.setUrl(dbUrl);
    dataSource.setUsername(dbUserName);
    dataSource.setPassword(dbPassword);
    dataSource.setSuppressClose(true);
    dataSource.setAutoCommit(true);
    return dataSource;
}

这对我有用,保留你需要在create-tables.sql中执行的查询

© www.soinside.com 2019 - 2024. All rights reserved.