Java 带有 H2 文件数据库的 Spring Boot 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24603721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot application with H2 file database
提问by Jerry
I'm trying to have a H2 database setup on spring boot application startup. I have configured the database in application.properties:
我正在尝试在 Spring Boot 应用程序启动时设置 H2 数据库。我已经在 application.properties 中配置了数据库:
spring.datasource.url = jdbc:h2:file:~/testdb
spring.datasource.username = sa
spring.datasource.password = sa
spring.datasource.driverClassName = org.h2.Driver
The Application.java file:
Application.java 文件:
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
CreateH2Database createH2Database = new CreateH2Database();
createH2Database.create();
}
}
CreateH2Database.java:
创建H2Database.java:
public class CreateH2Database {
private Logger log = Logger.getLogger(CreateH2Database.class);
@Autowired
protected JdbcTemplate jdbcTemplate;
public void create() {
log.info("Creating H2 Database");
createUsers();
}
private void createUsers() {
log.info("Creating users table");
jdbcTemplate.execute("create table if not exists users (id serial, first_name varchar(255), last_name varchar(255))");
String[] names = "John Woo;Jeff Dean;Josh Bloch;Josh Long".split(";");
for (String fullname : names) {
String[] name = fullname.split(" ");
log.info("Inserting user record for " + name[0] + " " + name[1] + "\n");
jdbcTemplate.update(
"INSERT INTO users(first_name,last_name) values(?,?)",
name[0], name[1]);
}
}
}
Once the application is started it should create the Users table if it doesn't already exist, and insert the users into the table. If the table does already exist, I don't want it to be modified.
一旦应用程序启动,它应该创建 Users 表(如果它不存在),并将用户插入到表中。如果表已经存在,我不希望它被修改。
- I get a
NullPointerException
onjdbcTemplate.execute
. How can I get thejdbcTemplate
injected? All the example I've seen require the datasource to be created manually, and then the JdbcTemplate is created. However, the datasource in this example seems to be created based on the application.properties values. - Is this the correct approach to setup the database (i.e. calling a
CreateH2Database
after starting the SpringApplication)? Will this approach work if I want to run the application as a WAR on another application server?
- 我得到一个
NullPointerException
上jdbcTemplate.execute
。我怎样才能得到jdbcTemplate
注射?我看到的所有示例都需要手动创建数据源,然后创建 JdbcTemplate。但是,本示例中的数据源似乎是基于 application.properties 值创建的。 - 这是设置数据库的正确方法(即
CreateH2Database
在启动 SpringApplication 后调用 a )吗?如果我想在另一个应用程序服务器上将应用程序作为 WAR 运行,这种方法是否有效?
采纳答案by geoand
Since you are using Spring Boot, you should take advantage of it's database initialization features. There is no need to roll out your own implementation.
由于您使用的是 Spring Boot,您应该利用它的数据库初始化功能。无需推出您自己的实现。
All you have to do is have the files schema.sql
and data.sql
on the root of the classpath (most likely under /resources
). Spring Boot will auto-detect these and run the first one in order to create the database and the second one to populate it.
所有您需要做的是有文件schema.sql
,并data.sql
在类路径中(最有可能的下根/resources
)。Spring Boot 将自动检测这些并运行第一个以创建数据库和第二个以填充它。
Check out thispart of the Spring Boot documentation
查看Spring Boot 文档的这一部分
If you need to perform the initialization conditionally (perhaps only when running integration tests), you can advantage of Spring profiles. What you would do in that case is have the properties file for the test profile contain
如果您需要有条件地执行初始化(可能仅在运行集成测试时),您可以利用 Spring配置文件。在这种情况下,您要做的是让测试配置文件的属性文件包含
spring.datasource.initialize=true
spring.datasource.initialize=true
while the properties file for the other profiles would contain
而其他配置文件的属性文件将包含
spring.datasource.initialize=false
spring.datasource.initialize=false