在我目前的spring / hibernate项目中,我刚刚将文件import.sql
添加到我的文件夹WEB-INF
,但是当部署到服务器时,没有数据导入到数据库中,即使是没有创建的表也没有问题
我的配置:
@Configuration
@EnableTransactionManagement
@ComponentScan
public class HibernateConfig {
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
sessionFactory.setPackagesToScan(new String[] { "org.kleber" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource restDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql:mydata");
dataSource.setUsername("kleber");
dataSource.setPassword("123");
return dataSource;
}
@Bean
public Properties hibernateProperties() {
return new Properties() {
private static final long serialVersionUID = 1L;
{
setProperty("hibernate.hbm2ddl.auto", "create-drop");
setProperty("hibernate.hbm2ddl.import_files", "/WEB-INF/import.sql");
setProperty("hibernate.show_sql", "true");
setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
}
};
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
@Bean
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory);
return txManager;
}
}
我的import.sql文件:
INSERT INTO credencial (nome) VALUES ('categoria');
INSERT INTO credencial (nome) VALUES ('produto');
INSERT INTO credencial (nome) VALUES ('pagina');
INSERT INTO credencial (nome) VALUES ('usuario');
INSERT INTO credencial (nome) VALUES ('admin');
INSERT INTO autorizacao (nome) VALUES ('insert_categoria');
INSERT INTO autorizacao (nome) VALUES ('update_categoria');
INSERT INTO autorizacao (nome) VALUES ('delete_categoria');
INSERT INTO autorizacao (nome) VALUES ('insert_produto');
INSERT INTO autorizacao (nome) VALUES ('update_produto');
INSERT INTO autorizacao (nome) VALUES ('delete_produto');
INSERT INTO autorizacao (nome) VALUES ('insert_pagina');
INSERT INTO autorizacao (nome) VALUES ('update_pagina');
INSERT INTO autorizacao (nome) VALUES ('delete_pagina');
INSERT INTO autorizacao (nome) VALUES ('insert_usuario');
INSERT INTO autorizacao (nome) VALUES ('update_usuario');
INSERT INTO autorizacao (nome) VALUES ('delete_usuario');
INSERT INTO credencial_autorizacao VALUES (1,1);
INSERT INTO credencial_autorizacao VALUES (1,2);
INSERT INTO credencial_autorizacao VALUES (1,3);
INSERT INTO credencial_autorizacao VALUES (2,4);
INSERT INTO credencial_autorizacao VALUES (2,5);
INSERT INTO credencial_autorizacao VALUES (2,6);
INSERT INTO credencial_autorizacao VALUES (3,7);
INSERT INTO credencial_autorizacao VALUES (3,8);
INSERT INTO credencial_autorizacao VALUES (3,9);
INSERT INTO credencial_autorizacao VALUES (4,10);
INSERT INTO credencial_autorizacao VALUES (4,11);
INSERT INTO credencial_autorizacao VALUES (4,12);
INSERT INTO usuario (login, senha, nome, sobrenome, email) VALUES ('klebermo', '202cb962ac59075b964b07152d234b70', 'kleber', 'mota', 'kleber@mail.com');
INSERT INTO usuario_credencial VALUES (1,1);
INSERT INTO usuario_credencial VALUES (1,2);
INSERT INTO usuario_credencial VALUES (1,3);
INSERT INTO usuario_credencial VALUES (1,4);
INSERT INTO usuario_credencial VALUES (1,5);
任何人都可以告诉我这里缺少什么?
答案 0 :(得分:5)
import.sql
应位于类路径的根目录中,因此请将其放在\WEB-INF\classes
内,然后将其拾取。