我使用Spring Boot,Hibernate,Spring Security工具創建了一個小型Web應用程序,現在我想在瀏覽器中測試它。我猜想一切都很好,但有一個我自己無法解決的小問題。我希望我的DataBase在與populatorDB.sql斷開/連接後自動填充它,以不再次創建/刪除我的數據。這意味着,當我在數據庫中添加/刪除新數據時,在瀏覽器中進行測試時,我將它們放在數據庫中,而不是斷開/連接,並且在數據庫中沒有新數據,只有使用populatorDB.sql的我的標準集。如何使用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;
}
}
在此先感謝。
但我在Spring Boot應用程序中沒有'profile(「populatedb」)''。我想在連接之前每次在populatorDB.sql的幫助下自動重啓數據庫。 –
通過連接/斷開你的意思是當用戶登錄和註銷? –
不,我的意思是在Tomcat中運行應用程序。 –