我正在使用Spring-Boot和内存中的H2数据库。我正在尝试使用DataSource Bean通过配置类来配置数据库。
我希望在应用程序启动时执行脚本,该脚本在数据库中插入一些行。
因此,我使用addScript()
的方法EmbeddedDatabaseBuilder
。但是我继续得到FileNotFoundExceptions。
该实体正确,并且创建了正确的表,我可以使用h2-console检查该表。此外,文件import.sql确实存在于正确的文件夹中。 Intellij确实找到了此文件。
import.sql
文件:
INSERT INTO USER (id,user_name,password,first_name,last_name,email) VALUES
(0,'test','12345','Test','Entry','example@mail.com'),
@Configuration
public class DataSourceConfig {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("db/sql/import.sql")
.build();
}
}
Caused by: java.io.FileNotFoundException: class path resource [db/sql/import.sql] cannot be opened because it does not exist
我希望在应用程序启动时正确执行sql语句。