Bakery is built on top of Spring Boot, which comes bundled with an in-memory H2 database. You can change which database is used by adding a dependency to your database connector into
pom.xml and modifying the
In order to replace the default H2 database with another database of your choice, you need to replace the H2 dependency with a dependency to the corresponding database connector library into
pom.xml. For example, if you use MySQL, you can add a dependency to
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
For Postgres DB you need to add
<dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency>
The application configuration is stored in
src/main/resources/application.properties. This is where you can provide the system properties required to establish a database connection. For example, for a MySQL connector you can set the following properties
spring.jpa.hibernate.ddl-auto=update spring.datasource.url=jdbc:mysql://localhost:3306/dbname spring.datasource.username=username spring.datasource.password=password
Here the URL to the database is specified with the
spring.datasource.url property. Most often Spring Boot can automatically deduce which database is in use from the URL and you do not have to explicitly specify it. For specifying a PosgreSQL database, you just need to change the URL to
If for some reason you need to have more fine-grained control over this, you can add explicit properties like
spring.jpa.hibernate.ddl-auto defines how the database should be initialized when the application starts. The valid options are
nonedoes not modify the database in any way on application start-up
validatevalidate the schema, makes no changes to the database.
updatetries to modify the existing schema to match the schema defined by the application without deleting old data. It creates the database if it does not exist yet.
createIt also creates the database if it does not exist yet, then creates the schema from scratch, destroying previous data.
create-dropalways deletes any existing database and creates it and the schema from scratch. It drops the schema when the application is stopped.
The default option depends on the choice of the database. For in-memory databases (like H2) Spring by default does
create-drop while for persistent databases (like MySQL) Spring often uses
When Bakery runs on a persistent database such as MySQL, it does not attempt to create sample data if it detects that the data already exists.
|Do not commit sensitive data such as production database URL, username and password into your project repository. You can have these as variables and have your build tool (e.g. Maven) substitute the appropriate values for them during build.|
For example, the
application.properties configuration above could be expressed as:
spring.jpa.hibernate.ddl-auto=update firstname.lastname@example.org@ email@example.com@ firstname.lastname@example.org@
Then to run the application:
mvn jetty:run -Ddb.url=$dbUrl -Ddb.user=$dbUser -Ddb.password=$dbPassword
In a CI environment, the parameters |
You can read more on how to configure the database in Spring documentation: Working with SQL databases.