-
Notifications
You must be signed in to change notification settings - Fork 11
simulations v0.2
In this lab we are going to:
- Focus on create Maven project with H2 JPA
- Test it with API Rest and web
- Work on @Entity
- IDE: IntelliJIdea Community 2023.3

Maven Project from Spring Boot init
- Spring Boot 3.2.3
- Thymeleaf 3.1.2
- Api Rest
- Postman to test
- H2 DataBase
package com.example.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor @AllArgsConstructor
@Entity
public class Simulation {
private String id;
private String createdAt;
private int time;
private String user;
}
server.port=8089
spring.h2.console.enabled=true
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false
#H2 DATASOURCE
##spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.url=jdbc:h2:mem:0f74afd0-eefb-4b37-af9f-64249cb2ffa2
#spring.datasource.url=jdbc:h2:tcp:/localhost/~/home/albert/MyProjects/MyDBs/h2DBs/monitorBook
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
#JPA
#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.defer-datasource-initialization=true
#Remove banner
spring.main.banner-mode=off
#DDL
#example: create table book_table (book_id bigint not null, author varchar(255), isbn varchar(255), pages integer not null, published_year integer, book_title varchar(255), primary key (book_id))
spring.jpa.hibernate.ddl-auto=createH2 Database is an open-source relational database management system (RDBMS) written in Java. It is designed to be fast, lightweight, and embeddable, making it suitable for development, testing, and small to medium-sized applications. H2 Database supports standard SQL syntax and provides features such as transactions, triggers, stored procedures, and more.
One of the notable features of H2 Database is its ability to run in embedded mode, where it can be embedded within Java applications, allowing developers to create self-contained applications with an embedded database. Additionally, H2 Database supports in-memory databases, which are stored entirely in RAM, providing extremely fast read and write operations, albeit with data being volatile and not persisted beyond the lifetime of the application.
H2 Database is popular among developers for its simplicity, performance, and ease of integration, making it a preferred choice for projects requiring an embedded or lightweight database solution.
2024-03-13T15:56:23.178+01:00 INFO 359 --- [ restartedMain] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:0f74afd0-eefb-4b37-af9f-64249cb2ffa2 user=SA
2024-03-13T15:56:23.183+01:00 INFO 359 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2024-03-13T15:56:23.215+01:00 INFO 359 --- [ restartedMain] o.s.b.a.h2.H2ConsoleAutoConfiguration : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:0f74afd0-eefb-4b37-af9f-64249cb2ffa2'
Upon startup, Spring Boot initializes a connection pool using HikariCP, a high-performance JDBC connection pooling library. It logs the addition of a connection named conn0, indicating the successful creation of the connection to the H2 in-memory database.
The URL jdbc:h2:mem:0f74afd0-eefb-4b37-af9f-64249cb2ffa2 specifies the database connection details, including the database type (H2), the in-memory storage (mem), and a unique identifier (0f74afd0-eefb-4b37-af9f-64249cb2ffa2).
Subsequently, Spring Boot confirms the completion of the startup process for the Hikari connection pool, indicating that it's ready to handle database connections.
Additionally, Spring Boot autoconfigures the H2 console, making it available at the '/h2-console' endpoint.

-
URL: Navigate to
http://localhost:8083/h2-console - Select H2 Embedded: Choose the H2 embedded database option
-
Driver Class and JDBC URL: Use
org.h2.Driveras the driver class andjdbc:h2:mem:database-connection-unique-idas the JDBC URL -
Username and Password: Enter
saas both the username and no password




First at all, you should populate your db table simulations:
http://localhost:8089/api/v1/simulation/populate
and then, fetch data:
http://localhost:8089/api/v1/simulation/simulations

React - Spring Boot - API Rest - H2 DataBase - Axios - React_router_dom - JPA