DataSourceInitializer在Spring Boot 1.2上不起作用 [英] DataSourceInitializer is not working on Spring boot 1.2

查看:507
本文介绍了DataSourceInitializer在Spring Boot 1.2上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring Boot的新手.我想在数据库创建种子数据时添加一些sql.

I am new to Spring boot.I want to add some sql while database is creating like seed data.

@Value("classpath:com/foo/sql/db-test-data.sql")
private Resource dataScript;

@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.addScript(dataScript);
    return populator;
}

props.put("hibernate.query.substitutions", "true 1, false 0");
        props.put("hibernate.hbm2ddl.auto", "create-drop");
        props.put("hibernate.show_sql", "false");
        props.put("hibernate.format_sql", "true");

我已经执行了此操作.但是它在弹簧靴上不起作用.任何人都可以帮助我.

I have perform this action.But it not working on spring boot.Can any one help me.

推荐答案

您可以利用Spring Boot数据库的初始化功能.最简单的方法是在类路径的根目录中放置一个"data.sql"文件.因此,您只需要:

You can take advantage of Spring Boot database initialization capabilities. The simplest way is to place a "data.sql" file in the root of the classpath. So you just need to:

  • 将您的sql文件名更改为"data.sql".
  • 将其放在"src/main/resources"中.

Spring Boot将在启动时自动选择文件并使用它来初始化数据库.

Spring Boot will automatically pick up the file and use it to initialize the database on startup.

您可以查看文档,如果您需要自定义文件名,位置等.

You can check the documentation if you need to customize the file name, location, etc.

这篇关于DataSourceInitializer在Spring Boot 1.2上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆