We will create initial project with https://start.spring.io/ tool, we will use these dependencies
lombok
spring web
actuator
validation
jpa
h2
We will use h2
which is a local in-memory database for storing data
Our application.propeeties
file will look like this
server.port=8080
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
Let's break down each property:
server.port=8080
: Specifies the port on which the embedded web server (typically Tomcat) will run. In this case, the application will be accessible on port 8080.spring.datasource.url=jdbc:h2:mem:testdb
: Configures the JDBC URL for the H2 in-memory database. This line indicates that the application is using the H2 in-memory database with the database name testdb
.spring.datasource.driver-class-name=org.h2.Driver
: Specifies the JDBC driver class for the database. Here, it's set to the H2 database driver.spring.datasource.username=sa
: Sets the username to be used when connecting to the database. In this case, it's set to sa
spring.datasource.password=
: Sets the password to be used when connecting to the database. It's an empty string, indicating no password is set.spring.h2.console.enabled=true
: Enables the H2 database console. With this configuration, you can access the H2 console to interact with the in-memory database during development.spring.jpa.hibernate.ddl-auto=update
: Configures Hibernate to automatically update the database schema based on the entity classes. This is useful during development, as it helps in synchronizing the database schema with the entity model.spring.jpa.show-sql=true
: Enables the display of SQL statements in the console. With this setting, you can see the SQL statements executed by Hibernate, which is helpful for debugging and understanding the interactions with the database.Creating our first controller
package com.eliasmanj.accoutns.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AccountsController {
@GetMapping("sayHello")
public String sayHello() {
return "hello world";
}
}