Connecting a database
Overview
In this lab, you will:
- Create a simple Entity and Repository.
- Use a CommandLineRunner to add and remove items from an H2 in-memory database.
- Log the state of the database at runtime.
You will use project from the previous lab springboot-rest-demo.
About H2 Database
- H2 is a lightweight, fast, in-memory database that requires no installation.
- It is often used for testing or development purposes.
- Data is lost when the application stops, because it runs entirely in memory.
- Spring Boot provides an easy way to connect to H2.
- You can also view and interact with your database in the browser using the H2 Console.
- Once configured, access it at:
http://localhost:8080/h2-console - JDBC URL:
jdbc:h2:mem:testdb
- Once configured, access it at:
Project Structure and File Locations
You will store your files in src/main/java/com/neueda.
Adding Pom Dependencies
For this lab you will need to add a some dependencies to the relevant build file.
Maven Dependencies
For Maven add the following to pom.xml found in the root directory:
Click here to view the example solution
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Gradle Dependencies
For Gradle add the following to the build.gradle file:
Click here to view the example solution
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'jakarta.persistence:jakarta.persistence-api:3.1.0'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
// other dependencies...
}
This dependency is used to pull in everything required to connect to a database and interact with it, as well as generating the required DB structure and performing CRUD operations.
A second dependency is required to use the H2 in memory database for simple testing purposes.
H2 Database Dependency - Maven
For Maven this is:
Click here to view the example solution
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
H2 Database Dependency - Gradle
For Gradle this is:
Click here to view the example solution
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
testRuntimeOnly 'com.h2database:h2' // Only available during tests
developmentOnly 'com.h2database:h2' // Only available during local development
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
1. Create the Entity
An Entity is a Java class that represents a table in a relational database. It serves as a mapping between your Java objects and database records, forming the foundation of Object-Relational Mapping (ORM) in Spring Boot applications.
- Package: entity
- File: Item.java
- Code:
Click here to view the example solution
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private double price;
// Constructors
public Item() {
}
public Item(String name, double price) {
this.name = name;
this.price = price;
}
// Getters and Setters
public Long getId() {
return id;
}
public String getName() {
return name;
}