Skip to content

simulations v0.2

Albert edited this page Apr 25, 2024 · 12 revisions

Project Overview

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

Create project

create project

Project

  • IDE: IntelliJIdea Community 2023.3

Project structure

image

Dependencies

Maven Project from Spring Boot init

pom.xml

Tech stack

  • Spring Boot 3.2.3
  • Thymeleaf 3.1.2
  • Api Rest
  • Postman to test
  • H2 DataBase

Code

Model

simulation java @Entity

simulation java @Entity

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;
}

Configuration

application.properties

application properties

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=create

Controller & Service

Repository

Templates

H2 database

H2 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.

H2 connection

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.

image

Step-by-step open web console

  1. URL: Navigate to http://localhost:8083/h2-console
  2. Select H2 Embedded: Choose the H2 embedded database option
  3. Driver Class and JDBC URL: Use org.h2.Driver as the driver class and jdbc:h2:mem:database-connection-unique-id as the JDBC URL
  4. Username and Password: Enter sa as both the username and no password

image

DDL

DDL

H2 console

image

Outputs & Running

Screenshots

H2 DB

image

image

API rest

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

image

Clone this wiki locally