Configuring the DataBase in Spring Boot applications

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 https://github.com/fmarchioni/mastertheboss/tree/master/spring/SpringBootJPA we have built so far:

SpringBootJPA/
├── pom.xml
└── src
    └── main
        ├── java
        │   └── com
        │       └── mastertheboss
        │           └── springboot
        │               ├── App.java
        │               ├── model
        │               │   └── City.java
        │               └── service
        │                   ├── CityRepository.java
        │                   ├── CityServiceImpl.java
        │                   └── CityService.java
        ├── resources
        │   ├── application.properties
        │   ├── import.sql
        │   ├── log4jdbc.log4j2.properties
        └── webapp

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

spring.datasource.url=jdbc:postgresql://localhost/demodb
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.properties.hibernate.dialect =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:

<dependency>
    <groupId>postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.1-901-1.jdbc4</version>
</dependency>

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

package com.mastertheboss.springboot.model;

import java.io.Serializable;

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;

@Entity
public class City implements Serializable {

	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Basic(optional = false)
	@NotNull
	@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();
		this.name = name;
		this.country = country;
	}

	public String getName() {
		return this.name;
	}

	public String getState() {
		return this.state;
	}

	public String getCountry() {
		return this.country;
	}

	public String getMap() {
		return this.map;
	}

	@Override
	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!

Do you want some more Spring stuff ? check Spring Boot Tutorials !

Found the article helpful? if so please follow us on Socials