Spring Boot 3 with H2 database example

realNameHidden - Jun 15 '23 - - Dev Community

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