The application comes bundled with an in-memory H2 database. You can change which database is in use by adding the correct dependencies for the your database and modifying the
application.properties file (when using Spring) or configuring the server to use another database (when using JavaEE). See the sections below for more detailed information.
The corresponding connector for you database needs to be available to be able to use your database of choice. For example if you use MySQL, you need to add the approriate dependency to your
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <!-- The version element is only needed for the JavaEE version --> <version>6.0.4</version> </dependency>
The database is configured in
src/main/resources/application.properties. You can configure the database with the following properties.
spring.jpa.hibernate.ddl-auto=create spring.datasource.url=jdbc:mysql://localhost/dbtest spring.datasource.username=someuser spring.datasource.password=somepassword
Here the URL to the database is given using
spring.datasource.url. Spring Boot can most often deduce which database you have in use from the url and you don’t have to explicitely specify it. If for some reason you need to have control of them, you can also add
In JavaEE applications, the database is configured in the server and made available through JNDI to the application. In
src/main/resources/META-INF/persistence.xml the database connection is configured using
<jta-data-source>BakeryDS</jta-data-source>. The server must make a data source named
BakeryDS available for this to work.
There are configuration files in the project to setup the test database in
resources.xml for TomEE,
wildfly-h2-ds.xml for Wildfly and
src/test/resources/server.xml for Liberty. These are included only to make it easy to get started and are not meant for production. For production, you should specify the datasource in the server itself.
If you want to change the database defined in the project itself, you can update the configuration file for the server you are using. All the configuration files specify more or less the same parameters: JDBC url, driver, user and password. These need to be updated to match your database.
application.properties (Spring) or
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create" /> in
persistence.xml (JavaEE) defines what should be done to the database when the application starts. The valid options are none, update, create and create-drop.
nonedoes not modify the database in any way on application start-up.
updatetries to modify an existing schema to match the one defined by the application without deleting old data.
createcreates the database if one doesn’t exist from before, but doesn’t modify an existing database in any way.
create-dropalways deletes any existing database and creates it again from scratch.
In Spring, the default option depends on the choice of database. In-memory databases by default do
create-drop while other databases use
You can read more on how to configure the database in Spring’s documentation Working with SQL databases