Logo

dev-resources.site

for different kinds of informations.

Spring Boot 3 with H2 database example

Published at
6/15/2023
Categories
spring
springboot
h2
db
Author
realnamehidden1_61
Categories
4 categories in total
spring
open
springboot
open
h2
open
db
open
Author
18 person written this
realnamehidden1_61
open
Spring Boot 3 with H2 database example

For More information watch below video

Directory Structure

Image description

add below dependencies

Spring web, datajpa, lombok , h2

pom.xml



<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>SpringBoot3H2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringBoot3H2</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>



Enter fullscreen mode Exit fullscreen mode

Product



package com.example.demo.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Product {

    @Id
    @GeneratedValue
    private Integer prodId;
    private String prodName;
    private double prodCost;
    public Product(String prodName, double prodCost) {
        super();
        this.prodName = prodName;
        this.prodCost = prodCost;
    }


}



Enter fullscreen mode Exit fullscreen mode

ProductRepository



package com.example.demo.repo;

import org.springframework.data.jpa.repository.JpaRepository;

import com.example.demo.entity.Product;

public interface ProductRepository extends JpaRepository<Product, Integer>{

}



Enter fullscreen mode Exit fullscreen mode

TestRunner



package com.example.demo.runner;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.example.demo.entity.Product;
import com.example.demo.repo.ProductRepository;

@Component
public class TestRunner implements CommandLineRunner{

    @Autowired
    private ProductRepository repo;


    @Override
    public void run(String... args) throws Exception {

        Product p1 = new Product("P1",5600.4);
        Product p2 = new Product("P2",6600.4);
        Product p3 = new Product("P3",7600.4);

        //save product

        repo.save(p1);
        repo.save(p2);
        repo.save(p3);

        //find
        Optional<Product> opt = repo.findById(1);
        if(opt.isPresent()) {
            System.out.println(opt.get());
        }else {
            System.out.println("Not FOund");
        }

        //find all
        repo.findAll().forEach(System.out::println);

        //delete by id
        repo.deleteById(1);

        //delete all
        repo.deleteAll();
    }

}



Enter fullscreen mode Exit fullscreen mode
db Article's
30 articles in total
Favicon
🛠️ DB Migration For Golang Services, Why it matters? 🐹
Favicon
MongoDb Atlas: manual backup and restore data
Favicon
Import the database from the Heroku dump
Favicon
Why MongoDB? Exploring the Benefits and Use Cases of a Leading NoSQL Database
Favicon
Database Pool Management with Sidekiq and load_async
Favicon
HyperGraphs In Relation Model
Favicon
ACID in DB
Favicon
Use EXISTS instead of COUNT > 0 when checking if records exist
Favicon
Converting MongoDB ObjectId to Timestamp in Snowflake: A Friendly Guide
Favicon
Level UP your RDBMS Productivity in GO
Favicon
Iniciar sesion en mongodb con usuario y contraseña | Mongodb
Favicon
DB POOL
Favicon
Exploring String Field Types in Databases: CHAR, VARCHAR, and TEXT
Favicon
How to connect to MySQL DB
Favicon
How to provision an AWS RDS MySQL Database
Favicon
Amazon RDS for MySQL
Favicon
Amazon Aurora
Favicon
Amazon RDS
Favicon
Understanding the Differences Between SQL and NoSQL Databases
Favicon
Advantages of Relational DB, Graph DB, and RGDB
Favicon
Understanding Foreign Keys in PostgreSQL
Favicon
What's behind scenes of PostgreSQL from Apache AGE?
Favicon
Spring Boot 3 with H2 database example
Favicon
Basic MongoDB Commands
Favicon
Guide to Apache AGE 1.3.0 Release for PostgreSQL 13
Favicon
Laravel useful DB/Eloquent functions:
Favicon
Docker/DB - Isn't it better to set share directory between host and db container?
Favicon
Alojar Wordpress en la nube (Google Cloud)
Favicon
IceFireDB:A database that supports IPFS storage and Redis protocol, interesting direction.
Favicon
MongoDB Complete Backup And Restore Command With Atlas

Featured ones: