2016-11-05 8 views
0

私はSpring Boot、Hibernate、Spring Securityツールで小さなWebアプリケーションを作成しましたが、今はブラウザでテストしたいと思います。すべてがうまくいくと思いますが、自分で解決できない小さな問題があります。私は私のDataBaseに自動的にデータを作成/削除しないpopulatorDB.sqlとの切断/接続後に自動的にデータを取り込みたい。つまり、DBに新しいデータを追加/削除するときにブラウザでテストしているときに、DBに接続して接続するよりも、データベースにpopulateDB.sqlで設定したスタンドアロンのデータだけがDBにはありません。Spring Boot/MySQL/Hibernatでデータベースを再起動する方法

PopulatorDB.sql

DELETE FROM lardi.user_roles; 
DELETE FROM lardi.contacts; 
DELETE FROM lardi.users; 

INSERT INTO lardi.users 
(login,password,full_name) VALUES 
    ('Bill', '112233', 'user'), 
    ('John', '112233', 'user'), 
    ('Mark', '112233', 'user'); 

INSERT INTO lardi.user_roles 
(role,user_id) VALUES 
    ('ROLE_USER',1), 
    ('ROLE_USER',2), 
    ('ROLE_USER',3); 

INSERT INTO lardi.contacts 
(first_name, last_name, patronymic, mobile_phone_number, home_phone_number, address, email, user_id) VALUES 
    ('Ivan','Ivanov','Ivanovych','+380(66)1234567','','USA','[email protected]', 1), 
    ('Petro','Petrov','Petrovych','+380(66)9876543','+380(44)1122334','USA','[email protected]', 1), 
    ('Sydor','Sydorov','Sydorovych','+380(99)1234567','','USA','[email protected]', 1), 
    ('Mykola','Mykolaiov','Mykolaiovych','+380(99)9876543','','USA','[email protected]', 1), 
    ('Aleksandr','Aleksandrov','Aleksandrovych','+380(50)5557799','+380(44)0000009','UK','[email protected]', 2), 
    ('Vasyl','Vasyliov','Vasyliovych','+380(00)1100999','','USA','[email protected]', 2), 
    ('Viktor','Viktorov','Viktorovych','+380(00)2244888','','USA','[email protected]', 2), 
    ('Kostia','Konstantynov','Konstantynovych','+380(69)8881188','+380(44)1111119','USA','[email protected]', 3), 
    ('Anton','Antonov','Antonovych','+380(67)9000001','','UK','[email protected]', 3); 

DBの設定:事前に

@Configuration 
@EnableTransactionManagement 
public class JPAConfig { 

    @Bean 
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() { 
     LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); 
     em.setDataSource(dataSource()); 
     em.setPackagesToScan("com.model"); 

     JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); 
     em.setJpaVendorAdapter(vendorAdapter); 
     em.setJpaProperties(additionalProperties()); 

     return em; 
    } 

    private Properties additionalProperties() { 
     Properties properties = new Properties(); 
     properties.setProperty("hibernate.hbm2ddl.auto", "update"); 
     properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect"); 
     return properties; 
    } 

    @Bean 
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf){ 
     JpaTransactionManager transactionManager = new JpaTransactionManager(); 
     transactionManager.setEntityManagerFactory(emf); 

     return transactionManager; 
    } 

    @Bean 
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){ 
     return new PersistenceExceptionTranslationPostProcessor(); 
    } 

    @Bean 
    public DataSource dataSource(){ 
     DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
     dataSource.setDriverClassName("com.mysql.jdbc.Driver"); 
     dataSource.setUrl("jdbc:mysql://localhost:3306/lardi?useSSL=false"); 
     dataSource.setUsername("root"); 
     dataSource.setPassword("2940063"); 
     return dataSource; 
    } 

    @Bean(name = "messageSource") 
    public ReloadableResourceBundleMessageSource messageSource(){ 
     ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); 
     messageSource.setBasename("classpath:validation"); 
     messageSource.setDefaultEncoding("UTF-8"); 
     return messageSource; 
    } 

    @Bean 
    public DataSourceInitializer dataSourceInitializer(DataSource dataSource){ 
     DataSourceInitializer dataSourceInitializer = new DataSourceInitializer(); 
     dataSourceInitializer.setDataSource(dataSource); 
     ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(); 
     databasePopulator.addScript(new ClassPathResource("populatorDB.sql")); 
     dataSourceInitializer.setDatabasePopulator(databasePopulator); 
     dataSourceInitializer.setEnabled(true); 
     return dataSourceInitializer; 
    } 

    @Bean 
    public static PropertyPlaceholderConfigurer placeHolderConfigurer(){ 
     final PropertyPlaceholderConfigurer props = new PropertyPlaceholderConfigurer(); 
     props.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE); 
     return props; 
    } 
} 

感謝。

答えて

2

src/main/resources内にschema.sqlとdata.sqlを配置し、デフォルトのJDBC Initializerを使用できます。中

詳細:http://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html、セクション75.3

+0

私はSpringブートアプリケーションで 'profile(" populatedb ")'を持っていません。私は接続の前に毎回populatorDB.sqlの助けを借りてDBを自動的に再起動したい。 –

+0

接続/切断により、ユーザーがログインしたときとログアウトしたときのことを意味しますか? –

+0

いいえ、Tomcatでアプリケーションを実行中です。 –

1

は、「更新」プロパティを削除し、

properties.setProperty("hibernate.hbm2ddl.auto", "create-drop"); 
hibernate.ddl-autoプロパティは、テーブルを作成します

と交換してください(あなたの宣言エンティティに基づきます@Entityと注釈されています)、アプリが終了すると、アプリはそれをドロップします。

関連する問題