Configuring the DataBase in Spring Boot applications

User Rating: 3 / 5

Star ActiveStar ActiveStar ActiveStar InactiveStar Inactive

In this tutorial we have learn how to setup a Spring Boot JPA application and deploy in on a Java EE container using the default H2 database: SpringBoot with JPA on WildFly. Now we will learn how to change the Database settings.

So here's the initial Project we have built so far:

├── pom.xml
└── src
    └── main
        ├── java
        │   └── com
        │       └── mastertheboss
        │           └── springboot
        │               ├──
        │               ├── model
        │               │   └──
        │               └── service
        │                   ├──
        │                   ├──
        │                   └──
        ├── resources
        │   ├──
        │   ├── import.sql
        │   ├──
        └── webapp

What we need is at first specifying the JDBC settings in the file Supposing we will use PostgreSQL:

spring.datasource.driver-class-name=org.postgresql.Driver =org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto = create-drop

Of course the property spring.jpa.hibernate.ddl-auto = create-drop is set just for demonstration purposes! Next, don't forget to include the JDBC driver dependency in your pom.xml file:


Finally, some adjusting might be required to handle the auto-generation of the primary key.

package com.mastertheboss.springboot.model;


import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;

public class City implements Serializable {

	private static final long serialVersionUID = 1L;
	@Basic(optional = false)
	@Column(name = "id", nullable = false)
	private Integer id;

	@Column(nullable = false)
	private String name;

	@Column(nullable = false)
	private String state;

	@Column(nullable = false)
	private String country;

	@Column(nullable = false)
	private String map;

	protected City() {

	public City(String name, String country) {
		super(); = name; = country;

	public String getName() {

	public String getState() {
		return this.state;

	public String getCountry() {

	public String getMap() {

	public String toString() {
		return getName() + "," + getState() + "," + getCountry();

We have used the @GeneratedValue annotation to specify how the primary key should be generated. In our case, we are using an Identity strategy which indicates that the database (Postgresql) must assign primary keys for the entity using a database identity column.

That's all. Build, Deploy, Test it!