如何使用Spring Boot / MySQL / Hibernate重启数据库

时间:2016-11-05 10:08:30

标签: mysql spring hibernate spring-boot

我使用Spring Boot,Hibernate,Spring Security工具创建了一个小型Web应用程序,现在我想在浏览器中测试它。我觉得一切都很好,但有一个小问题,我不能自己解决。我希望我的DataBase在断开/连接populatorDB.sql后自动填充它,以便不再创建/删除我的数据。这意味着,当我在浏览器中测试时将新数据添加/删除到数据库中时,我将它们放在数据库中,而不是断开/连接,而且我在数据库中没有新数据,只有我的标准设置为populatorDB.sql。

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','bill@gmail.com', 1),
  ('Petro','Petrov','Petrovych','+380(66)9876543','+380(44)1122334','USA','mark@gmail.com', 1),
  ('Sydor','Sydorov','Sydorovych','+380(99)1234567','','USA','barak@gmail.com', 1),
  ('Mykola','Mykolaiov','Mykolaiovych','+380(99)9876543','','USA','michel@gmail.com', 1),
  ('Aleksandr','Aleksandrov','Aleksandrovych','+380(50)5557799','+380(44)0000009','UK','david@gmail.com', 2),
  ('Vasyl','Vasyliov','Vasyliovych','+380(00)1100999','','USA','steve@gmail.com', 2),
  ('Viktor','Viktorov','Viktorovych','+380(00)2244888','','USA','tim@gmail.com', 2),
  ('Kostia','Konstantynov','Konstantynovych','+380(69)8881188','+380(44)1111119','USA','jim@gmail.com', 3),
  ('Anton','Antonov','Antonovych','+380(67)9000001','','UK','david@gmail.com', 3);

数据库配置:

@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 个答案:

答案 0 :(得分:2)

您可以将schema.sql和data.sql放在src / main / resources中,并使用默认的JDBC Initializer。

更多详情请见:http://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html,第75.3节

答案 1 :(得分:1)

删除“update”属性并将其替换为

properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");

hibernate.ddl-auto属性将创建表(基于您使用@Entity注释的声明实体),当应用程序完成时,它将删除它。